9.1.7 Checkerboard V2 Codehs Page
If you're working through the CodeHS Java course (or similar), you've likely encountered the 9.1.7 Checkerboard V2 exercise. It builds on the basic checkerboard concept but adds constraints that force you to think carefully about loops, conditionals, and drawing order.
In this post, I’ll break down exactly what the problem asks, how to plan your solution, and provide the complete code with explanations.
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);
Sometimes students write a complex condition like ((row % 2 == 0 && col % 2 == 0) || (row % 2 != 0 && col % 2 != 0)). This is logically correct but verbose and error-prone. Stick with (row + col) % 2.
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! 9.1.7 Checkerboard V2 Codehs
In CodeHS 9.1.7: Checkerboard V2, the goal is to create a pattern of alternating 1s and 0s in a 2D list (grid). Unlike version 1, which often uses simple row filling, version 2 requires you to use nested for loops and the modulus operator ( ) to check for even and odd positions. Logic for the Pattern
To create a checkerboard, a cell should be a 1 if the sum of its row and column indices is even (or odd, depending on your starting preference). Even sum : Assign 1. Odd sum : Assign 0. Step-by-Step Implementation
Initialize the BoardCreate an empty list and fill it with eight sub-lists, each containing eight zeros. board = [] for i in range(8): board.append([0] * 8) Use code with caution. Copied to clipboard
Iterate and Assign ValuesUse nested loops to traverse every row ( ) and column (
). Use an if statement with the modulus operator to decide where to place a 1.
for i in range(8): for j in range(8): if (i + j) % 2 == 0: board[i][j] = 1 Use code with caution. Copied to clipboard
Print the BoardDefine or use a function to print each row of the list so it looks like a grid.
def print_board(board): for row in board: print(" ".join([str(x) for x in row])) print_board(board) Use code with caution. Copied to clipboard ✅ Result The final output will be an
grid where 1s and 0s alternate perfectly in every direction. If you're working through the CodeHS Java course
1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 Use code with caution. Copied to clipboard
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).
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 Sometimes students write a complex condition like ((row
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 problem is a classic introduction to Nested Loops and Modular Arithmetic. It asks you to draw a checkerboard pattern where the color of each square depends on its position (row and column).
# Get the dimensions from the user (or specific constants if the problem asks)
width = int(input("What width? "))
height = int(input("What height? "))
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):
The goal is still the same: draw alternating black and red (or black and white) squares that form a chessboard pattern.