SlideShare a Scribd company logo
1 of 5
Download to read offline
/***********************************************
* Author: NAjAM AMjAD BuTT **** **
* Assignment of SIR NUMAN ** ** **
* CONNECT 4 GAME ** ** **
* Source: C++ ** ** **
* creation: 02/FEB/2015 ** ****
************************************************/
#include<iostream.h>
#include <string.h>
void display();
bool check(int a, int b);
int drop(int b, char player);
char place[6][7];//available for whole program
int main()
{
int choice;
do{
cout<<"********************************************
******"<<endl;
cout<<"Press 1 for Play GAME AGAIN "<<endl<<"Press
2 for EXIT GAME"<<endl;
cout<<"********************************************
******n"<<endl;
cout<<"Enter choice for operation of an array = ";
cin>>choice;
switch(choice)
{
case 1:{
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 chang
e back 2 player 1
for(int a =0;a <= 5; a++)
{ //fill place with whitespac
e
for(int b = 0; b<=6; b++)
place[a][b] = ' ';
}
display();//Displays for first time so players
can see the board
while(!gamewon){//will stop when game is w
on, ! means NOT makes the oppisite be checked
if(hold2 != -1){//check if there was a erro
r in the last drop
if(player == 15){//if player 2 lasted d
ropped a piece so its player 1s turn
cout<<"player 1 drop where?";
player = 254;//char of players piec
e
}
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 arra
ys starting at 0 not 1
if(hold <=6 && hold>= 0) break;//if wit
hin valid range stop loop
else cout<< "nplease enter a value be
tween 1 and 7 :";//ask for input and loop again
if (cin.fail()) //catch a non number
{ //
cin.clear(); //Stops cin try
ing to put its value in to hold
char c; //Try entering a n
on number without this, 2 see what this does
cin>>c; //
} //Catch a non n
umber
}
if(charsPlaced == 42) break;//if draw
hold2 = drop(hold,player);//drop the player
store the row in hold2
if(hold2 == -1) cout<<"Colomn is fullnPle
ase enter another number between 1 and 7:";//if er
ror -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 wo
rks 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 drawn";
system("pause");
return 0;
}
if(player == 15)//if won by player 2
cout<<"*************GAME WINNER IS : PLAYER
2*************n";
else
cout<<"*************GAME WINNER IS : PLAYER 1**
***********n";
system("pause");//pauses before exit so player
s can see who won, works with windows
//return 0;//Exit application
}break;
case 2: {
cout<<"GAME EXIT "<<endl;
}break;
}}while(choice!=2);
}
void display() // DISPLAY FUNCTION
{
cout<<" 1 2 3 4 5 6 7n";
for(int a = 0; a<= 5; a++)
{
for(int b =0; b <= 6; b++) cout<<char(218)<
<char(196)<<char(191)<<" ";
cout<<'n';
for(int b =0; b <= 6; b++) cout<<char(179)<
<place[a][b]<<char(179)<<" ";
cout<<'n';
for(int b =0; b <= 6; b++) cout<<char(192)<
<char(196)<<char(217)<<" ";
cout<<'n';
}
}
bool check(int a, int b)// CHECK FUNCTION
{
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 ++);//dow
n 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 ++);//u
p 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)// DROP FUNCTION
{
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;
}
}

More Related Content

What's hot (20)

Linear queue
Linear queueLinear queue
Linear queue
 
Python Developer's Daily Routine
Python Developer's Daily RoutinePython Developer's Daily Routine
Python Developer's Daily Routine
 
Electricitybillsystemreport
ElectricitybillsystemreportElectricitybillsystemreport
Electricitybillsystemreport
 
COSTEP Student Loan Brochure
COSTEP Student Loan BrochureCOSTEP Student Loan Brochure
COSTEP Student Loan Brochure
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Clang2018 class3
Clang2018 class3Clang2018 class3
Clang2018 class3
 
Misha Bilenko @ Microsoft
Misha Bilenko @ MicrosoftMisha Bilenko @ Microsoft
Misha Bilenko @ Microsoft
 
CQL 实现
CQL 实现CQL 实现
CQL 实现
 
Circular queue
Circular queueCircular queue
Circular queue
 
Effecting Pure Change - How anything ever gets done in functional programming...
Effecting Pure Change - How anything ever gets done in functional programming...Effecting Pure Change - How anything ever gets done in functional programming...
Effecting Pure Change - How anything ever gets done in functional programming...
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)
 
Railwaynew
RailwaynewRailwaynew
Railwaynew
 
dplyr
dplyrdplyr
dplyr
 
Groovy
GroovyGroovy
Groovy
 
Xstartup
XstartupXstartup
Xstartup
 
Who will play_with_me_2002
Who will play_with_me_2002Who will play_with_me_2002
Who will play_with_me_2002
 
Quiz using C++
Quiz using C++Quiz using C++
Quiz using C++
 
Sol 1
Sol 1Sol 1
Sol 1
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
 

Similar to COnnect4Game

RussianRouletteProgram
RussianRouletteProgramRussianRouletteProgram
RussianRouletteProgramAmy Baxter
 
DiceSimulatorProgram
DiceSimulatorProgramDiceSimulatorProgram
DiceSimulatorProgramAmy Baxter
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfanithareadymade
 
The following is my code for a connectn program. When I run my code .pdf
The following is my code for a connectn program. When I run my code .pdfThe following is my code for a connectn program. When I run my code .pdf
The following is my code for a connectn program. When I run my code .pdfeyelineoptics
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdffonecomp
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfanjandavid
 
#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdfsinghanubhav1234
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfarmcomputers
 
