// 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++)
: Colors must alternate both horizontally and vertically.
/* This program draws a checkerboard pattern using circles. * The board is 8x8. */ function start() // Calculate the radius based on canvas width (400) and 8 columns // Width / 8 = 50 per cell. Radius is 25. var radius = getWidth() / 16; // Outer loop for rows for (var row = 0; row < 8; row++) // Inner loop for columns for (var col = 0; col < 8; col++) // Calculate x and y positions var x = radius + (col * radius * 2); var y = radius + (row * radius * 2); // Create the circle var circle = new Circle(radius); circle.setPosition(x, y); // Alternate colors: // If (row + col) is even, color it gray. // If (row + col) is odd, color it red. if ((row + col) % 2 == 0) circle.setColor(Color.gray); else circle.setColor(Color.red); add(circle); Use code with caution. Copied to clipboard 🛠️ Key Concepts to Remember
: Inside the loops, use an if-else statement or a simple calculation to assign the value based on the parity of the sum of the indices. 9.1.6 checkerboard v1 codehs
Used to detect if a sum of indices is even or odd ( ), which helps determine where to place a '1'.
The checkerboard has 8×8 squares, but you might accidentally loop 0 to 7 (correct) or 1 to 8 (incorrect). Fix: Always start your loop at 0 and use < ROWS and < COLUMNS .
The "9.1.6 Checkerboard, v1" exercise, found in CodeHS courses like "Georgia Foundations of Artificial Intelligence" and "WCSD Python II", is the first step in a three-part project. The ultimate goal is to build a program that stores numbers representing checkers pieces on a board. In this initial version, you are tasked with creating the basic board structure in the form of a . // Loop through each row for (int row
What or dimensions does your specific assignment prompt require? Share public link
Ensure you are actually changing board[i][j] = 1 rather than just printing, or you won't pass the autograder.
public class Checkerboard extends ConsoleProgram public void run() // 1. Create a 2D array of size 8x8 int[][] board = new int[8][8]; // 2. Nest loops to traverse rows and columns for (int row = 0; row < board.length; row++) for (int col = 0; col < board[0].length; col++) // 3. Logic: If (row + col) is even, it's a 0. If odd, it's a 1. if ((row + col) % 2 == 0) board[row][col] = 0; else board[row][col] = 1; // 4. Print the result using the provided grid printer printBoard(board); // Helper method to print the 2D array public void printBoard(int[][] board) for(int[] row : board) for(int el : row) System.out.print(el + " "); System.out.println(); Use code with caution. Copied to clipboard 1. Initialize the 2D Array */ function start() // Calculate the radius based
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
After you've built a full row of 8 numbers, you append that row (as a list) to your main board list.
Here is the solution code:
. The checkerboard pattern relies on the sum of the row index ( ) and column index ( is even, you place a ; if it is odd, you leave it as if (i + j) % 2 == 0: board[i][j] = 1 else: board[i][j] = 0 Use code with caution. Step 4: Printing the Board