Connect-4 Game with CPP — Part 2

Aditya E Ajith
6 min readMay 16, 2022

--

How to create a Connect-4 game with CPP(C++) in the console.

This article will teach you how to create a Connect-4 Game in CPP with adequate explanations and links for further explorations.

Previously, We Implemented the game engine Logic and created the connection with the game Logic. If you haven't seen the Previous article… You can check’em out here. Next, We are going to work on Creating The Board.

Creating The Board

Now we have to create the Board. The standard size of the Board is 6x7. I’ll be going with that, But feel free to tweak those values. if you want you can make it dynamic as well.

I will create 2 variables that store this value. and Create a function to initialize them and call it in the Constructor.

Board.h

Next, we need to create the board itself. Since it's a console game. We will use char. You can create a 2-D array. There are different ways to create a 2D char array, You can find them here. But what I like to use is different.

I am going to create an array of coins. in which all the coins are going to be deactivated from the start. when the player inputs data, then the appropriate coin will be activated. For this,(you might have seen this coming) YES!. We will create a Coin class.

Creating The Coin

The coin has 2 States. Inactive and Active. If it's inactive it’ll display an empty char .What if it's active? Our Game is a 2 player co-op game. so what I’ll like to do is let the state say which player it's pointing to. But we haven't set that yet. So we will go with the ‘C’ for now.

Now to set these states, I’m going to create an enum CoinStates. And I’ll be using an enum Class, You can read about the advantages over normal enums here. since it's a part of the Coin. I’ll just define them in the coin.hfile itself and create a variable that will hold them.

Coin.h

As you know Enums hold int value. First Enum starts with 0 and then next is 1. we can Set custom values for them as well. and char uses ASCII value when converted to int. so what we’ll do is Set the enum to their corresponding char.

Coin.h

Now if we convert these to char , we’ll get the corresponding char. Pretty cool right? Of course, for that, we need to define getter and setter functions.

Coin.h

the line (char)state is called explicit type conversion. You can learn more here. Now we are done with coin.h . Next, we’ll go back to Creating the Board.

Creating The Board — continue

Next, we have to create the Array. You can simply just create it like
Coin coin[6][7] . But I like to create it dynamically since we have already initialized the max size. To create an array dynamically we need to use pointers. Creating a 2D array dynamically is a bit different.

Board.h

Since it's a pointer I need to free that memory when the program ends. we do that in the destructor.

The next thing is to Print out the Output. Since the output is displayed after we play the game. It makes sense to put it at the end ofPlayGame().

Coin.h

From here onwards the codes are going to get longer. So the images will only contain the ones that are changed or maybe only the funtion definition.

Now the Display is done. Go ahead and run the code. You might be seeing empty slots. Go ahead and try changing the state in coin.cpp. Try adding different characters try adding more enums. Try everything. Have fun!

After you are done having fun, Let's move to the next one.

Refactoring your Code

Now that we have reached a critical point. What I like to do is look back into my code and then see if any of the variable names to function names are appropriate for its function. also, check if I can read the functions properly or if something is repeating.

Now that I look at it. I see that my Board size is a bit wrong. column is vertical and row is horizontal.

drawing

So maxColumnCount is 7 and maxRowCount is 6. and initialization and display were inverted. Now you understood why I initialized it dynamically. Ease of edit.

Board.h

Now that I look at it… GameOver doesn't make sense as to what it holds. and the destructor doesn't explain what it's doing. so I’ll change those.

Board.h

Now it looks much better. Now let's move on to the next part. Processing the Input.

Processing The Input

The input is the column number. but the input we receive will be the Column number +1 because our Column starts from 0.

Now we need to set that column to active. but which row? In the game, you drop the coin in the column and it’ll fall to the top of that column stack. so we need to find the top Free slot in that column. then set that to active.

To check that we need to understand how the array is placed. If you see the drawing on top. The bottom is the last row. so we’ll check from maxRowCount to 0. if we get an inactive one, we will activate it.

Board.h

Now there are 3 issues that we are going to face.

  • If the input is Greater than max size or less than 1 then it should be invalid
  • If the Column is already filled then it should be invalid
  • If the input is invalid let the player try again

To solve this, What we can do is set a bool if the input is valid or not.

Board.h

I found that I again messed up the row and column in FindEmptyRow . Do fix that.

Now we need to loop until the input becomes correct in the Connect4.h . For this, I’ll use a do-While Loop. I also removed DisplayBoard() from playGame() and moved it to public. So that can Access it in Connect4.h . Now the board will be displayed before Getinput() .

Connect4.h

Go ahead. run your code, and Test it out with different inputs. Congratulations on reaching this point. You have got the Basics of the Game done.

Next, we are going to work with the Multiplayer and the Game over Logic in the Next Article. Happy Coding

--

--