If you need a 10x10 board, change NUM_ROWS and NUM_COLS to 10. Adjust SQUARE_SIZE to getWidth()/10.
Why is the checkerboard pattern so foundational in CS?
In advanced terms: The checkerboard is a 2D cellular automaton with a simple rule, a bipartite graph of grid adjacency, and a quilt of symmetry under translations by (2,0) or (0,2).
You now have the complete answer and, more importantly, the full conceptual breakdown for CodeHS 9.1.7 Checkerboard v2. The solution rests on three pillars: nested loops, modulus arithmetic, and basic ACM graphics. 9.1.7 checkerboard v2 answers
If this guide helped you, consider bookmarking it for future CodeHS challenges like 10.2.5 (Filled Pyramid) or 12.1.7 (Fibonacci Sequence). Happy coding—and may your checkered patterns forever alternate correctly
The solution to the 9.1.7 Checkerboard v2 programming exercise involves using nested for loops conditional logic (the modulo operator
) to create a grid where colors alternate both horizontally and vertically. 1. Initialize the Grid If you need a 10x10 board, change NUM_ROWS
Before drawing, you must define the dimensions of the checkerboard and the size of each square. This ensures your loops run the correct number of times to fill the screen. 2. Set Up Nested Loops To create a 2D grid, use a "loop within a loop." Outer Loop : Controls the rows (the vertical position). Inner Loop : Controls the columns (the horizontal position). 3. Apply Alternating Logic
The "v2" logic usually requires checking if the sum of the row index and column index is even or odd. , draw a square of Otherwise, draw a square of 4. Complete Python Implementation Assuming you are using a standard graphics library (like
or a specialized educational IDE), the logic follows this structure: # Constants for grid size SQUARE_SIZE range(ROWS): range(COLS): # Calculate coordinates = c * SQUARE_SIZE = r * SQUARE_SIZE # Checkerboard logic: alternate based on sum of indices # Function call to draw the square (varies by platform) draw_square(x, y, SQUARE_SIZE, color) Use code with caution. Copied to clipboard Why This Works In advanced terms: The checkerboard is a 2D
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
to toggle colors across a two-dimensional coordinate system. like Turtle or Pygame?
from graphics import *
def draw_checkerboard(win, rows, cols, size, color1, color2, start_color1):
for r in range(rows):
for c in range(cols):
x1 = c * size
y1 = r * size
x2 = x1 + size
y2 = y1 + size
rect = Rectangle(Point(x1, y1), Point(x2, y2))
if (r + c) % 2 == 0:
rect.setFill(color1 if start_color1 else color2)
else:
rect.setFill(color2 if start_color1 else color1)
rect.draw(win)