Connect-4 Game with CPP — Part 1

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.

This project might be too long for one article. so I might make it into a series. Now, This project is not precisely for Beginners. I will be using some intermediate concepts of Data structures and Algorithms. But I will do my best to make this as simple as possible and attach links for those who want to learn more about it.

I won't be explaining how to set up the CPP project. I assume you already know that if you don't, check this Microsoft doc for installing CPP in Visual Studio. You can also use Online compliers, I use replit most of the time. With that said Let’s START!

Visualizing The Result

This is the CRUCIAL part. You need to visualize what you want the Output to be. This will help you build better code Architecture. Here we’ll be making a console game. For that, we need to understand the game.

NOTE: If you want there are visual libraries that help you use graphics with CPP. I have made this same game using SFML. You can check them out in my Github link here.

Connect-4 game is a two-player connection board game, in which the players choose a color and then take turns dropping colored tokens 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 tokens.

This is my SFML Connect-4 game

From this, we can understand what we need,

  • 6x7 grid for the board
  • Coins of two types for each player

As for Logic,

  • Each player should get alternate turns
  • add coins to the grid
  • Check if there are any 4 coins of the same color in vertical, horizontal, and diagonal. If there is one set, then that corresponding player wins.
  • Check if there are any slots left on the board(ie, check if the board is full). If not, then it's a draw.

Now, We have an idea of how the Game architecture will be. With this, we Can start Building the Game.

Getting Started with Coding

This is what all you have been waiting for, The FUN part.

What I like to do is split my code into header files, for example Game.h, which will only have one class with its declarations and also the required header file. And all my definitions will be in a CPP file with the same name, Game.cpp. But for this tutorial, I’m going to put both in the Game.h file.

In CPP, all code starts with main(). So, we create main.cpp with amain() function in it.

main.cpp

Creating The Game Loop

Next, we will be creating the Game loop.

I don't like my main() function to be filled. Minimum stuff in the main function the better your code will look. It helps reduce Code smells and gives you code more readability.

So, I am going to create a class Connect4 in which I'm going to declare a function void Run(); , which will handle the game loop and call this in the main() . I’ll also throw in a constructor and destructor. I feel like I might need to use them. If these words are confusing you, don't worry, just go to the link attached to the words, and then you can learn about them.

Connect4. h
main.cpp

With this our main.cpp is done and we don't need to look into that again. Now let's look into our Game Loop. The game loop code should run until we Quit the game. To store that condition we are going to create a boolean isGameOver. We will set it to false when the game is created. We will create an Update()that will run every loop. inside that will set the value of isPlaying to trueif the game is over. Right now I don't have any idea of how I want it to be. So I’ll just keep it as a comment.

Connect4.h

Getting Input

Let’s now create an Input system that handles all the input from the player.

How to get the Input?

Since it's a console we can use cin to get the input. To use cin we need the header file iostream . And I will also add using namespace std;. This will tell the compiler that I’ll be using the std namespace. This is generally a bad practice because this will create ambiguity with other header files. But since I’m not using any other header files, This is fine.

The next question is What is the input?

In the game, you only have the choice of choosing which column to drop the coin. So, Our input will be the column number that the player chooses, which will be an int.

Connect4.h

You might notice that I'm using #ifndef ,#define and #endif . This is similar to #pragma once . This will help avoid duplication declarations on classes and includes. CONNECT4_H is the token that is used in checking duplication. You can read more about it here.

If you want, Go ahead and run your code. If there are any errors. Fix them and then move to the next topic where we will start Connecting the Board.

Connecting The Board

All the Game Logic is going to run inside the Board. So we’ll create a board class header file board.h . Add is all that #ifndef .

Board.h

Next what I like to do is Create connections with the Game Loop. In the last topic, we accepted an input. Now we need to use that input. This input is the one that’s going to let the player play the game. so we will create a function playGame()exactly for that and then take in the input.

Board.h

Next what we need is something that’ll notify Game over. Since the Game logic is Run inside board.h , It will handle that responsibility. So we’ll create a variable that’ll store this and then a function that returns it. Now to check if the game is over we need to check if any one of the players wins the match or if it's a draw, which we will do it later.

Board.h

Next, We need to call these functions inside Connect4.h . For that, we need to create an object of this class and access its member functions. To create an object of the class, we need to include it.

Connect4.h

With this, we Finished All the Game engine Logic. now we don't need to look into this class anymore. We can fully concentrate on the Board.cpp , Work on Creating the Board, the coin, and the Game Logic. Which we will do in the Next article. Happy Coding.

--

--