Do not use absolute numbers like 50 for the square size. CodeHS tests often run your code on multiple screen sizes; dynamic calculations ensure you pass all automated test cases.
function main() while (leftIsClear()) fillRow(); repositionToNextRow(); // Fill the very last row fillRow(); // Fills a row with the checkerboard pattern function fillRow() while (frontIsClear()) putBeeper(); if (frontIsClear()) move(); if (frontIsClear()) move(); putBeeper(); // Moves Karel up and aligns to the next row function repositionToNextRow() if (facingEast()) if (leftIsClear()) turnLeft(); move(); turnLeft(); else if (rightIsClear()) turnRight(); move(); turnRight(); // Helper to turn right function turnRight() turnLeft(); turnLeft(); turnLeft(); Use code with caution. 4. Detailed Explanation of the Code main() Function
To successfully complete this exercise, you must understand the following Python concepts:
is an exercise from the Python curriculum on the CodeHS platform. Its goal is to build a foundational piece of a larger checkers game by constructing a program to represent the game board using numbers. In this simulation, a 1 stands for a checker piece, and a 0 represents an empty square. 9.1.6 checkerboard v1 codehs
This pattern creates the diagonal "stepping stone" look of a checkerboard. 3. Grid Management
int[][] board = new int[8][8]; for (int row = 0; row < 8; row++) for (int col = 0; col < 8; col++) if ((row + col) % 2 == 0) board[row][col] = 0; else board[row][col] = 1; Use code with caution. Copied to clipboard Common Pitfalls
function start(): turn off beeper auto-placement var row = 1 while (front is clear or left is clear): placeRow(row) if (front is clear): moveToNextRow() row++ Do not use absolute numbers like 50 for the square size
for i in range(8): row = [] for j in range(8): if (i + j) % 2 == 0: row.append("R") # R for red else: row.append("B") # B for black board.append(row)
Output:
) to create a grid pattern. In the 9.1.6 Checkerboard assignment, the goal is to alternate colors (usually black and red) across a grid of squares. Key Concepts Nested Loops : You use an outer loop for the and an inner loop for the In this simulation, a 1 stands for a
for i in range(8): # Only modify the top 3 and bottom 3 rows if i < 3 or i > 4: for j in range(8): # If the sum of indices is even, set to 1 if (i + j) % 2 == 0: board[i][j] = 1 Use code with caution. Copied to clipboard 3. Print the Result
By checking if (r + c) is divisible by 2 using the modulo operator ( % ), you can cleanly alternate colors. Step-by-Step Code Implementation