9.1.7 Checkerboard V2 Codehs !full! May 2026
To solve the CodeHS 9.1.7 Checkerboard V2 exercise, you need to create an 8x8 grid (a 2D list) and fill it with alternating 0s and 1s to form a checkerboard pattern.
The core of this challenge lies in understanding how to access specific elements in a list of lists and applying a mathematical condition to alternate values. The Core Logic: The Modulo Operator
To create a checkerboard, we use the row and column indices. If the sum of the row index and column index is even, we assign one value (e.g., 0); if it is odd, we assign the other (e.g., 1). This is easily checked using the modulo operator (%): if (row + col) % 2 == 0: (Sum is even) else: (Sum is odd) Step-by-Step Implementation
Initialize the Grid: Create an empty list and use a loop to append 8 sub-lists, each containing eight zeros.
Nested For Loops: Use one loop to iterate through each row (0-7) and a nested loop to iterate through each column (0-7).
Assignment Statement: Inside the nested loop, use the (row + col) % 2 logic to assign 1 to the correct positions using the syntax grid[row][col] = 1.
Printing the Board: Call the provided print_board function to display your final 2D list. Solution Code
def print_board(board): for i in range(len(board)): # Joins the list elements into a single string for printing print(" ".join([str(x) for x in board[i]])) # 1. Initialize an 8x8 grid filled with 0s my_grid = [] for i in range(8): my_grid.append([0] * 8) # 2. Use nested loops to assign 1s in a checkerboard pattern for row in range(8): for col in range(8): # 3. Check if the sum of indices is odd or even if (row + col) % 2 != 0: my_grid[row][col] = 1 # 4. Print the final result print_board(my_grid) Use code with caution. Common Pitfalls
Indentation Errors: Python relies on proper indentation to know which code belongs inside a loop or function.
Assignment vs. Printing: The autograder often checks if you actually changed the values in the list using my_grid[row][col] = 1. Simply printing a pattern without updating the list will likely cause the test to fail.
Index Out of Range: Ensure both loops run exactly from range(8) to avoid errors when accessing the 8x8 grid.
Building the 9.1.7 Checkerboard V2 program in CodeHS is all about mastering nested loops and using the modulo operator (%) to alternate colors efficiently.
In this version, we move beyond hard-coding rows to creating a dynamic grid that fills the entire canvas with a checkerboard pattern. The Logic Breakdown
To get this right, you need to think in terms of rows and columns:
Nested Loops: Use an outer loop to move down the rows and an inner loop to place squares across the columns.
Alternating Colors: Instead of checking just the column index, we look at the sum of the row and col indices. If (row + col) % 2 == 0, color the square red. Otherwise, color it black.
Positioning: Each square's x position is col * SQUARE_SIZE and its y position is row * SQUARE_SIZE. The Code Solution (JavaScript/karel) javascript 9.1.7 Checkerboard V2 Codehs
/* This program draws a full checkerboard on the screen. * It uses a constant for square size to make it dynamic. */ var SQUARE_SIZE = 40; function start() // Calculate how many rows and columns fit on the screen var rows = getHeight() / SQUARE_SIZE; var cols = getWidth() / SQUARE_SIZE; for(var r = 0; r < rows; r++) for(var c = 0; c < cols; c++) drawSquare(r, c); function drawSquare(row, col) var x = col * SQUARE_SIZE; var y = row * SQUARE_SIZE; var rect = new Rectangle(SQUARE_SIZE, SQUARE_SIZE); rect.setPosition(x, y); // The magic logic: if the sum of row and col is even, it's red if((row + col) % 2 == 0) rect.setColor(Color.red); else rect.setColor(Color.black); add(rect); Use code with caution. Copied to clipboard Pro-Tips for Success
Constant Usage: Always use SQUARE_SIZE instead of typing 40 everywhere. This makes it easy to change the board's density later.
Screen Bounds: Using getWidth() and getHeight() ensures your checkerboard fills the entire canvas regardless of the window size.
Modulo is King: The (r + c) % 2 trick is the industry standard for creating grid patterns—it’s much cleaner than using multiple if/else statements for odd/even rows.
Need help debugging a specific error in your grid? Let me know what your console is saying!
Common Mistakes to Avoid
- Forgetting the Modulo Operator (
%): Students often try to use complicated boolean flags (likeis_red = True) and flip them back and forth. While possible, it is prone to errors. Using(row + col) % 2is the cleanest, "professional" way to solve grid patterns. - Overlap: If you don't move the turtle correctly between squares, you might draw squares on top of each other. Ensure your movement distance (
forward(square_size)) matches the size of the square you just drew. - Grid Alignment: A common error is that the next row starts where the previous one ended (on the far right). You must explicitly move the turtle back to the starting X position (using
backwardorgoto) before starting the next row.
In CodeHS 9.1.7: Checkerboard V2, the goal is to create a pattern of alternating 1s and 0s in a 2D list (grid). Unlike version 1, which often uses simple row filling, version 2 requires you to use nested for loops and the modulus operator ( ) to check for even and odd positions. Logic for the Pattern
To create a checkerboard, a cell should be a 1 if the sum of its row and column indices is even (or odd, depending on your starting preference). Even sum : Assign 1. Odd sum : Assign 0. Step-by-Step Implementation
Initialize the BoardCreate an empty list and fill it with eight sub-lists, each containing eight zeros. board = [] for i in range(8): board.append([0] * 8) Use code with caution. Copied to clipboard
Iterate and Assign ValuesUse nested loops to traverse every row ( ) and column (
). Use an if statement with the modulus operator to decide where to place a 1.
for i in range(8): for j in range(8): if (i + j) % 2 == 0: board[i][j] = 1 Use code with caution. Copied to clipboard
Print the BoardDefine or use a function to print each row of the list so it looks like a grid.
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
grid where 1s and 0s alternate perfectly in every direction.
1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 Use code with caution. Copied to clipboard
This essay explores the logic and implementation of the Checkerboard V2 challenge in the CodeHS 9.1.7 curriculum To solve the CodeHS 9
. This exercise is a pivotal moment for students learning Java or JavaScript (Karel), as it transitions from simple movement to complex nested loops and conditional logic. The Objective
The goal of Checkerboard V2 is to create a grid-like pattern of "markers" or "beepers" on a canvas of any size. Unlike the first version, V2 often requires the program to be dynamic—meaning it must work whether the grid is
. The pattern mimics a standard chessboard, where no two markers are adjacent horizontally or vertically. Core Logic: The Nested Loop The foundation of the solution is the nested loop
. To fill a 2D space, the program must iterate through rows and columns. Outer Loop:
Manages the vertical movement (moving from one row to the next). Inner Loop:
Manages the horizontal movement (placing beepers across a single row).
The challenge arises in the alternating nature of the rows. If every row started with a beeper, you would end up with vertical stripes rather than a checkerboard. The Parity Strategy The most elegant way to solve Checkerboard V2 is by using
(even vs. odd). By tracking the current row number and column number, the program can decide whether to place a marker based on the following rule: If the sum of the (Row + Column) is , place a beeper. If the sum is , leave the space empty.
This mathematical approach ensures the pattern remains consistent regardless of the grid’s dimensions. Execution and "The Turnaround"
In many Karel-based versions of this task, the difficulty lies in the "turnaround." Once Karel reaches the end of a row, the program must determine if there is another row above it. If so, Karel must turn, move up, and position itself to face the opposite direction to begin the next line. This requires careful use of
statements to check for walls and prevent the program from crashing at the edges of the grid. Conclusion
Solving 9.1.7 Checkerboard V2 is less about the act of placing markers and more about algorithmic thinking
. It teaches students how to use coordinates to control logic and how to write code that is flexible enough to handle varying input sizes. Mastering this exercise signals a transition from a beginner coder to one who understands the structural beauty of computer science. loops or the if/else statements needed for this?
The 9.1.7 Checkerboard V2 exercise on CodeHS involves creating an
grid of alternating values (typically 0 and 1) to represent a checkerboard pattern.
Here is a story that illustrates the logic behind this coding task through a real-world analogy. The Story: The Grand Tile-Setter's Strategy Common Mistakes to Avoid
In the ancient city of Quadra, there lived a master tile-setter named Modulo. He was commissioned by the Queen to floor the Grand Hall with a perfect
checkerboard of obsidian and pearl tiles. However, Modulo was notoriously lazy and wanted a single set of instructions his apprentices could follow without him being there. 1. Designing the First Row
Modulo told his first apprentice, "Start with obsidian (0), then pearl (1). Repeat this four times."The apprentice laid out: [0, 1, 0, 1, 0, 1, 0, 1]. 2. Planning the Alternation
For the next row, Modulo knew it couldn't be the same, or the colors would touch. He told the second apprentice, "Start with pearl (1) instead, then obsidian (0). Repeat that four times."The second apprentice laid out: [1, 0, 1, 0, 1, 0, 1, 0]. 3. Automating the Entire Floor
To finish the whole hall, Modulo realized he just needed to alternate between the "Obsidian-Start" row and the "Pearl-Start" row for a total of 8 rows. He wrote down a rule using the row number ( ) and the tile number ( "If you add the row number and the tile number ( ) and the result is even, place a Pearl (1)." "If the result is odd, place an Obsidian (0).".
By following this simple math, the apprentices completed the floor perfectly, ensuring no two tiles of the same color ever touched vertically or horizontally. The "Logic" Behind the Story
To translate this into your CodeHS assignment, the core logic relies on using nested loops and the modulo operator (%) to determine the color of each "tile" in your 2D list:
✅ The Resulting PatternThe code generates a list of lists where each inner list represents a row, alternating like this: Row 0 (Even): [1, 0, 1, 0, 1, 0, 1, 0] Row 1 (Odd): [0, 1, 0, 1, 0, 1, 0, 1] ...and so on.
9.1.7 Checkerboard, v2 I got this wrong, and I can't ... - Brainly
Step-by-Step Solution
- Set up the canvas: Create an HTML canvas element and get a reference to it in JavaScript.
- Define the square size and grid dimensions: Determine the size of each square and the number of rows and columns for the checkerboard.
- Create a function to draw a square: Write a function that takes the position (x, y) and color of a square as arguments and uses the canvas context to draw the square.
- Use nested loops to draw the checkerboard: Iterate over each row and column of the grid, calculating the position of each square and determining its color based on the row and column indices.
Mastering the Checkerboard: A Deep Dive into CodeHS 9.1.7 Checkerboard V2
If you are navigating the CodeHS Java (or JavaScript) curriculum, particularly in the "Advanced Arrays" or "Graphics" sections, you have likely encountered Exercise 9.1.7: Checkerboard V2.
At first glance, it seems simple: draw a checkerboard. However, this problem is a classic exercise in nested loops, conditional logic, coordinate math, and efficient rendering. It strips away the fluff of game logic and focuses on the core visual structure of an 8x8 grid.
This article will break down the problem, explore the common pitfalls, provide step-by-step solutions in both Java (Console/Graphics) and JavaScript (Web Graphics), and explain the underlying principles so you can truly master the concept.
Testing Your Solution
Before submitting, test these cases manually:
| Test Case | Expected Behavior | |-----------|------------------| | 1x1 board | Single square | | 1x5 board | Horizontal alternating pattern | | 5x1 board | Vertical alternating pattern | | 2x2 board | Top-left = dark, top-right = light, bottom-left = light, bottom-right = dark | | 10x10 board | Perfect checkerboard, no line breaks inside |
3. Drawing Mechanics
begin_fill()andend_fill(): These are required to actually color the inside of the square. Without them, the turtle would only draw the outline.- Positioning:
pen.forward(square_size)moves the turtle to the right after drawing a square, preparing for the next one in the row.- The block of code after the inner loop (
pen.backward...) handles the "Carriage Return." It moves the turtle all the way back to the left edge and shifts it down one row so the next row can be drawn.
Loop through the rows (height)
for i in range(height): # Loop through the columns (width) for j in range(width):
# The Logic: Check if the sum of row index and column index is even or odd
# This creates the alternating pattern on both axes.
if (i + j) % 2 == 0:
print("*", end=" ") # Print a star and a space
else:
print(" ", end=" ") # Print a space and a space
# Print a new line after every row is finished
print()
Approach 2: Graphical Checkerboard (CodeHS Graphics Library)
The graphical version is where many students struggle. CodeHS often uses its proprietary GraphicsProgram class (similar to ACM Java libraries) or Turtle graphics.