916 Checkerboard V1 Codehs Fixed -

Mastering the 916 Checkerboard v1: Solutions and Logic for CodeHS

Common Errors Students Make

  1. Wrong starting color – Checkerboard should start with red or black depending on the row.
  2. Not alternating colors correctly – Both row-by-row and column-by-column matter.
  3. Off-by-one errors – Especially in loops.
  4. Square size / positioning – Wrong x/y coordinates for each square.
  1. Initialization: Define the size of each square and starting coordinates ($x, y$).
  2. Grid Logic: Create an outer loop for the rows (running 8 times).
  3. Row Logic: Inside the outer loop, create an inner loop for the columns (running 8 times per row).
  4. 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.
  5. Drawing: Draw the rectangle at the current $(x, y)$ position.
  6. Position Update: Increment the $x$ coordinate by the square size to move to the next column.
  7. 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