9.1.7 Checkerboard V2: Codehs

Using setFilled(true) alone might leave a tiny border. Some CodeHS exercises expect setFilled(true) without setColor for the outline. If gaps appear, set the outline color to match the fill color:

square.setColor(square.getFillColor());

A checkerboard is defined by a simple mathematical rule: A square’s color is determined by the parity (even/odd) of the sum of its row and column indices.

If (row + column) % 2 == 0 → Color A.
If (row + column) % 2 == 1 → Color B.

This is the secret sauce. Memorize this rule – it is the heart of the 9.1.7 exercise. 9.1.7 Checkerboard V2 Codehs

You need to draw an 8x8 grid. The squares must alternate colors:


❌ Using two separate loops for colors.
❌ Forgetting to set the fill color before adding the rectangle.
❌ Off-by-one errors in the loop conditions.
❌ Hardcoding square size and board dimensions without using constants.

In Checkerboard V1, you might have created a static 8x8 board.
In Checkerboard V2, the requirements typically change in one of two ways (depending on your school’s version): Using setFilled(true) alone might leave a tiny border

The goal is still the same: draw alternating black and red (or black and white) squares that form a chessboard pattern.

Here’s a clean, V2-compliant solution in Java (using CodeHS’s GraphicsProgram and GRect):

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class CheckerboardV2 extends GraphicsProgram private static final int SIZE = 50; // square side length private static final int ROWS = 8; private static final int COLS = 8; A checkerboard is defined by a simple mathematical

public void run() 
    boolean isBlack = true;  // start with black for row 0
for (int row = 0; row < ROWS; row++) 
        for (int col = 0; col < COLS; col++) 
            int x = col * SIZE;
            int y = row * SIZE;
GRect square = new GRect(x, y, SIZE, SIZE);
            if (isBlack) 
                square.setFilled(true);
                square.setFillColor(Color.BLACK);
             else 
                square.setFilled(true);
                square.setFillColor(Color.RED); // or Color.WHITE
add(square);
// Toggle color for next square in the row
            isBlack = !isBlack;
// After finishing a row, toggle the starting color for the next row
        // Since we toggled an odd number of times (8 toggles),
        // the boolean is already opposite of row start.
        // But careful: after even number of columns, toggling 8 times
        // brings us back to original state. So we must toggle once more
        // to alternate row starting color.
        if (COLS % 2 == 0) 
            isBlack = !isBlack;

  • Order of Addition: In JavaScript, adding squares in nested loop order works fine because they are non-overlapping.