9.1.6 Checkerboard V1 Codehs [exclusive] -

CodeHS Exercise 9.1.6 (Checkerboard, v1) requires creating an 8x8 grid, utilizing nested loops to populate top and bottom rows with 1s in alternating positions while leaving middle rows as 0s. The solution initializes a 2D list and applies an assignment statement to update specific cells where the sum of the row and column indices is odd. For detailed code solutions, review the answers at

It looks like you are working on the 9.1.6 Checkerboard assignment in the CodeHS Graphics course. In this assignment, you are typically asked to write a function called create_checkerboard that draws an 8x8 grid of alternating black and white squares.

Here is the solution code:

# Constants for the board size and square size
NUM_ROWS = 8
NUM_COLS = 8
SQUARE_SIZE = 50
def create_checkerboard():
    # Create the main window
    win = Window()
    win.set_background("white")
# Loop through rows (vertical)
    for row in range(NUM_ROWS):
        # Loop through columns (horizontal)
        for col in range(NUM_COLS):
# Create the square at the correct position
            square = Rectangle(SQUARE_SIZE, SQUARE_SIZE)
            square.set_position(col * SQUARE_SIZE, row * SQUARE_SIZE)
# Determine the color based on the sum of row and col indices
            # If the sum is even, it's one color; if odd, it's the other.
            if (row + col) % 2 == 0:
                square.set_color(Color.black)
            else:
                square.set_color(Color.white)
# Add the square to the window
            win.add(square)
return win
# Call the function to display the board
create_checkerboard()

Key Requirements:

  • Canvas Size: 400x400 pixels.
  • Grid Structure: 8 rows, 8 columns (8x8 = 64 squares).
  • Square Size: 50x50 pixels (since 8 * 50 = 400).
  • Colors: Typically gray (Color.gray) and black (Color.black), or sometimes red and black.
  • Starting Color: Top-left corner (row 0, column 0) = Gray.
  • Alternating Pattern: Adjacent squares (left/right and up/down) must have opposite colors.

Problem Understanding

You need to create a checkerboard pattern (alternating black and red squares) using a grid of squares, typically with n rows and n columns (often n = 8 for a standard checkerboard).

2. Determining the Color

The challenge is deciding when to use gray and when to use black. There is a simple mathematical trick: look at the sum of the row index and column index. 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).

Let's test this:

  • Top-left: row 0, col 0 → 0+0 = 0 (even) → Gray ✓
  • Next to it: row 0, col 1 → 0+1 = 1 (odd) → Black ✓
  • Below: row 1, col 0 → 1+0 = 1 (odd) → Black ✓

This pattern creates a perfect checkerboard. CodeHS Exercise 9

Summary

This document analyzes the problem titled "9.1.6 checkerboard v1" from CodeHS (assumed exercise naming), reconstructs likely requirements, explains correct algorithmic approaches, provides a rigorous step‑by‑step solution, proves correctness, highlights edge cases, and offers an annotated reference implementation in Python (readable pseudocode for educators/students). Assumptions about the exact original prompt are made explicit where the source problem text is unavailable.

The Logic

A checkerboard alternates colors. If you look at the coordinates (row, col): Key Requirements:

  • If (row + column) is even, the square is usually Black.
  • If (row + column) is odd, the square is usually White.

Example:

  • Square (0, 0): $0 + 0 = 0$ (Even) $\rightarrow$ Black
  • Square (0, 1): $0 + 1 = 1$ (Odd) $\rightarrow$ White
  • Square (1, 0): $1 + 0 = 1$ (Odd) $\rightarrow$ White
  • Square (1, 1): $1 + 1 = 2$ (Even) $\rightarrow$ Black

Step-by-Step Guide

Variations and extensions

  • Allow rectangular boards m×n: use parity on (r + c) same way.
  • Variable square size (graphical).
  • Alternate orientation: start top-left with b instead of a by flipping parity condition.
  • Checkerboard with borders: draw grid lines in addition to filled squares.
  • Chessboard labeling: annotate ranks/files along edges.

2. Set Up the Grid

  • Use nested loops: one for rows, one for columns.
  • Inside the innermost loop, draw a square at the correct (x, y) position.
  • The size of each square = total canvas width / number of columns (assuming square canvas).