V2 Codehs !!hot!! — 9.1.7 Checkerboard
In the ancient city of Quadra, there lived a master tile-setter named . He was commissioned by the Queen to floor the Grand Hall with a perfect
Start by defining the size of the board and the squares. Using constants makes your code easy to modify later. javascript
You need two loops: an outer loop for rows and an inner loop for columns.
The 9.1.7 Checkerboard V2 assignment in CodeHS challenges you to move beyond simple loops and use nested structures to create a complex grid pattern. To solve this, you need to understand how row and column indices determine the color of each square. The Goal of Checkerboard V2 9.1.7 Checkerboard V2 Codehs
Depending on your specific language track in CodeHS (typically Java or JavaScript), you will implement nested for loops to traverse the matrix. 1. Initialize the Loops
: In Python, all code within the function must be indented properly. Ensure your
). If your code just prints the pattern without actually building the list structure, it may fail even if the output looks correct. Indentation In the ancient city of Quadra, there lived
The "9.1.7 Checkerboard, v2" challenge is an important building block. By understanding how to construct and display a 2D list using loops and conditional logic, you are gaining skills that are essential for more advanced programming, such as building games like Tic Tac Toe, Grid, and other applications that rely on a 2D coordinate system.
Remember that the x coordinate is determined by the column , while the y coordinate is determined by the row .
You will need one loop for the and another inside it for the columns . javascript javascript You need two loops: an outer loop
In the exercise on CodeHS, you create a checkerboard pattern by utilizing a for loop to iterate through a 2D array (grid) and assigning colors based on whether the sum of the row and column indices is even or odd. 1. Initialize the grid
Ensure your loops start at 0 and end at GRID_SIZE - 1 .
Ensure both loops run exactly from range(8) to avoid errors when accessing the 8x8 grid.
In some versions, V2 also requires that you use a to track color state, rather than just checking (row + col) % 2 .
# Assuming a standard CodeHS Python graphics setup import math # --- Constants --- SQUARE_SIZE = 40 ROWS = 8 COLS = 8 COLOR_1 = Color.red COLOR_2 = Color.black def draw_checkerboard(): for row in range(ROWS): for col in range(COLS): # 1. Calculate x, y positions x = col * SQUARE_SIZE y = row * SQUARE_SIZE # 2. Determine color using (row + col) if (row + col) % 2 == 0: rect_color = COLOR_1 else: rect_color = COLOR_2 # 3. Draw the rectangle rect = Rectangle(SQUARE_SIZE, SQUARE_SIZE) rect.set_position(x, y) rect.set_color(rect_color) add(rect) draw_checkerboard() Use code with caution. Common Mistakes and How to Avoid Them