0% found this document useful (0 votes)
20 views9 pages

Aaaa

This C++ code defines the structure and functions needed to implement a Connect 4 game. It defines a playerInfo struct to store player names and pieces. It also defines functions to get player input, place pieces, check for a win, display the board, and restart the game. The main function uses these functions in a loop to run a game between two players until someone wins or the board is full.

Uploaded by

ayubahmad207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Aaaa

This C++ code defines the structure and functions needed to implement a Connect 4 game. It defines a playerInfo struct to store player names and pieces. It also defines functions to get player input, place pieces, check for a win, display the board, and restart the game. The main function uses these functions in a loop to run a game between two players until someone wins or the board is full.

Uploaded by

ayubahmad207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

#include <iostream>

using namespace std;

//STRUCTURE DFINE KIYA HA JO USER DEFINED DYTA TYPE JO SINGLE NAME KY ANDAR

//grouping(combine two pices of information related to player)

//code organization and helps prevent mixing

struct playerInfo

char playerName[81];

char playerID;

};

//FUCNCTION DECLARE TO GET THE PLAYER CHOICE FOR DROPPING A PIECE

int PlayerDrop( char board[][10], playerInfo activePlayer );

//FUNTION

void CheckBellow ( char board[][10], playerInfo activePlayer, int dropChoice );

//TO DISPLAY CURRENT STATE OF BOARD

void DisplayBoard ( char board[][10] );

//TO CHECK IF THE PLAYER HA CONNECTED FOUR PIECES

int CheckFour ( char board[][10], playerInfo activePlayer );

//TO CHECK IF THE GAME BOARD IS FULL

int FullBoard( char board[][10] );

//IT WILL DISPLAY VICTORY MSG WHEN A PLAYE WIN

void PlayerWin ( playerInfo activePlayer );

//TO RESTART THE GAME OR BOARD

int restart ( char board[][10] );

int main()

playerInfo playerOne, playerTwo;

char board[9][10];

int trueWidth = 7;

int trueLength = 6;
//declare 4 variables intiliazie krvay ha

// drop choice Represents the column where the player chooses to drop their game piece.

//win indicates wather a player win or not

//full indicate board is full or not

//player choice to play agian or not

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';

//These variables will be used to track the game state and player choices
during the game loop.

full = 0;

win = 0;

again = 0;

//fucntion call

DisplayBoard( board );

do

//Invokes the PlayerDrop function, allowing Player One to choose a column

//to drop their game piece. The chosen column is stored in the dropChoice variable.

dropChoice = PlayerDrop( board, playerOne );

//function to handle placing Player One's game piece in the chosen column.

CheckBellow( board, playerOne, dropChoice );

//it will display board

DisplayBoard( board );

win = CheckFour( board, playerOne );


//Checks if Player One has connected four pieces in a

//row by calling the CheckFour function. The result is stored in the win variable.

if ( win == 1 )

PlayerWin(playerOne);

again = restart(board);

if (again == 2)

break;

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;

//it ensure that player slected a column with in 1 to 7

//and that cloumn is not already full

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;

void CheckBellow ( char board[][10], playerInfo activePlayer, int dropChoice )

{
int length, turn;

length = 6;

turn = 0;

do

//Checks if the current position in the column is unoccupied (neither 'X' nor 'O').

if ( board[length][dropChoice] != 'X' && board[length][dropChoice] != 'O' )

//Sets the current position in the column to the player's ID ('X' or 'O').

board[length][dropChoice] = activePlayer.playerID;

turn = 1;

else

--length;

//Sets turn to 1, indicating that the piece has been successfully dropped.

}while ( turn != 1 );

//i an ix are loop counters.

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;

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 )

//digonal check up and left

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;
}

//horizontally to the left

if( board[i][ix] == XO &&

board[i][ix-1] == XO &&

board[i][ix-2] == XO &&

board[i][ix-3] == XO )

win = 1;

// vertically upside

if( board[i][ix] == XO &&

board[i-1][ix] == XO &&

board[i-2][ix] == XO &&

board[i-3][ix] == XO )

win = 1;

//diognal up and right

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;

//horizontally to right

if ( board[i][ix] == XO &&

board[i][ix+1] == XO &&

board[i][ix+2] == XO &&

board[i][ix+3] == XO )
{

win = 1;

return win;

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;

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;

You might also like