⭐ If you would like to buy me a coffee, well thank you very much that is mega kind! : https://www.buymeacoffee.com/honeyvig Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Friday, November 7, 2025

Connect Four in JavaScript

 A retro grid-based game in vanilla JavaScript, HTML and CSS Connect Four (also known as Four Up, Plot Four, Find Four, Four in a Row, Four in a Line, Drop Four, and Gravitrips (in Soviet Union)) is a two-player board game in which the players first choose a color and then take turns dropping one colored disc from the top into a seven-column, six-row vertically suspended grid. The pieces fall straight down, occupying the lowest available space within the column. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own discs. What are we going to focus on? for loops Array work onclick() Rules of Connect Four You will start as player One You will take your go as player One You will not be allowed to go in squares that are not at the bottom of the grid, or do not have a taken square under it Once your go is taken and you did not win, It will be player Two's go The gamer will continue until a winning combination of four is reached by player One or player Two, or until the grid is full and no winning combination was found. 

 

0:00

okay are we ready hey everyone and welcome to another javascript gaming specifically to

0:06

practice our js skills so if you want to do that or you know you simply just want a gamer connect for

0:12

and want to try code along then please go for it it's gonna be fun now if you watched my videos before you will know that we

0:18

don't really do styling in these these are videos just for you to practice your logical thinking which means it's gonna look ugly but hey

0:25

that part is gonna be totally up to you i want you to take the game make it your own

0:30

level it up it's yours this is essentially what we're going to end up with by the end so like i said super basic now for those of

0:38

you who perhaps have never played connect 4 before let's actually go through how we're going to solve making this

0:44

game so of course we're first going to make our grid we are then going to make a

0:52

event listener listen out for on clicks and on the click it's going to put either

0:57

player 1 or player 2's piece now we can't put the pieces anywhere on the

1:03

board right because you know gravity they have to sink to the bottom so if you want to go

1:08

in a square that perhaps you can't go in we're gonna have an alert that tells you you can't go there you of

1:15

course win connect four by having four dots all in the line so that

1:21

horizontally vertically or diagonally so we're going to be doing that part as well now here's my favorite

1:28

part when you have finished your game please do share it with me i would absolutely love to see how you

1:34

have taken this tutorial taken the game and really made it your own so you know if you want to put like

1:40

batman's on your coins then please do if you want to make the grid bigger or have some sort of like level up

1:46

feature when you go to the next level then please go ahead and do that share your game with me on twitter

1:53

youtube instagram whatever you wish okay what are we waiting for let's do it as always please do like and

2:00

sub to my channel as it really does mean that i have the means to keep creating more content for you guys so let's do it

2:09

okay so let's do it as you can see here i've already have my project set up with an index html

2:16

file an index a js file and a styles css file which we now have to hook up to work

2:22

together i have gone ahead and put in some html boilerplate for us we have a title which

2:30

no will not appear in the browser it will however appear in the tab which i'll show you later

2:37

and a link tag so we can link our style sheet now as our style sheet is in the root of

2:44

our project i simply have to write styles css to hook it up

2:50

i'm also gonna put in a script tag and if you know me i'm gonna show you two ways to put in

2:55

the script tag we can put in a script tag at the bottom of our body so if i just

3:03

put a script tag with the source and once again it's in the root of our project so the index.js file

3:09

so that is what our script tag um is linked to i'm just going to put a

3:16

chart set as well whoops

3:21

so char set uh that one thank you very much

3:29

now if you know me you i will now tell you that you can't be your script tag at the bottom of the body but make sure that is

3:36

after any html that we want uh to put in or alternatively you can also put it

3:45

in the header so in the head i'm gonna just put it here just to show you another option because we can

3:52

also use a dom content loaded we can use an event listener to listen out for

3:57

uh once everything here is loaded and some people say that is a more foolproof way so i'm going to show you how to do

4:02

it now as always the option is up to you if you prefer to use the script tag at the bottom

4:07

of your um body that is fine just don't use both otherwise you'll have two scripts

4:14

running so i'm just going to leave mine here because i want to show you how to do this you can also use the document and use

4:21

add event listener to listen out for when the dom content has loaded

4:28

okay and then once it has all our javascript we're gonna put in between

4:35

these two little curly braces okay done now

4:42

let's move on now because i want this to be a sort of beginner lesson we could use javascript to add

4:49

all these divs for our connectful game but i'm just gonna keep it very simple and put them in here

4:55

in our html so our connect four game is gonna have it's gonna be seven

5:00

squares wide and six squares high okay but i also want to put in like a

5:06

secret seven squares at the bottom because we want to tell our browser where the bottom of the grid

5:13

is and this is just a solution that i thought of of course like you don't have to use it but this is just the way that i would

5:19

solve it in order to build our game so what's that seven times seven is 49

5:25

