9.1.6 Checkerboard V1 Codehs !full! -
The goal of the 9.1.6 Checkerboard v1 exercise in CodeHS is to create a 2D array representing an
checkerboard where '0' represents a white square and '1' represents a black square. Final Code Implementation
public class Checkerboard extends ConsoleProgram public void run() // 1. Create a 2D array of size 8x8 int[][] board = new int[8][8]; // 2. Nest loops to traverse rows and columns for (int row = 0; row < board.length; row++) for (int col = 0; col < board[0].length; col++) // 3. Logic: If (row + col) is even, it's a 0. If odd, it's a 1. if ((row + col) % 2 == 0) board[row][col] = 0; else board[row][col] = 1; // 4. Print the result using the provided grid printer printBoard(board); // Helper method to print the 2D array public void printBoard(int[][] board) for(int[] row : board) for(int el : row) System.out.print(el + " "); System.out.println(); Use code with caution. Copied to clipboard 1. Initialize the 2D Array
The first step is to declare and initialize a 2D integer array. Since a standard checkerboard is , the syntax is int[][] board = new int[8][8];. 2. Implement Nested For-Loops
To touch every single square on the board, you need two loops. The outer loop iterates through the rows ( ), and the inner loop iterates through the columns ( ) for each of those rows. 3. Apply Alternating Logic
The trick to the checkerboard pattern is checking the sum of the indices If the sum is even, the square should be 0. 9.1.6 checkerboard v1 codehs
If the sum is odd, the square should be 1.This ensures that as you move one space to the right or one space down, the value always flips, creating the alternating pattern. 4. Visualize the Grid
A checkerboard pattern relies on the parity of the coordinates. You can visualize the index sums like this: ✅ Final Result The program successfully generates an grid where every adjacent cell alternates between , starting with at position [0][0].
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()
Informative feature: "9.1.6 checkerboard v1 (CodeHS)"
Extending the Challenge
Once you have mastered 9.1.6 Checkerboard v1, challenge yourself with these modifications: The goal of the 9
- Variable Size: Ask the user for the number of rows/columns and square size.
- Random Colors: Fill the checkerboard with random colors instead of a fixed pattern.
- Click Detection: Make the program print the color of a square when the user clicks on it.
- Animations: Make the colors swap every second.
5. Implementation (JavaScript for CodeHS Karel)
function start()
// Start with a beeper on the first cell
if (noBeepersPresent())
putBeeper();
// Loop through rows
while (leftIsClear()
// Fill the current row starting with beeper presence based on row parity
function fillRow()
// Determine if first cell should have a beeper
// For row 1: have beeper, row 2: no beeper, etc.
// We check if we are on a beeper to decide pattern
var startWithBeeper = beepersPresent();
while (frontIsClear())
move();
if (startWithBeeper)
// Alternate pattern: after moving, we want opposite
// Better approach: move, then if column index is even/odd
// But simpler: use a counter
However, a more reliable approach:
function start()
var row = 1;
while (true)
// Determine if row starts with beeper
var startWithBeeper = (row % 2 == 1);
// Fill the row
var col = 1;
while (true)
if (startWithBeeper)
if (col % 2 == 1)
putBeeper();
else
if (col % 2 == 0)
putBeeper();
if (frontIsClear())
move();
col++;
else
break;
// Move to next row
if (facingEast())
if (leftIsClear())
turnLeft();
move();
turnLeft();
row++;
else
break;
// Optional: return to start
turnAround();
while (frontIsClear())
move();
turnAround();
Simplified and cleaner version (recommended for CodeHS submission):
function start()
// Create checkerboard pattern
var row = 1;
while (true)
for (var i = 0; i < 100; i++) // max columns
if (row % 2 == 1)
if (i % 2 == 0) putBeeper();
else
if (i % 2 == 1) putBeeper();
if (frontIsClear()) move();
else break;
// Go to next row
if (leftIsClear())
turnLeft();
move();
turnLeft();
row++;
else
break;
3. Squares Not Filled
Problem: The squares are outlined but not colored.
Fix: You must call both setFillColor() and setFilled(true).
Edge cases & testing
- Odd/even grid sizes (verify center pattern).
- Non-square grids (rows != cols) — still alternate based on parity.
- Parameterized start color: flip condition if top-left should be empty.
- Ensure newlines and spacing are consistent for console output.
