9.1.7 Checkerboard V2 Answers 🆒

Checkerboard Problem Overview

The checkerboard problem usually requires creating a program that can:

for row from 0 to 7: for col from 0 to 7: x = col * squareSize y = row * squareSize if (row + col) % 2 == 0: color = RED else: color = BLACK draw a square at (x, y) of size squareSize with fill color

# 2. Use nested loops to replace 0s with 1s in a checkerboard pattern 9.1.7 checkerboard v2 answers

to display each row of the grid as a string of numbers separated by spaces. Python Solution Code

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) 9.1.7 checkerboard v2 answers

def board(): my_grid = [] for i in range(8): # Even rows start with 0 if i % 2 == 0: my_grid.append([0, 1] * 4) # Odd rows start with 1 else: my_grid.append([1, 0] * 4) # Print each row in the grid for row in my_grid: print(row) board() Use code with caution. Copied to clipboard 1. Initialize the grid container

Cell at row r, column c is black if (r + c) % 2 == 0, otherwise white (or vice versa). 9.1.7 checkerboard v2 answers

print_board Function: A helper function often provided in the exercise to format the 2D list into a readable grid in the console.