9.1.6 Checkerboard V1 Codehs [new]
9.1.6 Checkerboard v1
Here's the code for the exercise on CodeHS:
Variable Confusion:
Swapping row and col inside the setPosition function is the most common reason for a "sideways" or broken grid. Remember: X is Columns, Y is Rows. 9.1.6 checkerboard v1 codehs
Edge cases and implementation details
- Using a
whileloop to handle rows. - Using a
whileloop inside to handle columns. - Alternating the starting position (with beeper or without) each row.
- Placing beepers on every other cell in a row.
- Using helper functions for better readability.
2. Set Up the Grid
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 Using a while loop to handle rows
Initialize the Grid
: Create a 2D list (an 8x8 grid) filled entirely with 0s. 9.1.6 checkerboard v1 codehs