9.1.7 Checkerboard V2 Codehs
print_board(my_grid)
rl.question("Rows: ", (rows) => rl.question("Cols: ", (cols) => rows = parseInt(rows); cols = parseInt(cols); for (let i = 0; i < rows; i++) let line = ""; for (let j = 0; j < cols; j++) if ((i + j) % 2 === 0) line += "X"; else line += "O";
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
If your squares do not line up perfectly, double-check your SQUARE_SIZE math. Dividing getWidth() by NUM_COLS ensures a perfect fit. 9.1.7 Checkerboard V2 Codehs
// Draw the checkerboard for (var row = 0; row < 8; row++) for (var col = 0; col < 8; col++) // Calculate the top-left corner of this square var x = col * SQUARE_SIZE; var y = row * SQUARE_SIZE;
public void run() double sqWidth = (double) getWidth() / NUM_COLS; double sqHeight = (double) getHeight() / NUM_ROWS;
Here is a comprehensive plan to create the solution: print_board(my_grid) rl
To touch every single square in a 2D array, you need a loop inside a loop. The outer loop moves down the rows, while the inner loop moves across the columns of that specific row. Step 3: Apply the Modulo Check
If your board looks correct but fails the CodeHS autograder, you might have swapped the colors. Switch Color.RED and Color.BLACK in your conditional statement to fix it. Advanced Optimization: The Single Loop Alternative
The 9.1.7 Checkerboard V2 Codehs problem is a fun and challenging coding challenge that requires students to think creatively and use problem-solving skills. By following the steps outlined in this article, you should be able to create a fully functional checkerboard with alternating black and white squares. Whether you're a student or a teacher, we hope this guide has been helpful in unlocking the secrets of the 9.1.7 Checkerboard V2 Codehs problem. Happy coding! Dividing getWidth() by NUM_COLS ensures a perfect fit
# 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
if (row + col) % 2 == 0: pen.color("red") else: pen.color("black")
If the expected output starts with a dark square at (0,0), ensure your if branch matches that. Swap colors if needed.
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.
Master this, and you have unlocked a fundamental pattern used in game development, graphics programming, and algorithm design.