News for hackers. Stuff that matters.
Re:Play against the computer – Connect Four game in JavaScript by pebcakID10T on Thursday October 20, 2022 @06:57PM (#33281917)
Just a fun little project I did. It's beatable, but not easy.
Re:Play against the computer – Connect Four game in JavaScript by POiNTx on Friday October 21, 2022 @05:46PM (#33293378)
How good is the AI and how was it made?
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.

Re:Play against the computer – Connect Four game in JavaScript by duderific on Friday October 21, 2022 @05:50PM (#33293406)
It seems like going first is a disadvantage, since the AI can just play defensively until you are backed into a corner.
Re:Play against the computer – Connect Four game in JavaScript by POiNTx on Friday October 21, 2022 @06:10PM (#33293528)
I think first is an advantage since the opposite side has to react to your moves, a bit like chess.

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.

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.
Re:Play against the computer – Connect Four game in JavaScript by drewnoakes on Friday October 21, 2022 @06:04PM (#33293487)
I've found that the only way to win is to go first, just like in tic tac toe. Going second can produce a draw at best, against an optimal opponent. This is just my experience though, not a strong assertion.
Re:Play against the computer – Connect Four game in JavaScript by pebcakID10T on Friday October 21, 2022 @06:11PM (#33293540)
With perfect play the first player will always win. My AI is not perfect and you can win as the second player.
Re:Play against the computer – Connect Four game in JavaScript by xerofuzzion on Saturday October 22, 2022 @01:20AM (#33295834)
This is not true, I've played twice as the second player and won both. The seem to likes setting up 3 diagonal, during that you are free to setup your own lower trap with two intersecting diagonals. So far doing that always seems to win.