/* 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
Ensure both loops run exactly from range(8) to avoid errors when accessing the 8x8 grid. 9.1.7 Checkerboard V2 Codehs
9.1.7 Checkerboard V2 on CodeHS is more than just a drawing exercise. It teaches you – how to break a repetitive visual problem into a compact, logical set of instructions. /* This program draws a full checkerboard on the screen
pen = turtle.Turtle() pen.speed(0) # Fastest drawing speed pen.hideturtle() var cols = getWidth() / SQUARE_SIZE