Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. Games

Build Your Own OOP Connect 4 Game

Read Time:10 minsLanguages:

OOP can make development faster, and your applications run faster. During this tutorial I'll demonstrate how to build an ActionScript Connect 4 game, using an organized OOP approach.

Let's get started!

Step 1: Start Off

In the source files I've included a connect4_start.fla file. Start from this file; it contains all the movieclips needed to finish the tutorial. It contains 4 movieclips. A red chip movieclip, a yellow chip movieclip, a boardpiece movieclip and a winner dialog movieclip.

Step 2: Create the Connect4 Class File

Create a new ActionScript file and save it with the name Connect4. Then add the following code:

In this code block we import the classes (I also used the Tweener class which you can find here or in my source files). We define the field variables and we create the constructor. The constructor has two arguments, the number of columns and rows you want for your game. We put these values in our field variables.

Finally we init the columnWidth and rowHeight variable. One column has the same width as the width of one boardPiece (same principle for rowHeight).

Step 3: drawBoard Function

This function will draw the playboard of the game depending on how many columns and rows you want. Add this function to the constructor of the Connect4 class:

Step 4: createBoardArray Function

Here we create a 2-dimensional array which will contain all the locations of the board.

We fill the array with 0's. A zero means there hasn't been a chip placed on that position. This function also needs to be added to the constructor.

Step 5: putChipReady Function

In this function we identify who the current player is and add the matching chip to the display list. We give the chip a negative y value so it shows up above our playboard. Once again, add this function to the constructor.

Step 6: enterFrameHander

On every frame this function calculates the column where the current chip should fall if you click at that moment. That way it's easier for the user to make a move. The function calculateColumn(explained in the next step) returns the column you are hovering.

Then we calculate the new x position of the chip. Therefore we multiply the currentColumn with the column width. Because the registration point of the chips in centered we have to add the column width divided by 2.

Finally we tween the current chip to the postion we just calculated.

Add the enter frame Event Listener to the constructor.

Step 7: calculateColumn Function

This helpfunction gets the x position of the mouse and returns whichever column matches.

If your mouse x position is less then zero then we return the first column(0 because array is zero-indexed). If your mouse x position is more then the width of your board we return the last column. If your mouse x position is on the board we devide the x postion by the width of one column.

Step 8: boardClick Function

This function first checks which column you clicked (by using the calculate column function which I explained in the previous step).

The for loops through all the rows and as soon as board[row][columnclicked] == 0 we know the chip has to be placed on that location. Remember 0 in the BOARD array means the location is empty.

If we find the location where the chip has to fall, we fill that postion in the board array with the number of the current player (we need these numbers later on to check for the winner) and we place that chip with the placeChip function (explained in the next step).

Finally we toggle the player (explained 2 steps further) and we put another chip ready. The return makes sure we exit the for loop.

Add the click Event Listener to the constructor.

Step 9: placeChip Function

This function gets the row(position.x) and column(position.y) where the chip has to enter, then calculates the y- and x distance.

distanceY: you multiply the row you clicked with the height of one row. Because the registration point of the chips is centered we have to add the row height divided by 2.

distanceX uses the same principle.

Then we use tweener to tween the current chip to the right position.

Step 10: togglePlayer Function

This function is pretty straight-forward. If the current player is 1 switch to player 2, else switch back to player 1.

At this point you can already place the chips, but there's currently no check to see if a player managed to connect 4 chips. The next step is to code this check.

Step 11: checkForWinner Function

This function has 1 argument, the position of the last placed chip and return true or false. The function uses 4 subfunctions each of which checks for a winner.

We pass through the position for each subfunction. If one of the 4 returns true we have a winner.

Step 12: verticalCheck

To check for a vertical connect 4 we only have to look at the chips beneath the current chip. (There can't be a chip above the current chip at this point).

First we check if there are 3 locations beneath the current chip. If not, there's no way you can connect 4 and we return false.

If there are 3 or more locations beneath, we start a loop which goes through the 3 rows beneath. If one of the chips beneath is from the other player we did not connect 4 chips (return false).
If the loop can be finished we know there are 4 connected (return true).

Step 13: horizontalCheck

What we do here is first check the chips on the left of the current chip and then the chips on the right side.

Therefore we init a counter with 1 (the chip you just placed is the first one in the row). Then we go to the left until we reach a chip of an other player and meanwhile we count the chips of the current player.

We do the same for the right side. So if our counter is 4 or more we have connected 4 chips (return true).

Step 14: leftUpDiagonalCheck

This functions is similar to the horizontal check. The only difference is that we now check diagonally.

First we go left upwards: we count the chips of the current player and if we come across a chip of the other player we break the loop. To make sure we don't go outside our board array the while loop has to stop when our row or our column is less then 0.

We use the same principle to check the positions right downwards.

Step 15: rightUpDiagonalCheck

This function is almost identical to the previous function. The only difference is the direction of the diagonal we check.

Step 16: Updating the boardClick Function

Now we have to implement the code we just wrote. After we place the chip we check for a winner. If we have a winner we remove our EventListeners so you can't place a new chip and we show who's won (explained in the next step). If we don't have a winner we togglePlayer and put a new chip ready.

Step 17: showWinnerDialog Function

This function simply adds a new instance of the WinnerDialog which you can find in the library. If the current player is 1 red wins, else yellow wins.

Step 18: Create the Document Class

Create a new ActionScript file and save it with the name "Application".

In this class we add an instance of the Connect4 Class we wrote to the display list. Don't forget the constructor arguments.

Finally, click on the stage and set the Document Class to "Application".


You just learned how to create a connect 4 game and how multiple dimensions can make life a lot easier! I hope you enjoyed the tutorial and thank you for reading.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.