9.1.6 Checkerboard V1 Codehs 'link' Info
public class Checkerboard extends ConsoleProgram public static void main(String[] args) int size = 8; int[][] board = new int[size][size]; for (int row = 0; row < size; row++) for (int col = 0; col < size; col++) // Check if the sum of row and col is even if ((row + col) % 2 == 0) board[row][col] = 1; // "Color A" else board[row][col] = 0; // "Color B" // Helper method to print the board printBoard(board); private static void printBoard(int[][] board) for (int[] row : board) for (int val : row) System.out.print(val + " "); System.out.println(); Use code with caution. Copied to clipboard Visual Representation Key Concepts to Remember : int[][] board = new int[rows][cols];
The outer loop handles rows, while the inner loop handles individual columns.
The board is represented with 1's and 0's:
remain all zeros, as these represent the empty "no-man's land" in the middle of a checkers game.
The prompt for this exercise typically reads something like this: 9.1.6 checkerboard v1 codehs
// 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
board = []
Instead of manually placing 64 squares, use a nested loop where an outer loop tracks the and an inner loop tracks the columns .
CodeHS exercise 9.1.6 (v1) requires creating an 8x8 2D list and using nested loops with assignment statements to place pieces (1s) in the top three (rows 0-2) and bottom three (rows 5-7) rows. The solution involves initializing a grid of zeros, applying conditional logic to update specific elements, and printing the formatted grid. For a detailed breakdown of the solution, refer to the discussion on Reddit [Link: Reddit user thread https://www.reddit.com/r/codehs/comments/kt28qe/916_checkerboard_v1_answers_needed_what_am_i/]. CodeHS exercise 9
. This specific version focuses on the foundational step of creating a 2D structure where values represent different game states: for a checker piece and for an empty square. The Assignment Objective The goal is to create an
. 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
Complexity constraints:
Example (pseudocode):
Using the parity rule (r + c) % 2 determines cell contents and yields a simple, provably correct O(n^2) algorithm. The provided textual and graphical templates adapt to typical CodeHS environments. If you supply the exact CodeHS problem text or target language/API (e.g., Java, JavaScript, CodeHS turtle), I will produce a tailored solution and classroom-ready explanation matching that context.
for row in board: # Convert numbers to strings and join with spaces print(" ".join(map(str, row))) Use code with caution. Copied to clipboard Key Takeaways for Your Post
By checking if (r + c) is divisible by 2 using the modulo operator ( % ), you can cleanly alternate colors. Step-by-Step Code Implementation
Using helper functions (CodeHS libraries): you can cleanly alternate colors.