9.1.7 Checkerboard V2 Codehs ((full)) Info

This essay explores the logic and implementation of the Checkerboard V2 challenge in the CodeHS 9.1.7 curriculum

. This exercise is a pivotal moment for students learning Java or JavaScript (Karel), as it transitions from simple movement to complex nested loops and conditional logic. The Objective

The goal of Checkerboard V2 is to create a grid-like pattern of "markers" or "beepers" on a canvas of any size. Unlike the first version, V2 often requires the program to be dynamic—meaning it must work whether the grid is

. The pattern mimics a standard chessboard, where no two markers are adjacent horizontally or vertically. Core Logic: The Nested Loop The foundation of the solution is the nested loop

. To fill a 2D space, the program must iterate through rows and columns. Outer Loop:

Manages the vertical movement (moving from one row to the next). Inner Loop:

Manages the horizontal movement (placing beepers across a single row).

The challenge arises in the alternating nature of the rows. If every row started with a beeper, you would end up with vertical stripes rather than a checkerboard. The Parity Strategy The most elegant way to solve Checkerboard V2 is by using

(even vs. odd). By tracking the current row number and column number, the program can decide whether to place a marker based on the following rule: If the sum of the (Row + Column) is , place a beeper. If the sum is , leave the space empty.

This mathematical approach ensures the pattern remains consistent regardless of the grid’s dimensions. Execution and "The Turnaround"

In many Karel-based versions of this task, the difficulty lies in the "turnaround." Once Karel reaches the end of a row, the program must determine if there is another row above it. If so, Karel must turn, move up, and position itself to face the opposite direction to begin the next line. This requires careful use of 9.1.7 Checkerboard V2 Codehs

statements to check for walls and prevent the program from crashing at the edges of the grid. Conclusion

Solving 9.1.7 Checkerboard V2 is less about the act of placing markers and more about algorithmic thinking

. It teaches students how to use coordinates to control logic and how to write code that is flexible enough to handle varying input sizes. Mastering this exercise signals a transition from a beginner coder to one who understands the structural beauty of computer science. loops or the if/else statements needed for this?

For the CodeHS exercise 9.1.7: Checkerboard, v2 , the objective is to create an grid where the values alternate between in a checkerboard pattern.

The most efficient way to pass the autograder—which often requires specific assignment statements

within nested loops—is to initialize a grid of zeros first and then use the modulus operator ) to change half of them to ones. Correct Solution (Python) # Pass this function a list of lists, and it will # print it such that it looks like the grids in # the exercise instructions. print_board range(len(board)): print( .join([str(x) board[i]])) # 1. Initialize the board with an 8x8 grid of 0s ): board.append([

# 2. Use nested for loops to create the checkerboard pattern # The pattern alternates where (row + col) is even : board[i][j] = # Assignment statement required by autograder # 3. Print the final board print_board(board) Use code with caution. Copied to clipboard Step-by-Step Explanation Initialize the Grid : We start by creating a list of lists ( ) filled with . This establishes the structure before we start modifying values. Nested Loop Logic : We use two loops—one for rows ( ) and one for columns (

). This allows us to access every individual coordinate in the grid. The Checkered Condition

: To get the alternating pattern, we check if the sum of the current row and column indices is even ( (i + j) % 2 == 0 ). If it is, we assign that spot a Example: At board[0][0] (even), so it becomes board[0][1] (odd), so it stays print_board

The CodeHS 9.1.7: Checkerboard V2 exercise is a fundamental lesson in manipulating 2D lists (nested lists) using nested for loops. While Version 1 often focuses on just filling the board, Version 2 requires a more complex logic: creating a alternating pattern of 0s and 1s, similar to a physical checkerboard. 🛠️ Problem Logic This essay explores the logic and implementation of

The goal is to generate an 8x8 grid where elements alternate. In computer science, this is a classic application of the Modulus Operator (%). Grid Structure: A list of lists (8 rows, 8 columns).

The Pattern: If the sum of the row index (i) and column index (j) is even, the value should be 1. If it is odd, the value should be 0 (or vice versa).

Key Constraint: You must use nested loops and assignment statements to modify an existing grid of zeros. 💻 Implementation (Python)

The most efficient way to solve this is to first create a blank board and then use a nested loop to "draw" the pattern.

