Skip to content
Sign upLog in
← Back to Community
Connect Four with Smart AI Player
Profile icon
WyattGeorge

Try to beat the AI! This is a version of Connect Four with a smart AI player that makes intelligent moves.

The AI evaluates each possible move and picks what it thinks is the best one, resulting in a hard (but not impossible!) to beat adversary.

https://repl.it/@WyattGeorge/ConnectFour

-Update: Changed characters to be clearer, console is now cleared after each move
-Update: Fixed a bug that prevented the AI from picking a column if it was guaranteed to lose

Voters
Profile icon
MollyLangdon
Profile icon
oocon3
Profile icon
whsbdavid
Profile icon
WyattGeorge
Profile icon
CollinWebb
Profile icon
CircuitSacul
Profile icon
MaliaJones
Profile icon
MarkRosenbaum
Profile icon
ChristianThone
Profile icon
Zwxwvut
Comments
hotnewtop
Profile icon
vishnunathcr

nice work.
I think 0 and O are little confusing, X and O would have been better
And a clear console before each print would also be nice.

Profile icon
ChrisHill

I agree with

@vishnunathcr
it might be because I played this on an android screen but it's next to impossible to see the difference in shape. O and o would even be better.

Profile icon
WyattGeorge

@vishnunathcr
yeah I agree about the clear console. I guess I didn’t think about the 0s and Os because they look different in the browser, but that’s an easy fix. Thanks for the feedback!

Profile icon
vishnunathcr
Profile icon
NilanP

what AI algorithm does it use

Profile icon
Michael8910

can you add color

Profile icon
MarkRosenbaum

I still love this game though.

Profile icon
MarkRosenbaum

I beat this AI over and over. I have not lost once to it.
try to make it harder.

Profile icon
WyattGeorge1

@markerosenbaum
Thanks for the comment. To be honest though, Connect Four is a solved game, so I could make the AI win every time if I really wanted to...this is supposed to be a tough but bearable opponent.

Profile icon
AmeliaBlackwell

woo i won! great game

Profile icon
UndertaleRulez

i really liked it there is one little thing that i didn't like tho, at one point in one of the rounds it said over the game board " there is no escape now" i would have taken a screen shoot if i knew how. please reply as soon as you can and explain to me if you meant to do that or not

Profile icon
WyattGeorge

@UndertaleRulez
Yeah that was just a little something I added to indicate that the AI is pretty confident (>80%) that it is going to win. I have noticed that it is sometimes wrong though (and there's no real need for it), so I may change that in the future. Thanks for the feedback!

Profile icon
RaiinWing

This file cannot be displayed: https://storage.googleapis.com/replit/images/1530987184467_14c573473cec68582d925f1d80a2ce97.pn
I was going to finally win :(

Profile icon
WyattGeorge

@RaiinWing
Ah thanks for finding that. Sorry you didn't get your win! I think I've fixed it, but I don't have the time to really test it right now. If anybody else gets something like that, please let me know!

Profile icon
CSharpIsGud

How are you supposed to win, the AI plays perfectly with the power of machine comparison

Profile icon
WyattGeorge

@CSharpIsGud
It is definitely possible to win, but the AI is indeed difficult. It will occasionally make mistakes though, so keep trying!

Profile icon
LinetteManuel

Really great job!! I have won only once and the code is amazing!

Profile icon
endodragon0729

I lost every round...

Profile icon
WyattGeorge

@endodragon0729
haha yeah I think I've only won a couple times. The AI is tough for sure

Profile icon
OwenBradstreet

This is awesome dude. Well done!

Profile icon
jarnoreynaert

How long did it take you to make this if I may ask?

Profile icon
WyattGeorge

@jarnoreynaert
A few days with a couple hours a day. Maybe 10-15 hrs total. Most of the time was spent debugging stuff but I already had the framework in my head. I've also made games like tic-tac-toe with a similar AI before, which helped a lot.

Profile icon
jarnoreynaert

Awesome!

Profile icon
vedprad1

This is fantastic! I like the fact that the AI is legit.

Profile icon
B_W_C_TESTER

Wow, this is pretty good!