Assignement c++
Assignement c++Assignement c++
Assignement c++Syed Umair
 
a) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdfa) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdfanuradhasilks
 
Arduino coding class
Arduino coding classArduino coding class
Arduino coding classJonah Marrs
 
TilePUzzle class Anderson, Franceschi public class TilePu.docx
 TilePUzzle class Anderson, Franceschi public class TilePu.docx TilePUzzle class Anderson, Franceschi public class TilePu.docx
TilePUzzle class Anderson, Franceschi public class TilePu.docxKomlin1
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfcalderoncasto9163
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfasif1401
 

Similar to COnnect4Game (20)

RussianRouletteProgram
RussianRouletteProgramRussianRouletteProgram
RussianRouletteProgram
 
Tic tac toe
Tic tac toeTic tac toe
Tic tac toe
 
Code em Poker
Code em PokerCode em Poker
Code em Poker
 
AI For Texam Hold'em poker
AI For Texam Hold'em pokerAI For Texam Hold'em poker
AI For Texam Hold'em poker
 
DiceSimulatorProgram
DiceSimulatorProgramDiceSimulatorProgram
DiceSimulatorProgram
 
Here is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdfHere is the code for youimport java.util.Scanner; import java.u.pdf
Here is the code for youimport java.util.Scanner; import java.u.pdf
 
The following is my code for a connectn program. When I run my code .pdf
The following is my code for a connectn program. When I run my code .pdfThe following is my code for a connectn program. When I run my code .pdf
The following is my code for a connectn program. When I run my code .pdf
 
RandomGuessingGame
RandomGuessingGameRandomGuessingGame
RandomGuessingGame
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
 
Assignement c++
Assignement c++Assignement c++
Assignement c++
 
cosc 281 hw2
cosc 281 hw2cosc 281 hw2
cosc 281 hw2
 
a) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdfa) In the code, board is initialized by reading an input file. But y.pdf
a) In the code, board is initialized by reading an input file. But y.pdf
 
Arduino coding class
Arduino coding classArduino coding class
Arduino coding class
 
TilePUzzle class Anderson, Franceschi public class TilePu.docx
 TilePUzzle class Anderson, Franceschi public class TilePu.docx TilePUzzle class Anderson, Franceschi public class TilePu.docx
TilePUzzle class Anderson, Franceschi public class TilePu.docx
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 

COnnect4Game

  • 1. /*********************************************** * Author: NAjAM AMjAD BuTT **** ** * Assignment of SIR NUMAN ** ** ** * CONNECT 4 GAME ** ** ** * Source: C++ ** ** ** * creation: 02/FEB/2015 ** **** ************************************************/ #include<iostream.h> #include <string.h> void display(); bool check(int a, int b); int drop(int b, char player); char place[6][7];//available for whole program int main() { int choice; do{ cout<<"******************************************** ******"<<endl; cout<<"Press 1 for Play GAME AGAIN "<<endl<<"Press 2 for EXIT GAME"<<endl; cout<<"******************************************** ******n"<<endl; cout<<"Enter choice for operation of an array = "; cin>>choice; switch(choice) { case 1:{ 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
  • 2. char player = 15;//start as player 2 will chang e back 2 player 1 for(int a =0;a <= 5; a++) { //fill place with whitespac e for(int b = 0; b<=6; b++) place[a][b] = ' '; } display();//Displays for first time so players can see the board while(!gamewon){//will stop when game is w on, ! means NOT makes the oppisite be checked if(hold2 != -1){//check if there was a erro r in the last drop if(player == 15){//if player 2 lasted d ropped a piece so its player 1s turn cout<<"player 1 drop where?"; player = 254;//char of players piec e } 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 arra ys starting at 0 not 1 if(hold <=6 && hold>= 0) break;//if wit hin valid range stop loop else cout<< "nplease enter a value be tween 1 and 7 :";//ask for input and loop again if (cin.fail()) //catch a non number { // cin.clear(); //Stops cin try ing to put its value in to hold char c; //Try entering a n on number without this, 2 see what this does cin>>c; //
  • 3. } //Catch a non n umber } if(charsPlaced == 42) break;//if draw hold2 = drop(hold,player);//drop the player store the row in hold2 if(hold2 == -1) cout<<"Colomn is fullnPle ase enter another number between 1 and 7:";//if er ror -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 wo rks 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 drawn"; system("pause"); return 0; } if(player == 15)//if won by player 2 cout<<"*************GAME WINNER IS : PLAYER 2*************n"; else cout<<"*************GAME WINNER IS : PLAYER 1** ***********n"; system("pause");//pauses before exit so player s can see who won, works with windows //return 0;//Exit application }break; case 2: { cout<<"GAME EXIT "<<endl; }break; }}while(choice!=2);
  • 4. } void display() // DISPLAY FUNCTION { cout<<" 1 2 3 4 5 6 7n"; for(int a = 0; a<= 5; a++) { for(int b =0; b <= 6; b++) cout<<char(218)< <char(196)<<char(191)<<" "; cout<<'n'; for(int b =0; b <= 6; b++) cout<<char(179)< <place[a][b]<<char(179)<<" "; cout<<'n'; for(int b =0; b <= 6; b++) cout<<char(192)< <char(196)<<char(217)<<" "; cout<<'n'; } } bool check(int a, int b)// CHECK FUNCTION { 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
  • 5. and left for(i = a +1, ii = b+1;place[i][ii] == player && i<=5 && ii <=6;diagonal1 ++, i ++, ii ++);//dow n 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 ++);//u p 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)// DROP FUNCTION { 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; } }