Workshop › Battleship › Step 22 of 35
Now the computer plays: when its timer runs out, it picks a random square on your sea it has not shot yet, fires, and gives the turn back.
```js
function computerShoots() {
let r
let c
do {
r = Math.floor(Math.random() * N)
c = Math.floor(Math.random() * N)
} while (theirShots[r][c])
const result = fire(myFleet, theirShots, r, c)
message = result === 'sunk' ? 'They sank your ship!' : 'Your turn: pick a square'
turn = 'you'
}
function update() {
if (state === 'playing' && turn === 'them' && --timer === 0) computerShoots()
}
update()
```do ... while keeps picking a random square until it finds one not shot yet.fire function you use, on your fleet and its own record.--timer subtracts 1 first and then compares: after 30 frames it reaches 0 and the computer shoots.playerShoots, write computerShoots and update.loop, call update() before draw().Skills: The game loop, Loops
← Take turns You can lose too →