9.1.6 Checkerboard V1 Codehs May 2026
The solution to CodeHS 9.1.6: Checkerboard, v1 involves creating an 8x8 grid of zeros and then using nested loops to modify the values in specific rows to represent checker pieces. Logic Breakdown
Apply Logic: Inside the loops, use an if-else statement or a simple calculation to assign the value based on the parity of the sum of the indices. 9.1.6 checkerboard v1 codehs
- If
(row + column) % 2 == 0, the square is one color (e.g., gray). - If
(row + column) % 2 == 1, the square is the other color (e.g., black).
for(int row = 0; row < size; row++): This loop runs for every row.for(int col = 0; col < size; col++): Inside every row, this loop runs for every column. This allows us to access every single cell in the grid.
Nested Loops:
3. Challenges
- Variable world size: Karel does not know width or height in advance.
- Alternating rows: The starting column for beepers changes each row.
- Edge cases: Odd/even widths and heights.
- Row transitions: Moving from the end of one row to the start of the next without breaking the pattern.
How it works:
- Loops: We use a nested loop. The outer loop handles the rows (top to bottom), and the inner loop handles the columns (left to right).
- Positioning: We calculate the x and y coordinates for each square by multiplying the current column or row number by the
SQUARE_SIZE. - Alternating Colors: The math trick
(row + col) % 2 == 0is the easiest way to alternate colors.: If you get a red mark saying "You should set some elements of your board to 1," ensure you are actually modifying or appending values to the list, not just printing text that looks like a grid. Function Placement : Always define your print_board function at the top of your script to avoid scope errors. for version 2 of this exercise? The solution to CodeHS 9