Share your repls and programming experiences

← Back to all posts
🧠 Connect 4 AI
llambda (213)

Just a simple but fast connect four AI made using HTML, CSS, JS.

How To Play:
The aim of the game is to get four of your counters (red) in row, this can be diagonally, horizontally or vertically. Each move click on one of the seven columns to drop your counter into that column.

How It Works:
1) Each potential place the AI can place a counter is scored based on the amount of counters around that place and the length of the segments around it also.
2) The move above is also checked to make sure that by moving to that position that the AI doesn't cause the player to win, or for the AI to sacrifice a potentially good move, if this is true then the score of that move becomes 0.
3) If there is one distinct highest score, that one is chosen, else the AI just picks a random move from the highest scoring moves.

Brandaboss (89)

Best pattern ever.

Brandaboss (89)

I tied it.

Brandaboss (89)

A strategy to always tie is put your first block in the middle and always place your place your next block on the block the AI places. If it hits the top, go to the next row over.

lydiahallie (37)

Amazing, well done!! Super fun to play! I only won 2/5 rounds haha, it's seriously much harder than I expected :')

bearbearmo (200)

Not sure the AI always works:

DaCheeseWhale (0)

I found a consistent win with AI first :D
It doesn't seem to be careful enough for the trap.

JohnnyWobble (49)

Cool game,
but i won

Night_Shadow (2)

Doesn't do what i want it to

llambda (213)

@Night_Shadow What do you want it to do?

NadiaJennings (0)
first try i won

RachelleHu (0) got five in a row

Suparn (2)

I found a way to always win :P
Start at the center column.
Fill in both columns left and right of the center.
Then fill in the rightmost and leftmost column.
You should win every time :p

iamcooliguess (17)

If I click on a piece (mine or opponent's), then it will think I have placed a piece and it makes its move!

DovberEisenberg (0) 2nd to best pattern

spagmeat (1)

Also found an edge case where there is a very obvious placement for the AI, but it goes in the wrong spot. Maybe something to investigate:

llambda (213)

@spagmeat I've fixed the issue, apparently the last move it took was so much "better" than where it should go, that it went there. The problem was that it raised the length of the segments it found to the power of five, which in this case added to bigger than 100 / 101 which indicated in order a red win or a blue win. It was probably a bad idea to do set numbers to indicate a winning move.

llambda (213)

@spagmeat Also your method of winning every time has been fixed.

spagmeat (1)

@LucHutton Bummer! I'll have to find another during my break today. ;)

spagmeat (1)

@LucHutton - I had a break. :)
What's particularly interesting about this one is that I couldn't get the same results when I shift the whole thing by one to the right or when I try to mirror it. I'll take a look at the code later if I have time to try to see whats up. :)

llambda (213)

@spagmeat Turns out I missed something from the previous bug, the new method no longer works. :)

spagmeat (1)

Nicely done. However I found myself able to create this win very consistently.

VicenteLopez (0)

Good job! I couldn´t won a single time.


It was a very good AI, but in the end, humans will win

DovberEisenberg (0)
i just won

ElliottKim (0)

llambda (213)

@kaiserb1 It got 4 in a row diagonally.

themaka (188)

Pretty good AI. Nicely done!

Stanlyhalo (22)
Didn't win?

Stanlyhalo (22)

I got 4 in a row, dianical, but I didn't win?

WiNDfaLLx (33)

^-^ I'm gonna use this strategy on my friends next! corner first :D