Workshop › Connect Four › Step 32 of 38
Rule 4: most lines of four pass through the middle columns, so they are the most useful. Each choice gets a score, higher closer to the middle, plus a little randomness so games differ, and the best score wins.
```js
// 4. Prefer the middle, where most lines of four pass; a little randomness breaks ties.
let best = choices[0]
let bestScore = -Infinity
for (const col of choices) {
const score = -Math.abs(col - 3) + Math.random() * 0.5
if (score > bestScore) {
best = col
bestScore = score
}
}
return best
```-Math.abs(col - 3) is 0 for the middle column and -1, -2, -3 towards the edges.Math.random() * 0.5 adds up to 0.5, enough to pick between equal columns but never to beat a column nearer the middle.-Infinity, below every number.In computerMove, replace return choices[0] with the scoring lines.
Skills: Game states
← Don't help the player The computer's turn →