Connect-4 Game with CPP — Part 3

Aditya E Ajith
5 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 Basic Game Logic. If you haven’t seen the Previous article… You can check’em out here. The first part is here. Next, We are going to work on multiplayer and Game Over Part.

Adding Co-op Multiplayer

I’m adding the Co-op now because it is required for the GameOver conditions.

I’m going to set up 2 players: red and Blue. Inside Connect4.h I’m going to create a variable that’ll alternate between Red and Blue Input. For that, I’ll use a boolean. And if the input is valid then change the player.

Connect4.h

For those who don’t know, In the input() , You might have seen a weird like (isRedPlayer)?”Red”:”Blue” .This is called a Ternary Operation. What it does is, if whatever in the ‘( )’ is true then do the first one after ? else do the one after :. It's like a smaller version of If-Else. You can read more here.

Next, I need to tell PlayGame() that I’m sending the Player as well. and to add the coin According to that. Before that, we need to set the character in the CoinState enum.

Coin.h
Board.h

The ternary operator is very Convenient. Run your Code. Next, We Are Going to Set the Game over the condition.

Adding GameOver

The game over Condition :

  • if all coins are filled, Draw
  • if vertical or horizontal or diagonal 4 coins with the same type are present, win

Draw Condition

For checking if coins are filled. I don't want to run loops all the time. So I’ll create a variable that’ll count the coins each time it is added. and I’ll check if it's equal to the total coins, Then it's a draw.

I’ve also added a condition so that if already game over. Then we don't need to check of fill.

Win Condition

For this as well. I don't want to loop through all the coins. If I did that I’ll be checking on what I’ve already checked in the previous Update Loop.

So what I’ll do is check the surrounding on the coin that the player input. When the player input and then we put the coin. With that location and coin state, we can check 4 in all directions and if we can hit any 4 with the same then it's a win.

First I’ll create a variable that can help store the current player in Board.h because I'll be checking in lots of places.

Next, is to check for all the conditions if true then set isGameOver to true.

Vertical

First, we’ll Check for Vertical. I need to Check for both vertically up and vertically down. But Vertical Up is not needed, because the coin will always be on the top no matter what.

Horizontal

Next, We Need to check for Horizontal left and right. Here it's a bit tricky. we need to check both.

The rightand left variables are used to make sure that it stops once the coin of the opposite state hits. why? Well, imagine we have a situation where a Red player just inputs in the middle. like this
R B ’R’ R R
what happens is that the loop will keep on running even if B is hit as wrong and then it sees the next as R and then it increments the count. The red player wins in this case. But if you deactivate the left checking after it hits the B then it won't check. This is the use of left and right .

Diagonal

For Diagonal, It's a mix of both. but we need to check for up and down as well as look left and right for both.

R       R
R R
R
R R
R R

In all 3 of them, I’m stopping when i reaches 4 or when there's nothing else left. and if the count is greater than 4 then there is a set available. so the current player wins.

With this, you are finally done with the Game. Run the game. Refactor your code. If you know any optimizations then do that. That’s all folks.

I won't be attaching the Whole code here. I want you guys to do it yourself. I Don’t want you to just look at the last code and copy-paste. Understand the code. How each line is working and why I have written it like that. Always have clarity in what you code. This will help you become a better programmer. If you want to, read the previous articles of this series.Read Part1 and Part2

This has been a fun experience. Thank you all for reading. Stay safe. Happy coding.

--

--