: Creating a list of lists (a 2D list) representing the 8x8 board.
print_board(my_grid)
Here is the accepted, functional code that passes the CodeHS auto-grader.
The secret to solving any checkerboard algorithm lies in basic coordinate math. Every square in your grid has a specific address defined by its Row index ( r ) and Column index ( c ).
The solution above is correct for the specific 9.1.7 assignment on CodeHS. However, many "checkerboard" problems require a pattern that alternates with every single square, similar to a chessboard (e.g., starting with 0, then 1, then 0). 9.1.7 checkerboard v2 answers
logic is the most efficient way to solve version 2 of this problem. In "v1," you might have only alternated colors per row, but adding the row and column indices together ensures that if you are on an even row, the pattern starts with "Color A," and if you are on an odd row, it starts with "Color B." ✅ Final Result The answer is achieved by using nested for loops combined with the conditional statement if (row + col) % 2 == 0
By using (row + col) % 2 == 0 , we alternate 0s and 1s perfectly. 2. The Nested Loop Structure
This often involves printing an 8x8 grid that alternates between two different colors (typically black and white) for each square.
Double-check if the top-left square (0,0) is supposed to be black, white, red, or active. Swap your if/else block conditions if the colors are inverted. : Creating a list of lists (a 2D
The outer for loop runs once for each row. For every iteration, a new empty list called row is created. This list will hold the values for that specific row.
). The answer lies in identifying the parity of the grid coordinates.
Encapsulate the data (the board) and methods (placing pieces, making moves).
: Ensure your loops run exactly range(8) to match the 8x8 requirement. Every square in your grid has a specific
, successfully passing the autograder's requirement for using assignment statements to modify the list. If you'd like, I can: Show you how to modify this for a larger grid Explain how to change the symbols (e.g., using "X" and "O" instead of 1 and 0). Checkerboard v3 if you're stuck on the next level. Let me know how you'd like to continue your project!
public void run() // Loop through each row for (int row = 0; row < NUM_ROWS; row++) // Loop through each column in the current row for (int col = 0; col < NUM_COLS; col++) // Calculate the x and y coordinates for this square int x = col * SQUARE_SIZE; int y = row * SQUARE_SIZE;
Tracks the vertical movement down the screen.