Share your repls and programming experiences

← Back to all posts
Connect Four with Smart AI Player
WyattGeorge (33)

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.

-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

NilanP (0)

what AI algorithm does it use

MarkRosenbaum (10)

I still love this game though.

MarkRosenbaum (10)

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

WyattGeorge1 (0)

@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.

UndertaleRulez (33)

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

WyattGeorge (33)

@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!

RaiinWing (4)
I was going to finally win :(

WyattGeorge (33)

@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!

CSharpIsGud (1164)

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

WyattGeorge (33)

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

LinetteManuel (0)

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

vishnunathcr (1)

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.

ChrisHill (19)

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.

WyattGeorge (33)

@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!

endodragon0729 (0)

I lost every round...

WyattGeorge (33)

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

OwenBradstreet (133)

This is awesome dude. Well done!

jarnoreynaert (0)

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

WyattGeorge (33)

@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.

vedprad1 (922)

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


Wow, this is pretty good!