Workshop › Tic-tac-toe › Step 18 of 33
Find three in a row
What we are doing
winningLine(cells) looks through LINES and returns the first line whose three cells hold the same mark, and
that mark is not empty.
```js
function winningLine(cells) {
return LINES.find(([a, b, c]) => cells[a] !== '' && cells[a] === cells[b] && cells[a] === cells[c])
}
```
What it means
LINES.find(test) returns the first line that passes the test, or undefined if none does.
([a, b, c]) => ... names the three numbers of each line a, b and c.
&& means "and": all three conditions must be true.
- The board comes in as a parameter
cells instead of reading board, so we can test any board.
Your turn
Under let player = 'X', leave an empty line and write winningLine. It stays above function play.
Skills: Arrays and objects, Functions
← The eight winning lines Win, draw or go on →
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