The objective is to create a checkerboard pattern using a 2D array logic concept. You are usually provided with a Rectangle class and a Checkerboard class (which uses Grid).
In "Checkerboard v1", the standard logic is to determine the color of a square based on the sum of its row and column indices.
Some versions of the CodeHS exercise use red instead of gray. If your prompt says "red and black", simply change the color in the conditional:
if ((row + col) % 2 == 0)
square.setFillColor(Color.RED);
else
square.setFillColor(Color.BLACK);
In this specific CodeHS exercise, you typically edit the file named Checkerboard.java. You are expected to fill in the logic inside the nested for loops to set the color of the Rectangle objects stored in a 2D array.
Here is the completed code for the relevant section: 9.1.6 checkerboard v1 codehs
/* * This class represents a checkerboard. * It creates a grid of Rectangle objects. */ public class Checkerboard private Rectangle[][] board; private int size;public Checkerboard(int size) this.size = size; board = new Rectangle[size][size]; // Create the Grid to display the board Grid grid = new Grid(size, size, 50); // Nested loops to iterate through the 2D array for(int row = 0; row < size; row++) for(int col = 0; col < size; col++) // 1. Create a new Rectangle object Rectangle rect = new Rectangle(); // 2. Determine color based on row + col sum if((row + col) % 2 == 0) rect.setColor(Color.BLACK); else rect.setColor(Color.WHITE); // 3. Add the rectangle to the array board[row][col] = rect; // 4. Add the rectangle to the Grid to visualize it grid.add(rect, row, col);
Problem: The squares are outlined but not colored.
Fix: You must call both setFillColor() and setFilled(true).
The Modulo Operator (%):
Setting the Color:
Grid Integration:
function start() var row = 1; while (true) // Fill current row var col = 1; while (true) if (row % 2 == 1) if (col % 2 == 1) putBeeper(); else if (col % 2 == 0) putBeeper(); if (frontIsClear()) move(); col++; else break;// Move to next row if (leftIsClear()) turnLeft(); move(); turnLeft(); row++; else break;
End of Report
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()
Course: CodeHS Introduction to Programming (JavaScript)
Module: 9.1 - Karel Challenges
Problem: 9.1.6 Checkerboard v1
Objective: Write a Karel program that places a checkerboard pattern of beepers on a rectangular world of any size (within Karel’s limits). The objective is to create a checkerboard pattern