The solution to the CodeHS 9.1.7: Checkerboard V2 exercise requires creating an 8x8 grid represented by a list of lists, where the values alternate between
to form a checkerboard pattern. Unlike Version 1, which might only change specific rows, Version 2 tests your ability to use nested loops and logic to handle the alternating pattern across the entire board. Core Logic for Checkerboard V2 The key to this exercise is the modulus operator (
, which determines if a row or column index is even or odd. A standard checkerboard pattern follows these rules: Even rows (0, 2, 4, 6) : Start with [0, 1, 0, 1, 0, 1, 0, 1] Odd rows (1, 3, 5, 7) : Start with [1, 0, 1, 0, 1, 0, 1, 0] Step-by-Step Implementation Initialize the Grid Create an empty list (often named ) to hold the rows of your checkerboard. Outer Loop for Rows loop to iterate through the 8 rows of the board. Alternating Row Logic Within the outer loop, check if the current row index is even or odd: i % 2 == 0 : Append a row where elements alternate starting with : Append a row where elements alternate starting with Nested Loop Method (Alternative)
Some versions of this exercise require you to start with a board of all s and use a nested loop to set specific indices to . In this case: Iterate through each row and each column (i + j) % 2 != 0 board[i][j] = 1 Example Python Solution
Below is a standard way to implement this logic to satisfy the CodeHS autograder: create_checkerboard # If the row index is even # Pattern starts with 0 my_grid.append([ # Pattern starts with 1 my_grid.append([ # Print each row to verify the output my_grid: print(row)
create_checkerboard() Use code with caution. Copied to clipboard Common Pitfalls Assignment Requirement
: The CodeHS autograder often checks for an "assignment statement" (e.g., grid[i][j] = 1
). If your code just prints the pattern without actually building the list structure, it may fail even if the output looks correct. Indentation
: In Python, all code within the function must be indented properly. Ensure your
loop is outside the construction loop but still inside the function. Final Result The program creates an 8x8 nested list where board[row][col]
alternates between 0 and 1, effectively simulating a checkerboard in a data structure. Are you having trouble with a specific error message
from the autograder, such as the "assignment statement" requirement?
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
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). 9.1.7 Checkerboard V2 Codehs
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
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
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: The solution to the CodeHS 9
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
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?
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.
This article provides a comprehensive walkthrough for completing the 9.1.7: Checkerboard V2 exercise in CodeHS. This challenge builds upon basic looping concepts by introducing nested loops and conditional logic to create a complex visual pattern. Understanding the Objective
The goal is to create a grid where the colors of the squares alternate like a traditional checkerboard. Unlike the first version of this exercise, "V2" usually requires a more dynamic approach—often utilizing variables for row and column counts or specific helper methods to determine which color should be placed at a specific coordinate. The Logic Behind the Grid
To build a checkerboard, you need to understand the relationship between the row index ( ) and the column index (
Even Rows: If the row index is even, the pattern might start with Color A.
Odd Rows: If the row index is odd, the pattern must start with Color B. : The CodeHS autograder often checks for an
The Sum Rule: A more efficient way to calculate the color is to check if the sum of the row and column indices (
) is even or odd. If the sum is even, use Color A; if odd, use Color B. Step-by-Step Implementation 1. Define Constants
Start by defining the size of your board and the colors you want to use. This makes your code easier to read and modify later. javascript
var GRID_SIZE = 8; var SQUARE_SIZE = getWidth() / GRID_SIZE; var COLOR_ONE = Color.RED; var COLOR_TWO = Color.BLACK; Use code with caution. 2. The Nested Loop Structure
You will need one loop for the rows and another inside it for the columns. javascript
for (var row = 0; row < GRID_SIZE; row++) for (var col = 0; col < GRID_SIZE; col++) // Drawing logic goes here Use code with caution. 3. Applying Conditional Logic
Inside the inner loop, use an if/else statement to decide which color the current square should be. javascript
var x = col * SQUARE_SIZE; var y = row * SQUARE_SIZE; var square = new Rectangle(SQUARE_SIZE, SQUARE_SIZE); square.setPosition(x, y); // The "Checkerboard" Logic if ((row + col) % 2 == 0) square.setColor(COLOR_ONE); else square.setColor(COLOR_TWO); add(square); Use code with caution. Common Pitfalls to Avoid
Off-by-one errors: Ensure your loops start at 0 and end at GRID_SIZE - 1.
Coordinate Math: Remember that the x coordinate is determined by the column, while the y coordinate is determined by the row.
Scaling: If you hardcode the pixel values, the checkerboard won't resize correctly if the GRID_SIZE changes. Always use getWidth() / GRID_SIZE for dimensions.
The 9.1.7 Checkerboard V2 exercise is a rite of passage in CodeHS. It transitions you from writing linear code to thinking in two dimensions. By mastering the nested loop and the modulo operator (%), you gain the tools necessary to build more complex graphics and data structures in the future. Need help with a specific part of the code, or
To solve 9.1.7 Checkerboard V2, you need to be comfortable with these four pillars:
import acm.graphics.*; import acm.program.*; import java.awt.*;public class CheckerboardV2 extends GraphicsProgram
private static final int NUM_ROWS = 8; private static final int NUM_COLS = 8; public void run() double sqWidth = (double) getWidth() / NUM_COLS; double sqHeight = (double) getHeight() / NUM_ROWS; for (int row = 0; row < NUM_ROWS; row++) for (int col = 0; col < NUM_COLS; col++) double x = col * sqWidth; double y = row * sqHeight; GRect square = new GRect(x, y, sqWidth, sqHeight); square.setFilled(true); if ((row + col) % 2 == 0) square.setFillColor(Color.BLACK); else square.setFillColor(Color.RED); add(square);
When you run the code, you should see a perfect 8x8 checkerboard with alternating colors, no bleeding, no misalignment, and the first square of row 0 black, row 1 red/white, row 2 black, etc.