let's do it i'm gonna make a div that i'm gonna give the class of grid because this is where all the

5:33

magic is gonna happen this is gonna be our game grid okay so let's call it a class now any

5:40

div that i'm gonna put in here i essentially want to be or represent the little squares on my

5:46

connect for game so as i said i'm gonna need seven times six for the game but then a secret

5:52

seven which we will go into later so i need 49 of these so i'm still just gonna face one two three four five six seven

5:58

eight nine ten of course like i said we could do this in javascript um i do do it i do add these divs with

6:06

javascript in a lot of my other games so if you want to check out how i do that it's a create board function that i use

6:11

pretty generically please go ahead and check those out so one two three four five that's 50

6:19

divs i'm just gonna get rid of the last one to make it 49.

6:24

okay now i did say i wanted the last seven to have like a like uh represent the end of our

6:32

you know connect four board so i'm just gonna give them a class of taken again you will see why

6:39

later on so there's one two three four five

6:46

six seven great so we've got a div with a classic grid

6:52

with 49 divs in it now let's actually style it up to uh see

6:58

it visually so because we want to grab the grid i'm

7:03

going to grab the class so use a dot for class of grid and i'm going to decide

7:10

that i want all my squares to be 20 by 20 pixels which means that i need to make the grid

7:16

well let's first give it a border of one pixel solid and as a default it's going to appear black solid one pixel black

7:25

and then height i'm going to go with 120 pixels of course and then width 7 x 20 is 140 pixels

7:34

okay and now while i'm here i'm also going to style every single div inside the div with the class of grid

7:40

and i can do so like this by once again grabbing the class of grid

7:46

and then every div inside of that so that's how i would write that is going to have the following styling i

7:52

just want them to be height 20 pixels and

7:58

width 20 pixels now i'm just going to give a background color for now so we can see

8:04

what's going on so now if i just copy

8:10

the relative oops copy the path sorry and then go to a browser

8:17

and paste it you will see there's my grid and there are all the squares but it

8:23

obviously doesn't look right right we need the squares to fit in the grid so i want them to sort of like snake

8:29

over so i'm going to do this by adding display flex

8:37

so now if i show you what that looks like and refresh whoops fresh

8:43

okay now they're in there but they're all squashed look how thin they are i don't want that i wanted

8:49

to snake over so i'm also going to use flex wrap

8:54

wrap and there we go okay so now oh i think we've got too

9:01

many one two three four five six seven yeah we have too many but that's

9:07

that is fine seven times one two three four five six seven so we need to delete

9:13

seven ten more ten of these is to leave ten we did a few too many so one two three four five six seven eight

9:21

nine ten delete

9:27

okay great so now they all fit if i just go over the one two you will see

9:32

how they are snaking over and then of course we have the last seven which have the class of taken

9:39

so so they're outside of my grid and we're just using them to let our code know that this is the

9:45

bottom of the connect4 uh board game is that a board game you know what i mean game

9:52

okay so i'm just gonna get rid of the background color blue and while we're here i'm gonna add a

9:58

class of player player player one i'm gonna give it a background color

10:06

of let's say red and then a border radius because i'm

10:12

gonna make them circles of 10 pixels because border radius on a 20 by 20 square is gonna make this

10:18

a circle so we've got play one and let's make player two

10:24

let's move to player two blue okay cool so we've got everything we

10:31

need now let's start picking out elements from our html and our javascript so we can work with

10:37

them so the first thing i'm going to do is actually pick out the squares so i'm going to save them as squares i'm

10:44

going to use document query selector all thank you tab 9 to pick out and once again this is going

10:51

to be useful because we just learnt this to pick out all the divs inside the div

10:56

with the class of grid so that's how i would do that

11:02

so we've just picked out all the squares uh now i also want a way to i guess tell the

11:09

user whose go is so i'm going to use an h3 tag for this and i'm just going to write

11:16

the current player is player and then i'm going to use a

11:23

span tag to just interrupt that h3 tag and i'm going to hard code a one and i'm just gonna use an id just in

11:30

case we wanna use another span right so i'm just gonna put current player

11:36

and while we're here i'm also just going to put a place we can show the result i'm

11:42

going to leave it empty for now and just give an id of results

11:49

okay so that's looking good and in here let's also pick out uh let's pick out the result

11:57

so const result equals document query selector i could

12:04

use get element by id but it's totally up to you i'm gonna use query selector to look for an id

12:09

so we use a hash for id we're looking for the id of result and another id we need to pick out which

12:16

is the display for the current player so i'm just going to display current player equals once again we get the

12:23

document oops and we use i'm going to use query selector again just to make everything

12:29

consistent i'm going to look for the id and save it for our javascript

12:36

okay we also need to let our javascript know who the current player is and we hardcoded one so i'm just gonna put this

12:41

here two i've used the let as this will not always be one it might be player two so

12:46

that's why i use the let there okay now i think the next thing i

12:52

want to do is add um an on click to all our squares

12:57

okay so i want uh essentially something to happen every time i click any of the squares in

13:04

our connectful grid so i'm going to use a for loop for this oops making sure that i'm in the curly braces

13:10

four let i equals zero and now because we're dealing with the

13:16

squares uh i could use the square's

13:23

length that'll mean even including all the taken squares

13:32

and i think that's fine let's just carry on and see what happens

13:38

okay so for each of the squares in my squares in my uh grid sorry

13:45

i want to get the squares and then for each one because they're looping so i'm going into the array

13:52

and i'm looping i just want to add it on click and then on the click and we need a

13:58

function so i just use an arrow function for now i'm just going to make an alert that says

14:04

you have clicked square just so you can see what's happening i

14:10

want to show you so i'm just going to use the i and

14:16

save now let's refresh so if i click here

14:22

oops you have clicked square 13. let's refresh i'm going to click the

14:28

first one you have clicked square zero because as you know we start erase from zero

14:33

okay so this is working we have added an on clip to each of these squares including these ones but for now

14:40

let's just ignore it um we'll decide what we want to do with that later but i think that's fine

14:46

cool it's pretty fun isn't it we know exactly which square we have clicked yeah

14:53

okay so we actually don't want this alert i've just done that so we you can see what's going on before me coding any further because i think that

14:59

is important okay to delete well the first thing that

15:05

we want to do is decide how we're going to solve this

15:11

so obviously in connect four when you drop the little um [Music] token we need to account for gravity

15:18

right so i think let's do it that if the square below your current square that you want to go

15:25

is taken you can go on top of it right simple so that's why we added the class of taken

15:30

at the very bottom because at the moment we're going to write something that says you can only go on that bottom

15:36

so only on this row because all of these are the class taken

15:42

so if the square below your current

15:52

square is taken you can

15:59

go on top of it cool i'm just going to minimize this

16:04

because we don't need it so how do we write this in code if

16:10

we go into our squares array i'm just going to use i but i'm going to add 7

16:17

because i want to check what's directly below the current square that we are so we do that by adding seven to the

16:22

index number of it and then if that square class list contains

16:31

t can well we can go right

16:36

so i would do it like

16:42

oops this so if that is true and the square directly below us is

16:48

taken uh if the current player equals player one thank you tab nine if the

16:54

current player equals player one i don't know why it keeps formatting like that i don't like it

17:00

um well then to that square so once again that square i'm gonna add the class list

17:08

um i'm gonna add well i'm gonna add taken for one because it's obviously taken but i'm

17:14

also gonna add squareless class list add player

17:23

one okay and then of course we want to change it to player two so current player

17:30

equals two um and then let's display it so display current player

17:38

in uh html uh and let's just display whatever the current player is because we've just

17:44

changed it to two so let's actually show that visually to our user okay ah click okay call is changing

17:51

okay now let's play a twos go so we can't do anything so now now let's write some logic for if it's player two

17:58

um if current player equals two

18:07

well then essentially we just it's going to copy this really because we're doing the same but just we're gonna add player

18:15

two and then change it back to player one cool um and then i'm also just gonna add

18:24

an you know like an else because we've got these if if this or we could just um

18:34

we might have to do an else if i think we might have to do that

18:41

okay yes we might have to do that else if else alert

18:48

uh can't go here okay so now

18:56

change player two player two goes let's play one again

19:01

player two oh can't go there where's my alert where's my loud it's annoying

19:08

hmm what did i do ah right i put it in the wrong place ah else if alert

19:16

okay so now one can't go here can't go here but play two

19:23

can go there can player one go here yes it can so awesome

19:28

so that's why i did the else if if i just got rid of this and just did an alert well obviously

19:36

yes i know i can't go there i can click here it'll just tell me i can't go there even though i can

19:41

because it's uh sort of like executing this and then executing that anyway so that's why i

19:46

did else if else cool

19:53

okay so we've done that part of the logic the last thing i need to do is check the

19:58

board for wins so i'm going to do that with a function um this i do actually want to execute

20:04

every time we click so there's no need for else if if we just want to check the board every time

20:10

we click on the square so now let's write our function i'm going to do up here of course because

20:17

we need to write it um you need to write the function before

20:22

use the function so function checkboard

20:29

like so now this might not be like the most uh

20:35

i guess it's a lazy way to check for wins i mean if you want to uh i guess make

20:42

this cleverer please do and share with me i just couldn't be bothered it's a small board right so i've just gone ahead and figured out

20:49

all the winning combinations on it so please feel free to take this i'm gonna obviously put it

20:54

in my um github but i've just got a winning uh a raise

21:02

array again if i've got any wrong in here i'm sorry um let me know but con swinging arrays

