5 ms·
Play against the computer – Connect Four game in JavaScript
- pebcakID10T 4y agoJust a fun little project I did. It's beatable, but not easy.
- POiNTx 4y agoHow good is the AI and how was it made?
- pebcakID10T 4y agoThe computer AI code is in this file: http://flydaddy.net/games/connect-four/js/computerOpponent.js 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.
- deleted 4y ago[deleted]
- duderific 4y agoIt seems like going first is a disadvantage, since the AI can just play defensively until you are backed into a corner.
- drewnoakes 4y agoI'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.
- pebcakID10T 4y agoWith perfect play the first player will always win. My AI is not perfect and you can win as the second player.
- xerofuzzion 4y agoThis 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.
- POiNTx 4y agoI 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.
- jbluepolarbear 4y agoThe 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.
- gus_massa 4y agoIt would be nice to be able to click in any square in the board, instead of just the ones in the bottom.
- pebcakID10T 4y agoGood idea. Done.
- TedDoesntTalk 4y agoI've thought about winning algorithms for this game before. The best I could come up with is roughly "get the highest locations you can while still defending against 3-in-a-row".
- pebcakID10T 4y agoThe computer AI code is in this file: http://flydaddy.net/games/connect-four/js/computerOpponent.js 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.
- Pxtl 4y agoRan into bug - AI beat me by creating a diagonal with the piece in the top rank and it did not count that as endgame. I was able to keep playing.
- pebcakID10T 4y agoYes, I need a bug report button.
- chairfield 4y agoSame, it only registered my victory on the second connect-4.
- pebcakID10T 4y agoShould be fixed now - an off by one error checking diagonals that hit the top of the board. Thanks!
- i13e 4y agoICYI Connect Four is a solved game, you can read about it here: https://en.wikipedia.org/wiki/Connect_Four#Mathematical_solution https://en.wikipedia.org/wiki/Connect_Four#Mathematical_solu...
- pebcakID10T 4y agoYes, I may do a minimax version with a difficulty setting. That would be fun to do.
- Fatnino 4y agoIf you tell the optimal algorithm to only think 3 moves ahead, it's too easy, and if you let it think 4 ahead it becomes very difficult (but not quite impossible).
- tromp 4y agoYou can solve it yourself from scratch in a few minutes by running my Fhourstones benchmark [1]. Other interesting connect-4 tidbits may be found on my connect-4 page [2]. [1] https://tromp.github.io/c4/fhour.html https://tromp.github.io/c4/fhour.html [2] https://tromp.github.io/c4/c4.html https://tromp.github.io/c4/c4.html
- isquaredr 4y agoThanks for sharing! This sent me down the solvability path. It would be cool to have an ‘expert’ mode that plays the optimal strategy.
- ReaLNero 4y agoYour wish has been granted: https://connect4.gamesolver.org/ https://connect4.gamesolver.org/
- pipeline_peak 4y agoThe best trick I can think of is to reach a point where you can get connect four in two or more places and the bot can only cancel out one.