← Back to Community
🧠 Connect 4 AI
Profile icon
llambda

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.
Voters
Profile icon
GSwope
Profile icon
SasinduMissaka
Profile icon
CHRISTOPHERTIPP
Profile icon
aaku
Profile icon
TristanMisja
Profile icon
sRaynanCravey
Profile icon
coder787878
Profile icon
ash15khng
Profile icon
ZHIWENPING
Profile icon
FARZANA_ALAALA
Comments
hotnewtop
Profile icon
Brandaboss

Best pattern ever.
This file cannot be displayed: https://storage.googleapis.com/replit/images/1548479491481_e6bf7438d403a1c226a0d139afbfdada.pn

Profile icon
MarceloCarpenter

Wow, that was hard

Profile icon
Brandaboss

I tied it.
This file cannot be displayed: https://storage.googleapis.com/replit/images/1548479406428_49c1913f2dc2b9d71770c78b0548503e.pn

Profile icon
Brandaboss

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.

Profile icon
lydiahallie

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

Profile icon
bearbearmo

Not sure the AI always works:

This file cannot be displayed: https://storage.googleapis.com/replit/images/1548164671314_141d5e7b9476e58b3527d82f4b06350f.pn

Profile icon
llambda
Profile icon
DaCheeseWhale

image

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

Profile icon
JohnnyWobble

Cool game,

This file cannot be displayed: https://storage.googleapis.com/replit/images/1551728210698_b0d7c78147aecc589335e9ae75902002.pn
but i won

Profile icon
UnknownZettai

Doesn't do what i want it to

Profile icon
llambda

@Night_Shadow What do you want it to do?

Profile icon
NadiaJennings

This file cannot be displayed: https://storage.googleapis.com/replit/images/1551384363587_8c3068172f6b19be781014696e8f4833.pn
first try i won

Profile icon
RachelleHu

This file cannot be displayed: https://storage.googleapis.com/replit/images/1551299657375_811e2ec3db84333ae5c4367aed76da6e.pn...I got five in a row

Profile icon
Suparn

I found a way to always win :P
Steps:
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
This file cannot be displayed: https://storage.googleapis.com/replit/images/1551145368776_bf73202506952ca5666c766e8abf33aa.pn
This file cannot be displayed: https://storage.googleapis.com/replit/images/1551145372206_a7855b5b739f56ee9971c1cb809ee64f.pn

Profile icon
iamcooliguess

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

Profile icon
llambda
Profile icon
ObadahMuhammad

Quite challenging.

Screenshot_1

Profile icon
DovberEisenberg

This file cannot be displayed: https://storage.googleapis.com/replit/images/1550702882511_b28351134c39053d477c306c0f24f956.pn 2nd to best pattern

Profile icon
spagmeat

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:

This file cannot be displayed: https://storage.googleapis.com/replit/images/1550686398701_e5ecd5575f32214a857b02a1a0d3e574.pn

Profile icon
llambda

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

Profile icon
llambda

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

Profile icon
spagmeat

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

Profile icon
spagmeat

@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. :)
This file cannot be displayed: https://storage.googleapis.com/replit/images/1550771457037_39b806deb192654b9c09b506fd41444d.pn

Profile icon
llambda

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

Profile icon
spagmeat

Nicely done. However I found myself able to create this win very consistently.
This file cannot be displayed: https://storage.googleapis.com/replit/images/1550685932353_b751ef3178214121799cd3dd64000eea.pn

Profile icon
VicenteLopez

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

Profile icon
[deleted]

It was a very good AI, but in the end, humans will win
This file cannot be displayed: https://storage.googleapis.com/replit/images/1550630567154_782dfbdc92f6cc74e0b37876b2b0c043.pn

Profile icon
DovberEisenberg

This file cannot be displayed: https://storage.googleapis.com/replit/images/1550616758138_4cb55735fb99898099b737bf9c9f8823.pn
i just won

Profile icon
ElliottKim

This file cannot be displayed: https://storage.googleapis.com/replit/images/1549040858509_2e7af8099149713b024dee146598178d.pn

Profile icon
kaiserb1

Connect4

Are they?

Profile icon
llambda

@kaiserb1 It got 4 in a row diagonally.

Profile icon
themaka

Pretty good AI. Nicely done!

Profile icon
Stanlyhalo

This file cannot be displayed: https://storage.googleapis.com/replit/images/1548265420377_0f5b7f9c3561299824ffeaae072904b6.pn
Didn't win?

Profile icon
llambda
Profile icon
Stanlyhalo

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

Profile icon
LukeGordon2

nice

Profile icon
WiNDfaLLx

This file cannot be displayed: https://storage.googleapis.com/replit/images/1548187240986_5a2dd5aa4462b9ce5c79904325a75311.pn

This file cannot be displayed: https://storage.googleapis.com/replit/images/1548187635084_c5631a7323715fcbdabc4f7d384c898f.pn

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

Profile icon
CallumFord

hi

Profile icon
LiamMiller1

hi