Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Connect Four is a two-player board game in which the players alterantely drop co

ID: 3707668 • Letter: C

Question

Connect Four is a two-player board game in which the players alterantely drop colored disks into a seven-column, six-row vertically suspended grid, as shown below.

The objective of the game is to connect four same-colored disks in a row, a column, or a diagonal before your opponent can do likewise. The program prompts two players to drop a red or yellow disk alternately. Whenever a disk is dropped, the program redisplays the board on the console and determines the status of the game (win, lose, or continue). Here is a sample run:

| | | | | | | |

| | | | | | | |

| | | | | | | |

| | | | | | | |

| | | | | | | |

| | | | | | | |

| | | | | | | |

Drop a red disk at column (0-6): 0 ? Enter

| | | | | | | |

| | | | | | | |

| | | | | | | |

| | | | | | | |

| | | | | | | |

| | | | | | | |

|R | | | | | | |

Explanation / Answer

//The program below shows a small demo program for the connect 4 game , whcih may require some modification //as according to the requirement , though the basic concept for the connect 4 game is intact.The program has got //comments for the better understanding which have been inspired from an already available program written.

#include<iostream>

#include <string>

using namespace std;

void display();

bool check(int a, int b);

int drop(int b, char player);

char place[6][7];//available for whole program

int main()

{

for(int a =0;a <= 5; a++){

for(int b = 0; b<=6; b++)

place[a][b] = '|';

}

display();//This will display the board for the first time

int hold;//Will house user row choice

int hold2 = 0;//will hold drop value

int charsPlaced = 0;//Number of peices dropped so can end game if a draw

bool gamewon = false;//Will be changed to true when game is won and will exit while loop

char player = 15;//start as player 2 will change back to player 1

while(!gamewon){//will stop when game is won, ! means NOT makes the oppisite be checked

if(hold2 != -1){//check if there was a error in the last drop

if(player == 15){//if player 2 lasted dropped a piece so its player 1s turn

cout<<"player 1 drop where?";

player = 254;//char of players piece

}

else{

cout<<"player 2 drop where?";

player = 15;//char of player piece

}

}

while(true){//will run untill 'break;'

if(charsPlaced == 42) break;//if draw

cin>>hold;//get user input

hold--;//take off 1 to account for arrays starting at 0 not 1

if(hold <=6 && hold>= 0) break;//if within valid range stop loop

else cout<< " please enter a value between 1 and 7 :";

if (cin.fail()) //catch a non number

{

cin.clear();

char c;

cin>>c;

}

}

if(charsPlaced == 42) break;//if the match is draw

hold2 = drop(hold,player);//drop the player store the row in hold2

if(hold2 == -1) cout<<"Column is full Please enter another number between 1 and 7:";//if error -1 row is full

else{

gamewon = check(hold2,hold);//check if game is run

charsPlaced ++;//another character has been succesfully placed

system("cls");//This clears the screen works with windows, not nesscery to run game

display();//displayed updated board

}

}

system("cls");//this clears the screen

if(charsPlaced == 42){//if draw

cout<<"No winner, Game was draw ";

system("pause");

return 0;

}

if(player == 15)//if won by player 2

cout<<"gamewon by : player 2 ";

else cout<<"gamewon by : player 1 ";

return 0;

}

void display(){

cout<<" 1 2 3 4 5 6 7 ";

for(int a = 0; a<= 5; a++)

{

for(int b =0; b <= 6; b++) cout<<char(218)<<char(196)<<char(191)<<" ";

cout<<' ';

for(int b =0; b <= 6; b++) cout<<char(179)<<place[a][b]<<char(179)<<" ";

cout<<' ';

for(int b =0; b <= 6; b++) cout<<char(192)<<char(196)<<char(217)<<" ";

cout<<' ';

}

}

bool check(int a, int b){

int vertical = 1;//(|)

int horizontal = 1;//(-)

int diagonal1 = 1;//()

int diagonal2 = 1;//(/)

char player = place[a][b];

int i;//vertical

int ii;//horizontal

//check for vertical(|)

for(i = a +1;place[i][b] == player && i <= 5;i++,vertical++);//Check down

for(i = a -1;place[i][b] == player && i >= 0;i--,vertical++);//Check up

if(vertical >= 4)return true;

//check for horizontal(-)

for(ii = b -1;place[a][ii] == player && ii >= 0;ii--,horizontal++);//Check left

for(ii = b +1;place[a][ii] == player && ii <= 6;ii++,horizontal++);//Check right

if(horizontal >= 4) return true;

//check for diagonal 1 ()

for(i = a -1, ii= b -1;place[i][ii] == player && i>=0 && ii >=0; diagonal1 ++, i --, ii --);//up and left

for(i = a +1, ii = b+1;place[i][ii] == player && i<=5 && ii <=6;diagonal1 ++, i ++, ii ++);//down and right

if(diagonal1 >= 4) return true;

//check for diagonal 2(/)

for(i = a -1, ii= b +1;place[i][ii] == player && i>=0 && ii <= 6; diagonal2 ++, i --, ii ++);//up and right

for(i = a +1, ii= b -1;place[i][ii] == player && i<=5 && ii >=0; diagonal2 ++, i ++, ii --);//up and left

if(diagonal2 >= 4) return true;

return false;

}

int drop(int b, char player){

if(b >=0 && b<= 6)

{

if(place[0][b] == '|'){

int i;

for(i = 0;place[i][b] == '|';i++)

if(i == 5){place[i][b] = player;

return i;}

i--;

place[i][b] =player;

return i;

}

else{

return -1;

}

}

else{

return -1;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote