Share your repls and programming experiences

← Back to all posts
ConnectFour with difficult AI 💻🧠
19wintersp (1131)

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!

Comments
hotnewtop
the_cool_dood (2)

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.

19wintersp (1131)

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

19wintersp (1131)

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

the_cool_dood (2)

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

19wintersp (1131)

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

gwood5901 (22)

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 :/

19wintersp (1131)

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

xxpertHacker (920)

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

Coder100 (17479)

also btw you might want to precompile these things ;)

OH NO I LOSE TO THE AI ON RECURSION 2

19wintersp (1131)

@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

Coder100 (17479)

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

19wintersp (1131)

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

Coder100 (17479)

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

19wintersp (1131)

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

Coder100 (17479)

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 :)

19wintersp (1131)

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

Coder100 (17479)

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

Coder100 (17479)

Nice! But why no makefiles?
:(

19wintersp (1131)

@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

Coder100 (17479)

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