Skip to content
← Back to Community
ConnectFour with difficult AI 💻🧠
Profile icon
19wintersp

19wintersp ConnectFour

Yet another ConnectFour game and AI!

This is a small side project: a fast and powerful ConnectFour game with a difficult-to-beat AI. Written in C++ because speed, it's quite difficult to beat level 7. Can you?

Tree-based C++ AI which can evaluate all possible moves up to nine steps ahead; command-line game UI; full of bugs. You can tweak the settings of the AI yourself by modifying some of the definitions in ~/cfb/app.h without needing to figure out exactly how the spaghetti works.

Play

Edit

Congratulations @the_cool_dood for managing to beat it at level 8!

Voters
Profile icon
CoolCoderSJ
Profile icon
dudeactualdev
Profile icon
LAMAQDAHODWALA
Profile icon
MemeManHimself
Profile icon
xxpertHacker
Profile icon
rediar
Profile icon
DominicHuieWhit
Profile icon
RayhanADev
Profile icon
Coder100
Profile icon
DynamicSquid
Comments
hotnewtop
Profile icon
the_cool_dood

2: easy; 4: average; 6: hard; 8: computer on fire.
What difficulty do you want to play? (recursion depth) 8

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

1 2 3 4 5 6 7

Your turn; what column? 7

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . O . . X

1 2 3 4 5 6 7

Your turn; what column? 4

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . X . . .
. . . O . . X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . X . . .
. . O O . . X

1 2 3 4 5 6 7

Your turn; what column? 2

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . X . . .
. X O O . . X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . X . . .
. X O O . O X

1 2 3 4 5 6 7

Your turn; what column? 5

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . X . . .
. X O O X O X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . O X . . .
. X O O X O X

1 2 3 4 5 6 7

Your turn; what column? 3

. . . . . . .
. . . . . . .
. . . . . . .
. . X . . . .
. . O X . . .
. X O O X O X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . . . . . .
. . . . . . .
. . X . . . .
. . O X O . .
. X O O X O X

1 2 3 4 5 6 7

Your turn; what column? 4

. . . . . . .
. . . . . . .
. . . . . . .
. . X X . . .
. . O X O . .
. X O O X O X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . . . . . .
. . . . . . .
. . X X O . .
. . O X O . .
. X O O X O X

1 2 3 4 5 6 7

Your turn; what column? 2

. . . . . . .
. . . . . . .
. . . . . . .
. . X X O . .
. X O X O . .
. X O O X O X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . . . . . .
. . . . O . .
. . X X O . .
. X O X O . .
. X O O X O X

1 2 3 4 5 6 7

Your turn; what column? 5

. . . . . . .
. . . . X . .
. . . . O . .
. . X X O . .
. X O X O . .
. X O O X O X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . . . X . .
. . . O O . .
. . X X O . .
. X O X O . .
. X O O X O X

1 2 3 4 5 6 7

Your turn; what column? 3

. . . . . . .
. . . . X . .
. . X O O . .
. . X X O . .
. X O X O . .
. X O O X O X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . . . X . .
. . X O O . .
. . X X O . .
. X O X O O .
. X O O X O X

1 2 3 4 5 6 7

Your turn; what column? 3

. . . . . . .
. . X . X . .
. . X O O . .
. . X X O . .
. X O X O O .
. X O O X O X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . X . X . .
. . X O O . .
. . X X O O .
. X O X O O .
. X O O X O X

1 2 3 4 5 6 7

Your turn; what column? 6

. . . . . . .
. . X . X . .
. . X O O X .
. . X X O O .
. X O X O O .
. X O O X O X

1 2 3 4 5 6 7

My turn; thinking...

. . . . . . .
. . X O X . .
. . X O O X .
. . X X O O .
. X O X O O .
. X O O X O X

1 2 3 4 5 6 7

Your turn; what column? 3

. . X . . . .
. . X O X . .
. . X O O X .
. . X X O O .
. X O X O O .
. X O O X O X

1 2 3 4 5 6 7

My turn; thinking...

You won!

Final board:
. . X . . . .
. . X O X . .
. . X O O X .
. . X X O O .
. X O X O O .
. X O O X O X

1 2 3 4 5 6 7

End

beat the bot hardness 8.

Profile icon
19wintersp

@the_cool_dood 👏 Well done! I'm working to add multithreading so that I can add higher recursion levels. I think you bamboozled it with your last two moves. Thanks for playing! :)

Profile icon
19wintersp

@the_cool_dood BTW, if you use three backticks before and after a section of text, it will be put in a monospaced (code) font.

Profile icon
the_cool_dood

@19wintersp could you make an option to make the computer play itself?

Profile icon
19wintersp

@the_cool_dood As in, have two instances of the AI running against each other? Interesting...

It is quite easy actually, I'd just need to run two instances of the bot class. If you fork it, I can tell you how.

Profile icon
AmethystMoon1

I'm the one that made the "why does this still exist post". The reason I'm coming here to post and not replying to your comment is because anytime that i click any link, including "edit" "delete" or "reply" it logs me out and won't let me perform that action. So, repl is very messed up. Anyways, sorry :/

Profile icon
19wintersp

@gwood5901 It's fine, but I think that's probably why you shouldn't do that :)

Profile icon
xxpertHacker

I know a well-written program when I see one, nice job, +1.
(I'll check the spaghetti code later)

Profile icon
Coder100

also btw you might want to precompile these things ;)

OH NO I LOSE TO THE AI ON RECURSION 2

Profile icon
19wintersp

@Coder100 You mean how it compiles each time it gets run? Sorry, that was a temporary thing I left in, I'll fix the .replit file. Whoops

Profile icon
Coder100

@19wintersp yeah, probably don't want to compile it each time it is run considering few things get changed

Profile icon
19wintersp

@Coder100 should be fixed, might need to mess around with chmod though

Profile icon
Coder100

@19wintersp hm, i never had to mess with chmod, usually it just worked

Profile icon
19wintersp

@Coder100 You're right, all working now :) added makefile too, though still not sure whether I've done it right

Profile icon
Coder100

yay!! :) @19wintersp

Profile icon
Coder100

the makefile is actually pretty good, here's a tutorial
click me
@19wintersp
if you made them autogenerate object files, it would get faster because it would only compile what changed :)

Profile icon
19wintersp

@Coder100 thanks for all the help, I'm a culprit for unusable repositories and unreadable code - will take a look at the tutorial :)

Profile icon
Coder100

np :) happy coding C++! @19wintersp

Profile icon
Coder100

btw i used that tutorial, it is worth the 10 hours! (at the cost of pointers) @19wintersp

Profile icon
Coder100

Nice! But why no makefiles?
:(

Profile icon
19wintersp

@Coder100 Hi! I'm not actually too used to C[++], and so any help with that would be appreciated... I think makefiles are "instructions" to compile from source, but I'm not sure... I'll look it up

Profile icon
Coder100

@19wintersp oh wow, that's a good program considering no using namespace std;!