Workshop › Chess › Step 46 of 48
Connect the computer to the game. After a move that gives black the turn, thinking = 20: the game waits 20 frames
(a third of a second) so you see your move land. update counts down on black's turn and then plays
computerMove(). The loop calls update before drawing.
```js
let thinking // frames until the computer moves
thinking = 0
if (legalMoves().length === 0) state = inCheck(turn) ? 'checkmate' : 'stalemate'
else if (turn === 'b') thinking = 20
function update() {
if (state !== 'playing' || turn !== 'b') return
thinking -= 1
if (thinking <= 0) play(computerMove())
}
function loop() {
update()
draw()
```else if runs only when the game is not over: then, if black is to move, start the wait.update does nothing unless the game is on and it is black's turn; each frame thinking goes down by 1, and at 0
the computer's move is played with the same play your clicks use.let state ... write let thinking ...; in reset, under state = 'playing' write thinking = 0.play, under the if (legalMoves()... line write the else if line.function draw() { write update.loop, above draw() write update().Skills: The game loop, Game states
← Try the best-looking moves first You are white →