9.1.7 Checkerboard V2 Codehs Instant

Some "V2" extensions require non-square patterns. The parity rule still applies, but you might need to offset odd rows. Example: staggered rows like a brick wall.

Solution: If row % 2 == 1, start with the opposite color. Equivalent to:

if (row % 2 == 0) 
    // normal parity
 else 
    // shifted: (col % 2 == 0) gives opposite
  • For each cell (row, col):
  • Decide color by parity: if (row + col) is even → one color (e.g., black), else the other (e.g., white).
  • Create a square (e.g., Rectangle) sized squareSize and positioned at (x, y).
  • Set its fill color and add it to the canvas.
  • Ensure no stroke (or set stroke to same as fill) if you want seamless adjacent squares.
  • If window size must be set, use width = N * squareSize and height = N * squareSize.
  • The Checkerboard V2 exercise is an excellent way to practice:

    Once you understand the toggle-and-reset pattern, you can adapt this code to draw any tile-based pattern (chessboards, game boards, pixel art, etc.).

    Happy coding! 🧩


    Have questions or an alternate solution? Drop a comment below!


    In CodeHS graphics (using the GraphicsProgram or Turtle), you will use add(new Rectangle(x, y, width, height)) or similar methods.


    A standard checkerboard has a simple rule:
    If (row + column) % 2 == 0, use Color A; else, use Color B.

    But V2 often forbids that direct math approach and asks you to explicitly toggle a boolean variable. 9.1.7 Checkerboard V2 Codehs

    Example pattern (B = black, W = white):

    Row 0: B W B W B W B W
    Row 1: W B W B W B W B
    Row 2: B W B W B W B W

    If each square is 50x50 pixels, the top-left corner of the square at (row, col) is:

    function start()
        var boardSize = 8;
        var squareSize = 50;
        var colors = ["red", "black"];
    
    for(var i = 0; i < boardSize; i++)
        for(var j = 0; j < boardSize; j++)
            var square = new Rectangle(squareSize, squareSize);
            square.setPosition(j * squareSize, i * squareSize);
            square.setColor(colors[(i + j) % 2]);
            add(square);
    

    If you are navigating the CodeHS Java (or JavaScript) curriculum, particularly in the "Advanced Arrays" or "Graphics" sections, you have likely encountered Exercise 9.1.7: Checkerboard V2.

    At first glance, it seems simple: draw a checkerboard. However, this problem is a classic exercise in nested loops, conditional logic, coordinate math, and efficient rendering. It strips away the fluff of game logic and focuses on the core visual structure of an 8x8 grid.

    This article will break down the problem, explore the common pitfalls, provide step-by-step solutions in both Java (Console/Graphics) and JavaScript (Web Graphics), and explain the underlying principles so you can truly master the concept. Some "V2" extensions require non-square patterns