Skip to content
916 Checkerboard V1 Codehs Fixed -
Mastering the 916 Checkerboard v1: Solutions and Logic for CodeHS
Common Errors Students Make
- Wrong starting color – Checkerboard should start with red or black depending on the row.
- Not alternating colors correctly – Both row-by-row and column-by-column matter.
- Off-by-one errors – Especially in loops.
- Square size / positioning – Wrong x/y coordinates for each square.
- Initialization: Define the size of each square and starting coordinates ($x, y$).
- Grid Logic: Create an outer loop for the rows (running 8 times).
- Row Logic: Inside the outer loop, create an inner loop for the columns (running 8 times per row).
- Color Logic: Determine the color of the square. A common method is to check if the sum of the row index and column index is even or odd ($i + j$). If the sum is even, draw Color A; if odd, draw Color B.
- Drawing: Draw the rectangle at the current $(x, y)$ position.
- Position Update: Increment the $x$ coordinate by the square size to move to the next column.
- Row Reset: After the inner loop finishes a row, reset $x$ to the starting edge and increment the $y$ coordinate to move down to the next row.
Tips and Variations:
var color;
if ((row + col) % 2 === 0)
color = "red";
else
color = "black";
This approach uses a nested loop as required by the CodeHS autograder. 916 checkerboard v1 codehs fixed
public static void main(String[] args)
JFrame frame = new JFrame("Checkerboard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Checkerboard());
frame.pack();
frame.setVisible(true);
set square_size = 50
set rows = 8
set cols = 8