Reflection
For this assignment, I created a checkers board using loops and conditionals in p5.js. The program generates an 8×8 grid with alternating black and white squares, and places red and blue checkers pieces on the appropriate black squares using conditionals. The checkers pieces are drawn as circles and are positioned in the top three (red) and bottom three (blue) rows. One of the most interesting parts of the code is how (i + j) % 2 !== 0 ensures that pieces only appear on black squares, while additional conditionals determine their placement. In the future, I could improve this by adding interactivity to allow piece movement, highlighting valid moves, implementing kinged pieces with visual markers, and enabling player turns to make it a fully playable checkers game.
Code
function setup() { createCanvas(400, 400); } function draw() { background(30); let cols = 8; let rows = 8; let squareSize = width / cols; for (let i = 0; i < cols; i++) { for (let j = 0; j < rows; j++) { let x = i * squareSize; let y = j * squareSize; // Alternating black and white squares if ((i + j) % 2 === 0) { fill(240); } else { fill(20); } rect(x, y, squareSize, squareSize); // Placing checkers pieces only on black squares in the right rows if ((i + j) % 2 !== 0) { if (j < 3) { fill(200, 50, 50); ellipse(x + squareSize / 2, y + squareSize / 2, squareSize * 0.8); } else if (j > 4) { fill(50, 50, 200); ellipse(x + squareSize / 2, y + squareSize / 2, squareSize * 0.8); } } } } }