Workshop › Tic-tac-toe › Step 5 of 33
Finish the grid
What we are doing
The horizontal lines follow the same pattern, with x and y (and width and height) swapped. One more line in the
loop finishes the 3×3 grid.
```js
for (let i = 1; i < 3; i++) {
ctx.fillRect(i * CELL - 2, 0, 4, canvas.height)
ctx.fillRect(0, i * CELL - 2, canvas.width, 4)
}
```
What it means
- The new line starts at the left edge (
x = 0), at y = i * CELL - 2, is as wide as the board and 4 pixels tall.
- Each round of the loop now draws one vertical and one horizontal line: four lines in total.
Your turn
Inside the loop, under the vertical fillRect, add the horizontal one. Press Run.
Skills: Drawing on a canvas, Loops
← Repeat with a loop The board as a list →
All steps
- Get the canvas and the pen
- Paint the board
- The first grid line
- Repeat with a loop
- Finish the grid
- The board as a list
- A function that draws
- Write an X
- From cell number to position
- Draw what the board holds
- A color for each mark
- Make a move
- No playing on a taken cell
- Listen for clicks
- Which cell was clicked?
- Clicks on any screen size
- The eight winning lines
- Find three in a row
- Win, draw or go on
- The round ends
- Show who won
- Light up the winning line
- A function for a fresh round
- Click to play again
- A computer that picks a cell
- The computer answers
- Rule one: take the center
- Rule two: win when you can
- Rule three: block the opponent
- Score a finished game
- Look ahead: minimax
- A computer that never loses
- Build it yourself: keep score