21:11

and there we go so i have literally just pasted all the possible winning arrays

21:19

uh so by using my index number of course on our board once again if you can think of something

21:24

clever then please do i'm really lazy this was fine okay so we've got our winning arrays

21:29

now let's actually use them in order to check our board if any of the players have a winning

21:35

array so to do this um well again i'm just going to use a

21:40

for loop so for this time let uh i we can use i we can use y

21:48

like it's totally up to you whatever you want okay so as long as y

21:55

i'm gonna get the winning arrays uh length it's gonna loop over

22:04

but we're gonna loop over each one of these right we're not looping over this this this this we're just looping over

22:09

one two three each of these so however many of those are that's however many times we're going to

22:15

loop and increment by one now um it's always i think it's always

22:23

important to use const as much as possible we don't use a let this is blocked scoped so that's why i'm using a const we're

22:30

gonna say for each time we loop we're gonna save square one square two square square three square four so i'm

22:37

literally just gonna name it square one squares for the square one we're going

22:43

to go into the squares array this time so it's const square one well let's just

22:49

say we want to get the index 13 we need to go into our squares array

22:54

and then i'm going to literally pass through so i'm going to go into the winning arrays and go into the so i'm going to loop

23:02

over the first one so let's just say we're doing our first loop i'm going to grab this so i'm going

23:08

into the winning arrays going into the array and i'm grabbing this and then i want to get the first item

23:14

from it so i need to pass through a zero so that is how i would do that and

23:21

whatever that is i'm saving a square one

23:27

okay so i hope that makes sense i'll talk you through it again we now need to get square two

23:34

so what i would do is um of course we're checking on our squares grid so if i've just uh checked

23:42

yeah the square on our board with index zero so for example let's go back to our

23:47

board say i want to check this is what we're checking we're taking the square with index zero one

23:53

two three four so we're checking those so of course i would then need to go

23:59

into my squares array i'm gonna go into it and then i'm just

24:05

gonna pass through this value so i'm gonna i need to now get this value which is a one

24:10

so i need to go into my winnings array this is an array so why still because we're still in the

24:17

same ones whatever y is is going to be the same for this so winning

24:22

arrays and then i need to get the

24:28

item with index one so zero one so this will return all of this essentially is returning

24:35

back one the number one okay so i'm going into the squares array i'm going into

24:41

the squares with index one okay so hopefully you get that

24:46

i'm just gonna carry on if you don't please feel free to reach out and i'm happy

24:51

to explain further and one more of course because we need

24:56

to check for four matches and there we go so we are looping we're

25:02

grabbing all four squares that we want to check okay so now

25:08

we want to check um check those squares

25:19

to see if they all have the class

25:25

of player one okay so if um

25:34

i mean this is probably the easiest way to do it i'm gonna go if squares one i mean we could do

25:39

another loop but there's only four so i don't think it's worth it but please feel free to do another loop if you wish

25:45

i'm just gonna go squares class if square if that first square that we

25:50

picked out if it has a class that contains oh god contains

25:58

uh player one

26:04

so if that is true

26:10

and the second square contains play one how do i do this so it looks neat

26:18

ah there we go and the third square contains the class of

26:24

player one and the fourth square contains a class of player one

26:30

and if all that is true well then we want to get our results

26:37

that we picked out was it result oops don't want that i think it was

26:43

result result okay

26:49

you want to get a result and use inner html to say

26:55

player one wins okay and of course

27:01

i mean let's just copy this for player two

27:08

just make sure that that's an if statement i'm just going to copy all of this actually so we have the pseudocoders as

27:14

well oops okay

27:20

i'm just gonna use command d

27:25

two player two wins okay i think that's all we need

27:35

whoops we did this outside of our for loop so i'm just going to get all of this

27:47

right here's our function okay then we need to actually put all of this

27:55

inside

28:01

okay i'm just gonna make sure

28:08

if then this oops then

28:15

otherwise this

28:21

so that is our loop great

28:30

i'm just gonna format this a little bit better so just go command

28:35

so just tab it out and click save and there we go

28:42

so now we play the game uh just need to make sure we can only

28:49

add players if that space is not currently taken

28:57

okay now let's play so now we play the game

29:08

and we get a four so there we go three four tada

29:15

we have now finished our game of connect four you will see that the game

29:22

works okay so that is how i would build a gamer connect four in a

29:28

super simple way please do feel free to take this game make your own

29:33

silent up uh give it next extra levels if you can think of a funky way uh to not have to write out all the

29:39

winning arrays uh then please do let me no like i said i was pretty lazy with this and i just

29:44

stuck them all in there because i thought you know like it's it's a small grid thanks so much again

29:50

for watching and i do really want this to be a conversation so yeah please do show me your games okay

29:58

nice later

No comments:

Post a Comment