# 1. Initialize an 8x8 grid filled with 0s board = [] for i in range(8): board.append([0] * 8) # 2. Use nested loops to assign 1s in a checkerboard pattern for i in range(8): # Loop through rows for j in range(8): # Loop through columns # If the sum of indices is even, set to 1 if (i + j) % 2 == 0: board[i][j] = 1 # 3. Print the board to verify for row in board: print(row) Use code with caution. Copied to clipboard 🔍 Why it Works: The "Parity" Rule

The checkerboard pattern relies on the concept of parity (even vs. odd). The Row-Column Sum

By adding the row index and column index (i + j), you create a diagonal wave of even and odd numbers: Row 0, Col 0: 0+0 = 0 (Even) → 1 Row 0, Col 1: 0+1 = 1 (Odd) → 0 Row 1, Col 0: 1+0 = 1 (Odd) → 0 Row 1, Col 1: 1+1 = 2 (Even) → 1 Alternative Approach: Even vs Odd Rows You can also think about it row by row: Even rows (0, 2, 4...) start with 1. Odd rows (1, 3, 5...) start with 0. ⚠️ Common Pitfalls in CodeHS

Hardcoding: Do not just print the lists manually. The autograder looks for the use of the board[i][j] = 1 assignment statement.

Indentation: Python is strict about spacing. Ensure your if statement is inside the second loop, and the second loop is inside the first.

The "V1" Confusion: In Version 1, you might have filled entire rows. In V2, you must alternate within the row. Pro-Tip for Advanced Users Basic JavaScript + Graphics Solution function start() var

If you want to write this more concisely (though CodeHS might prefer the loop method for grading), you can use a List Comprehension:board = [[(i + j + 1) % 2 for j in range(8)] for i in range(8)] If you'd like, I can help you: Debug your specific error message Explain how to change the grid size dynamically

Show how to do this in Java if you are in the Nitro/Java course

CodeHS exercise 9.1.7 Checkerboard V2 requires students to generate an 8x8 2D list, using nested loops and the modulus operator to create an alternating pattern. The core logic involves evaluating (row + col) % 2 to determine if a cell receives a 0 or 1, a key differentiator from the simpler, row-based V1 assignment. Read user discussions on the solution at Reddit.


Basic JavaScript + Graphics Solution

function start()
    var size = 40; // size of each square
    var startX = 0;
    var startY = 0;
for(var row = 0; row < 8; row++)
    for(var col = 0; col < 8; col++)
        var x = startX + col * size;
        var y = startY + row * size;
var rect = new Rectangle(size, size);
        rect.setPosition(x, y);
// Alternate colors
        if((row + col) % 2 == 0)
            rect.setColor("red");
         else 
            rect.setColor("black");
add(rect);

The Solution Code

Here is the correct Python code using the Turtle module to solve this problem.

import turtle
# Setup the screen and turtle
screen = turtle.Screen()
screen.setup(500, 500)
screen.title("Checkerboard V2")
pen = turtle.Turtle()
pen.speed(0) # Fastest drawing speed
pen.hideturtle()
# Define square size
square_size = 50
# We need an 8x8 board
# Outer loop handles the rows (y-axis)
for row in range(8):
    # Inner loop handles the columns (x-axis)
    for col in range(8):
# This is the logic for the checkerboard pattern:
        # If the sum of the row and column indices is even, make it red.
        # Otherwise, make it black.
        if (row + col) % 2 == 0:
            pen.color("red")
        else:
            pen.color("black")
# Draw the square
        pen.begin_fill()
        for i in range(4):
            pen.forward(square_size)
            pen.left(90)
        pen.end_fill()
# Move to the next column position
        pen.forward(square_size)
# After finishing a row, move down to the start of the next row
    pen.backward(square_size * 8) # Return to the left side
    pen.right(90)                 # Turn down
    pen.forward(square_size)      # Move down one row
    pen.left(90)                  # Turn back to facing right

Code Review & Explanation

Here is a detailed review of why this code works and the specific concepts being tested.

3. Incorrect Starting Color

If the expected output starts with a dark square at (0,0), ensure your if branch matches that. Swap colors if needed.

Alternative: Using Variables for Customization

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);

© Bookchoice 2026 - Alle rechten voorbehouden