5. Troubleshooting: "You should set some elements of your board to 1"
function start() var size = 8; var squareSize = getWidth() / size; for (var row = 0; row < size; row++) for (var col = 0; col < size; col++) var x = col * squareSize; var y = row * squareSize; var color = ((row + col) % 2 === 0) ? "red" : "black"; // create and add rectangle with x, y, squareSize, color
: Create an empty list called board and fill it with eight rows of eight zeros.
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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 9.1.6 checkerboard v1 codehs
This article breaks down the logic, the math, and the exact code structure needed to solve this exercise efficiently. Understanding the Goal
let board = [];
Combining the steps above, here is the complete, functional code to solve the challenge on CodeHS, according to user discussions on Reddit : 1 0 1 0 1 0 1 0
The objective of this exercise is to create a grid of alternating colored squares to form a classic checkerboard pattern. You will use the tkinter graphics library in Python to draw these shapes on a digital canvas. This assignment focuses on mastering nested loops, grid coordinates, and conditional logic. Core Concepts Explained
# Pass this function a list of lists, and it will # print it such that it looks like the grids in the exercise instructions. def print_board(board): for i in range(len(board)): # This line uses some Python you haven't learned yet. # It turns each cell into a string and joins them with a space. print(" ".join([str(x) for x in board[i]]))
CodeHS expects a specific output format. The print(" ".join(map(str, row))) line is an efficient way to convert each integer 0 or 1 in a row into a string, and then join them together with a space in between. This assignment focuses on mastering nested loops, grid
. Use a loop to populate it with 8 rows, each initially filled with zeros to establish the basic structure. 2. Target Specific Rows for Pieces
// Define constants for better readability private static final int ROWS = 8; private static final int COLUMNS = 8; private static final int SQUARE_SIZE = 50; private static final int WINDOW_WIDTH = COLUMNS * SQUARE_SIZE; // 400 private static final int WINDOW_HEIGHT = ROWS * SQUARE_SIZE; // 400
The outer loop ( row ) tells the program to start at the top and move down. For every single row, the inner loop ( col ) runs across from left to right. This ensures every single coordinate on the grid is visited. 2. The Modulo Operator (%) The line (row + col) % 2 == 0 is the "brain" of the code. % 2 finds the remainder when divided by 2. If the remainder is 0 , the number is even.
The prompt for this exercise typically reads something like this:
This public link is valid for 7 days and shares a thread, including any personal information you added. This link or copies made by others cannot be deleted. If you share with third parties, their policies apply. Can’t copy the link right now. Try again later.