V1 Codehs | 9.1.6 Checkerboard

Let's test this:

public class Checkerboard extends ConsoleProgram public void run() // Define the size of the board int numRows = 8; int numCols = 8; // Create the grid Grid board = new Grid(numRows, numCols); // Use a nested loop to traverse every cell for (int row = 0; row < numRows; row++) for (int col = 0; col < numCols; col++) // Check if the sum of row and col is even if ((row + col) % 2 == 0) // Set color (e.g., Black) board.set(row, col, Color.black); else // Set color (e.g., White/Empty) board.set(row, col, Color.white); // Display the board System.out.println(board); Use code with caution. Key Components Explained 1. Nested For Loops

For any given square at row r and column c (using 0-indexed tracking): x position = c * Square Size y position = r * Square Size 2. The Alternating Color Formula 9.1.6 checkerboard v1 codehs

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

If you add the current row index ( r ) and column index ( c ), the sum alternates between even and odd numbers across the entire grid: Row 0, Col 0 Row 0, Col 1 Row 1, Col 0 Row 1, Col 1 javascript If you add the current row index

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.

rect.setColor(Color.WHITE);

Ensure your loops run from 0 to NUM_ROWS - 1 . Using <= instead of < will often result in the board drawing off the screen.