9.1.6 checkerboard v1 codehs 9.1.6 checkerboard v1 codehs

Codehs — 9.1.6 Checkerboard V1

Solving this exercise teaches you several fundamental programming concepts:

: Ensure your xPos multiplies by the column variable ( c ) and yPos multiplies by the row variable ( r ). Reversing them will cause your grid to render improperly or throw off-screen coordinates.

: Ensure your loops run from 0 to 7 (less than 8 ).

For the top and bottom sections, we use the expression (row + column) % 2 == 0 to determine if a cell gets a 1 or a 0 . 9.1.6 checkerboard v1 codehs

// Loop through each row for (int row = 0; row < ROWS; row++) // Loop through each column in the current row for (int col = 0; col < COLUMNS; col++)

In this article, we will break down exactly what the 9.1.6 Checkerboard v1 assignment asks for, how to approach the logic, and provide a fully commented solution.

// Constants for the checkerboard dimensions var NUM_ROWS = 8; var NUM_COLS = 8; var SQUARE_SIZE = getWidth() / NUM_COLS; function start() for (var r = 0; r < NUM_ROWS; r++) for (var c = 0; c < NUM_COLS; c++) // Calculate pixel positions var xPos = c * SQUARE_SIZE; var yPos = r * SQUARE_SIZE; // Create the square graphic object var rect = new Rectangle(SQUARE_SIZE, SQUARE_SIZE); rect.setPosition(xPos, yPos); // Determine the color based on row and column indexes if ((r + c) % 2 === 0) rect.setColor(Color.black); else rect.setColor(Color.white); // Draw the square onto the screen add(rect); Use code with caution. Step-by-Step Code Explanation 1. Dynamic Sizing via Constants For the top and bottom sections, we use

Do you need to use instead of standard red/black?

Need help overcoming common challenges? Here are some tips and tricks to keep in mind:

You need an outer loop to control the rows (vertical movement) and an inner loop to control the columns (horizontal movement). Coordinate Math: The canvas uses coordinates. The top-left corner is Need help overcoming common challenges

: Ensure your loops use the strictly less than operator ( < NUM_ROWS ), not less than or equal to ( <= ). JavaScript arrays and grid iterations are zero-indexed.

By checking if (r + c) is divisible by 2 using the modulo operator ( % ), you can cleanly alternate colors. Step-by-Step Code Implementation

9.1.6 checkerboard v1 codehs