This public link is valid for 7 days and shares a thread, including any personal information you added. This link or copies made by others cannot be deleted. If you share with third parties, their policies apply. Can’t copy the link right now. Try again later.
Most CodeHS versions of this exercise use the Grid class or a simple graphics library. Below is the standard structural approach using nested for loops. javascript
You need to iterate 8 times to create each row. Inside this loop, you will determine what values to add based on the row index Apply Logic for Pieces vs. Blanks Pieces (1s) statement to check if the current row index is less than 3 (top) OR greater than 4 (bottom). Blanks (0s) statement for the middle rows. Example Implementation
// Constants for the checkerboard layout var NUM_ROWS = 8; var NUM_COLS = 8; var COLOR_ONE = Color.red; var COLOR_TWO = Color.black; function start() // Calculate size dynamically based on canvas width var squareSize = getWidth() / NUM_COLS; // Outer loop iterates through each row for (var r = 0; r < NUM_ROWS; r++) // Inner loop iterates through each column in the current row for (var c = 0; c < NUM_COLS; c++) // Create the square geometry var square = new Rectangle(squareSize, squareSize); // Calculate the X and Y screen positions var xPos = c * squareSize; var yPos = r * squareSize; square.setPosition(xPos, yPos); // Check if the sum of row and column is even or odd to alternate colors if ((r + c) % 2 === 0) square.setColor(COLOR_ONE); else square.setColor(COLOR_TWO); // Render the square to the canvas add(square); Use code with caution. Code Step-by-Step Breakdown 9.1.6 checkerboard v1 codehs
grid, a checkerboard pattern alternates colors. If you look at the coordinates of any square: Square (0,0) is Color A. Square (0,1) is Color B. Square (1,0) is Color B. Square (1,1) is Color A.
public void run() // Set the canvas size setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Define constants for better readability private static final int ROWS = 8; private static final int COLUMNS = 8; private static final int SQUARE_SIZE = 50; private static final int WINDOW_WIDTH = COLUMNS * SQUARE_SIZE; // 400 private static final int WINDOW_HEIGHT = ROWS * SQUARE_SIZE; // 400 This public link is valid for 7 days
The mathematical secret to this pattern is the . If you add the row index and the column index Even sums result in one color. Odd sums result in the other color. The Code Implementation
Completing "Checkerboard, v1" teaches you several key concepts:
Hardcoding pixel values makes code fragile and unresponsive to screen resizing. This solution uses getWidth() to automatically adapt to any canvas size: Can’t copy the link right now
The 9.1.6 Checkerboard V1 is a specific project within the CodeHS platform. It's a coding exercise that challenges users to create a functional checkerboard game using a programming language, typically JavaScript or Python.
Never try to move() if you are at a wall. This will cause a Karel crash.