Codehs Extra Quality | 9.1.7 Checkerboard V2
Next, we need to determine the color of each square. We can do this by using a conditional statement that checks the row and column numbers. If the sum of the row and column numbers is even, we'll draw a black square. Otherwise, we'll draw a white square.
In the exercise, the objective is to create an 8x8 grid of alternating 0s and 1s using nested loops and lists. This task builds on previous iterations by requiring a dynamic approach to row and column indexing. Key Programming Concepts
: In Python, all code within the function must be indented properly. Ensure your
System.out.println(); // new line after each row 9.1.7 Checkerboard V2 Codehs
Top Row (Row 0): (0,0) sum=0 [Even], (0,1) sum=1 [Odd], (0,2) sum=2 [Even]
Here is a detailed review of why this code works and the specific concepts being tested.
public class CheckerboardV2 extends ConsoleProgram public void run() // Define the dimensions of the checkerboard int rows = 8; int cols = 8; // Initialize the 2D array int[][] board = new int[rows][cols]; // Populate the array using nested loops for (int r = 0; r < rows; r++) for (int c = 0; c < cols; c++) // Check if the sum of indices is even if ((r + c) % 2 == 0) board[r][c] = 1; // Primary value else board[r][c] = 0; // Secondary value // Print the final grid to the console printBoard(board); private void printBoard(int[][] grid) for (int r = 0; r < grid.length; r++) for (int c = 0; c < grid[r].length; c++) System.out.print(grid[r][c] + " "); System.out.println(); // Move to the next line after each row Use code with caution. Common Pitfalls and Troubleshooting Next, we need to determine the color of each square
The solution to the CodeHS 9.1.7: Checkerboard V2 exercise requires creating an 8x8 grid represented by a list of lists, where the values alternate between
var GRID_SIZE = 8; var SQUARE_SIZE = getWidth() / GRID_SIZE; var COLOR_ONE = Color.RED; var COLOR_TWO = Color.BLACK; Use code with caution. 2. The Nested Loop Structure
def print_board(board): for row in board: print(" ".join([str(x) for x in row])) print_board(board) Use code with caution. Copied to clipboard ✅ Result The final output will be an Otherwise, we'll draw a white square
The exercise tasks students with drawing a checkerboard pattern (alternating colors) using code, often requiring a array or nested loops to fill a specific area.
If you are comfortable with this, the next logical step is to try generating a 10x10 or even a variable-sized checkerboard!
Expected 4x4 pattern: R B R B B R B R R B R B B R B R
Solving 9.1.7 Checkerboard V2 is less about the act of placing markers and more about algorithmic thinking
