Connect Four is a popular two-player connection 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 goal of the Connect Four game is to be the first player to form a horizontal, vertical, or diagonal line of four of one?s own discs. Connect Four is a solved game. Thus, the first player can always win by playing the right moves. In that tutorial, you are going to create your own Connect Four Game in Java.
You can also watch this tutorial in video on YouTube:
Game modeling
We start by modeling the game. To do this, we create a ConnectFour class. Within this class, we will define the following elements:
- An array with 2 characters initialized with ?R? and?Y? representing the discs of each player
- The grid dimensions of our Connect Four
- A 2-array of characters to store the discs placed by the players
- Two variables allowing to keep in memory the last played position
The elements not occupied within our Connect Four grid will be represented by a dot character.
It gives us the following code for the constructor and the properties of the ConnectFour class:
Displaying the Grid of the Connect Four Game
Next step is to display the grid of the Connect Four game. For that, we define the toString() method of our ConnectFour class. For the sake of brevity, we use the new features introduced by Java 8, including the famous Streams.
It gives us the following code:
Getting representation of lines
Before we can check if a player has won after placing a disc, we need to be able to get a representation of a line horizontally or vertically but also diagonally. In order to get these representations, we will use the variables lastCol and lastTop containing the last position played by a player.
To get the representation of a horizontal line, we simply have to build a String from the lastTop line of the grid array. To get the representation of a vertical line, we will iterate from bottom to top on the first index of the grid array.
It gives us the following code for the horizontal() and vertical() methods:
Now, we need to get representation of diagonals. We start with the slash diagonal:
Then, we write the backslashDiagonal() for getting the backslash diagonal:
Checking if a player won the Connect Four Game
Now, we can use our 4 methods to get representation of lines for checking if a player won the Connect Four Game. For that, we create String with four characters corresponding the last player character. Then, we have just to check if this String is contained in the horizontal, vertical or diagonal representation of lines.
It gives us the following code for the isWinningPlay() method:
Interacting with the player in the Console
For our Connect Four Game to be functional, we must be able to ask each player to enter the column in which they want to place their discs. For that, we use the Scanner API provided by Java in standard. So, we ask to the users to enter an integer between 0 and width-1 of the grid.
Before placing the disc, we also need to check that the column is not full. If so, we ask to the user entering a new column number.
It gives the following code for the chooseAndDrop method:
Assembling all the pieces of the Puzzle
Last step of this tutorial is to assemble all the piece of the puzzle for our Connect Four Game. So, we define the variables for the dimensions of the board. Our board will be a classical 8 by 6 grid. The maximum number of moves will be equald to height multiplied by width. Then, we instantiate a ConnectFour object.
We will continue to ask for the next player to place a disc while this maximum number of moves is not reached or while no player won the game. At each step of the loop, we need to change the player who must place a disc. For letting the player placing a disc, we call the chooseAndDrop method of our ConnectFour instance.
Once a player placed a disc, we display the board to the screen. Then, we check is last player won the game. If so, we display a message indicating we have a winner. If we reach the maximum of moves with no winner, it is a draw.
It gives us the following complete code for our ConnectFour class:
Our Connect Four Game in Action!
Best part of the tutorial is there since we are going to put our Connect Four Game in Action!
Once the Connect Four Game is launched, we have the following display:
Red player chooses to place a disc on the column 0:
Yellow player chooses to place a disc on the column 4:
Red and Yellow players continue to place their discs and we have the following display after several other moves:
By placing a disc on the column 5, the Red player won the game!
That?s all for that tutorial.
To go further
To go further, it could be great to add a GUI made with Swing for our Connect Four Game. If you are interested for such tutorial, feel free to use comments below and tell me.