THE
UNIVERSITY OF
FAISALABAD
TOPIC:
CONNECT 4 GAME
Building a Simple
Console-based
Connect 4 Game
06/10/2024 SAMPLE FOOTER TEXT 2
INTRODUCTION:
Connect Four (also known as Connect 4, Four Up, Plot Four, Find Four, Captain's
Mistress, Four in a Row, Drop Four, and Gravitrips in the Soviet Union) is a game in
which the players choose a color and then take turns dropping colored tokens into a
six-row, seven-column 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. It is therefore a type of M,n,k-game (7, 6, 4) with restricted piece placement.
Connect Four is a solved game. The first player can always win by playing the right
moves.
The game was created by Howard Wexler, and first sold under the Connect
Four trademark[10] by Milton Bradley in February 1974.
06/10/2024 SAMPLE FOOTER TEXT 3
Objective:
Implement a two-player Connect 4
game with a text-based interface. Utilize
a 2D array to represent the game board.
Implement functions for player turns,
piece placement, win checking, and
board display. Allow players to restart
the game if desired.
06/10/2024 SAMPLE FOOTER TEXT 4
Components:
• 2D Arrays
• Functions
• Loops
• Condition
• Structure
06/10/2024 SAMPLE FOOTER TEXT 5
CODE:
#include <iostream>
using namespace std;
struct playerInfo
{
char playerName[81];
char playerID;
};
int PlayerDrop( char board[][10], playerInfo activePlayer );
void CheckBellow ( char board[][10], playerInfo activePlayer, int dropChoice );
void DisplayBoard ( char board[][10] );
int CheckFour ( char board[][10], playerInfo activePlayer );
int FullBoard( char board[][10] );
void PlayerWin ( playerInfo activePlayer );
int restart0 6(/ char
1 0 / 2 0 2board[][10]
4 ); SAMPLE FOOTER TEXT 6
CODE:
int main()
{
playerInfo playerOne, playerTwo;
char board[9][10];
int trueWidth = 7;
int trueLength = 6;
int dropChoice, win, full, again;
cout << "**********************Let's Play Connect 4***************************" << endl << endl;
cout << "Player One please enter your name: ";
cin >> playerOne.playerName;
playerOne.playerID = 'X';
cout << "Player Two please enter your name: ";
cin >> playerTwo.playerName;
playerTwo.playerID = 'O';
06/10/2024 SAMPLE FOOTER TEXT 7
full = 0;
win = 0;
again = 0;
DisplayBoard( board );
do
{
dropChoice = PlayerDrop( board, playerOne );
CheckBellow( board, playerOne, dropChoice );
DisplayBoard( board );
win = CheckFour( board, playerOne );
if ( win == 1 )
{
PlayerWin(playerOne);
again = restart(board);
if (again == 2)
{
break;
}
06/10/2024
}
SAMPLE FOOTER TEXT 8
dropChoice = PlayerDrop( board, playerTwo );
CheckBellow( board, playerTwo, dropChoice );
DisplayBoard( board );
win = CheckFour( board, playerTwo );
if ( win == 1 )
{
PlayerWin(playerTwo);
again = restart(board);
if (again == 2)
{
break;
}
}
full = FullBoard( board );
if ( full == 7 )
{
cout << "The board is full, it is a draw!" << endl;
again = restart(board);
}
}while ( again != 2 );
return 0;
} 06/10/2024 SAMPLE FOOTER TEXT 9
int PlayerDrop( char board[][10], playerInfo activePlayer )
{
int dropChoice;
do
{
cout << activePlayer.playerName << "'s Turn ";
cout << "Please enter a number between 1 and 7: ";
cin >> dropChoice;
while ( board[1][dropChoice] == 'X' || board[1][dropChoice] == 'O' )
{
cout << "That row is full, please enter a new row: ";
cin >> dropChoice;
}
}while ( dropChoice < 1 || dropChoice > 7 );
return dropChoice;
}
06/10/2024 SAMPLE FOOTER TEXT 10
void CheckBellow ( char board[][10], playerInfo activePlayer, int dropChoice )
{
int length, turn;
length = 6;
turn = 0;
do
{
if ( board[length][dropChoice] != 'X' && board[length][dropChoice] != 'O' )
{
board[length][dropChoice] = activePlayer.playerID;
turn = 1;
}
else
--length;
}while ( turn != 1 );
}
06/10/2024 SAMPLE FOOTER TEXT 11
void DisplayBoard ( char board[][10] )
{
int rows = 6, columns = 7, i, ix;
for(i = 1; i <= rows; i++)
{
cout << "|";
for(ix = 1; ix <= columns; ix++)
{
if(board[i][ix] != 'X' && board[i][ix] != 'O')
board[i][ix] = '*';
cout << board[i][ix];
cout << "|" << endl;
}
} 06/10/2024 SAMPLE FOOTER TEXT 12
int CheckFour ( char board[][10], playerInfo activePlayer )
{
char XO;
int win;
XO = activePlayer.playerID;
win = 0;
for( int i = 8; i >= 1; --i )
{
for( int ix = 9; ix >= 1; --ix )
{
if( board[i][ix] == XO &&
board[i-1][ix-1] == XO &&
board[i-2][ix-2] == XO &&
board[i-3][ix-3] == XO )
{
win = 1;
}
06/10/2024 SAMPLE FOOTER TEXT 13
if( board[i][ix] == XO &&
board[i][ix-1] == XO &&
board[i][ix-2] == XO &&
board[i][ix-3] == XO )
{
win = 1;
}
if( board[i][ix] == XO &&
board[i-1][ix] == XO &&
board[i-2][ix] == XO &&
board[i-3][ix] == XO )
{
win = 1;
}
if( board[i][ix] == XO &&
board[i-1][ix+1] == XO &&
board[i-2][ix+2] == XO &&
board[i-3][ix+3] == XO )
{
win = 1;
06/10/2024
} SAMPLE FOOTER TEXT 14
if ( board[i][ix] == XO &&
board[i][ix+1] == XO &&
board[i][ix+2] == XO &&
board[i][ix+3] == XO )
{
win = 1;
}
}
return win;
}
06/10/2024 SAMPLE FOOTER TEXT 15
int FullBoard( char board[][10] )
{
int full;
full = 0;
for ( int i = 1; i <= 7; ++i )
{
if ( board[1][i] != '*' )
++full;
}
return full;
}
void PlayerWin ( playerInfo activePlayer )
{
cout << endl << activePlayer.playerName << " Connected Four, You Win!" << endl;
}
06/10/2024 SAMPLE FOOTER TEXT 16
int restart ( char board[][10] )
{
int restart;
cout << "Would you like to restart? Yes(1) No(2): ";
cin >> restart;
if ( restart == 1 )
{
for(int i = 1; i <= 6; i++)
{
for(int ix = 1; ix <= 7; ix++)
{
board[i][ix] = '*';
}
}
}
else
cout << "Goodbye!" << endl;
return restart;
} 06/10/2024 SAMPLE FOOTER TEXT 17
Benefit:
Provides an engaging implementation of the classic Connect 4 game. Demonstrates the
use of fundamental C++ concepts, making it suitable for educational purposes. Offers a
simple and fun text-based game for two players.
Conclusion:
The proposed Connect 4 game in C++ aims to provide an accessible and educational
implementation of a classic game. It serves as a foundation for learning and can be
expanded upon with additional features to enhance user experience.
06/10/2024 SAMPLE FOOTER TEXT 18
06/10/2024 SAMPLE FOOTER TEXT 19