Re:Play against the computer – Connect Four game in JavaScript
by jbluepolarbear on Friday October 21, 2022 @06:40PM
(#33293747)
The fastest I could beat this AI was going first and playing 7,4,4,3,6,7,5. The computer always plays the same moves and has to block a connect, but gives up a connect.
A strategy that works for me is building a tower. In that tower try to construct 2 rows of 3 length, can be one horizontal and one diagonal but doesn't have to be, that when you go to one side of the tower, it would give you 4 in a row 2 times right after another circle is placed on top of the first one. While doing this also making sure the opponent isn't doing the same below your rows.
Doing this makes it impossible for the other to defend as when you're building on the side of the tower, it gives the opponent 2 options:
- Defending the first row and placing a circle, allowing you to place another on that circle and making the second 4 in a row.
- Or not defending and you can obviously finish your first in a row.
Following this strategy I was able to win both as player 1 and 2.