9.1.6 Checkerboard V1 Codehs
The "9.1.6 Checkerboard v1" exercise in CodeHS is a classic challenge designed to test your mastery of and 2D arrays (or grids). Creating a checkerboard pattern requires a logical approach to alternating colors based on row and column indices.
This exercise focuses on using nested loops modulus operator
So, why is the 9.1.6 Checkerboard V1 so important? This project holds significant value for several reasons:
It belongs to the other "color" if the sum of its indices is . Example Code Implementation (Java) 9.1.6 checkerboard v1 codehs
turnAround();
if the prompt specifically requests them, as simply printing the pattern without storing it in a grid may cause errors. Typical Pitfalls Incorrect Function Placement : Defining the print_board function inside another block or incorrectly indenting it. Missing Middle Rows
Do you need to add specialized features like or moving game pieces ? Share public link The "9
: If the sum leaves a remainder, it colors the square white . Visual Example Matrix (Row + Col) : Top-Left cell (0,0) : →right arrow Next cell right (0,1) : →right arrow First cell second row (1,0) : →right arrow Second cell second row (1,1) : →right arrow 3. Calculating Coordinates
// 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
While the exercise uses Python, the core programming logic is universal. Here is how the same solution looks in JavaScript, a language also widely used on CodeHS: This project holds significant value for several reasons:
Here is the complete, clean solution for the CodeHS 9.1.6 Checkerboard v1 exercise using JavaScript Graphics. javascript
: Forgetting that the middle two rows (index 3 and 4 in an 8-row grid) must remain empty (0s). Bypassing Assignment