News for hackers. Stuff that matters.
Re:Play against the computer – Connect Four game in JavaScript by pebcakID10T on Friday October 21, 2022 @06:11PM (#33293537)
The computer AI code is in this file: http://flydaddy.net/games/connect-four/js/computerOpponent.j...

Basically when it’s the computer’s turn it evaluates every possible move and assigns an attack and defense value for each move. The attack value is based on how many in a row and whether it can win on next turn, and the defense value is how many opponent’s chips in a row and whether it will lose on opponent’s turn. And if 2 or more moves are equal, favor the move closest to the center of the board.

It's fairly difficult, but not perfect, and you can win as the second player.

There are no comments on this comment yet.