Workshop › Connect Four › Step 31 of 38
Rule 3: never play right under a hole where the player would win, because your disc would give them the step they need. For each open column, imagine the computer's disc there and ask whether the player could then win on top of it.
```js
// 3. Do not play right under a square where the player would win.
const safe = open.filter((col) => {
const row = dropRow(col)
if (row === 0) return true
board[row][col] = 2
const danger = winsWith(col, 1)
board[row][col] = 0
return !danger
})
const choices = safe.length > 0 ? safe : open
return choices[0]
```winsWith(col, 1) for the hole above it, take the disc back.row === 0) has nothing above it: safe.choices is the safe columns, or all open ones if none is safe.In computerMove, replace return open[0] with the new lines.
Skills: Game states
← Win, or block Prefer the middle →