You are on page 1of 9

//Connect-4 game

//Made by Mads Foged


#include <iostream>
#include <stdio.h> // for the jumping statement to work
using namespace std;

//How big do you want the connect-4 to be? Standard is 6 rows and 7 cols
int const ROWS = 6; //Standard: 6
int const COLS = 7; //Standard: 7 More then 9 COLS wouldn't show more then 9
numbers and putting in 10 still works
int const connect = 4; //Standard; 4 If you wanna to have to have more then 4
connections to win!!

// Making it a global scope so that we function works


int const row = ROWS +2; //adds two extra to creat the board
int const col = COLS +2; //adds two extra to creat the board
char gamearray[row][col]={}; //makes the GAME ARRAY
//Put in whatever simbol you like - All tested simbols works
char Player1 = 'x'; //Standard: x
char Player2 = 'o'; //Standard: o

//Function to check is col is full or not


int check (int colnr){
if(gamearray[1][colnr] == '*'){ //spot not taken
return(0);
}
else {
return (1); //spot taken

}
}

//If col is not full this will find the lowest available stop
int checkrow (int colnr){
int rownr = 0;
while(gamearray[row-rownr-2][colnr]!='*'){
rownr++;
}

return row-rownr-2;
}

//To display the board


int displayarray(){
cout << string(50, '\n'); //Getting a "new" window
//Display the ARRAY
int width = 3; //width of the gameboards spots
for(int r=0;r<row;r++){
cout << endl;
cout.width(width);
for(int c=0;c<col;c++){
cout.width(width);
cout<< gamearray[r][c];
}
}
cout << endl;
return 0;
}

//Function that checks for a winner and a tie


//It checks horizontal,vertical and diagonal
//Its checks ALL positions everytime.. Note: Better solution > just checking the
closes connections would save processing time but as Josh would have said: "pro
cessors capacity is so hughes anyways"
int checkforwinner(){
//horizontal check:
int one=0; //Counter for Player one
int two=0; //Counter for Player two
int tie=0; //Counter for tie
//For loops that runs through the array in a horizontal way
for(int r=1;r<row-1;r++){
one =0;
two =0;
for(int c=1;c<col-1;c++){
//If it founds one of player 1s game bricks
if(gamearray[r][c]==Player1){
one++; //Adds one to player 1
tie++; //Adds one to fie
two = 0; //If it finds one of player 1 it mea
ns that player 2 cant win right here
if(one==connect){ //Connect is the number of connectio
ns you have to win
return 1; //Returns a 1 if player one is winni
ng
}
}
if(gamearray[r][c]==Player2){
two++; //Adds one to player 2
tie++; //Adds one to fie
one = 0; //If it finds one of player 2 it mea
ns that player 1 cant win right here
if(two==connect){ //Connect is the number of connectio
ns you have to win
return 1; //Returns a 1 if player one is winni
ng
}
}
if(gamearray[r][c]=='*'){ //if there is an empty it means that
no one can win right here
one=0;
two=0;
}

}
}
if(tie==ROWS*COLS){ //If it is a tie
return 2; //returns a 2

//vertical check
//Almost the same as the horizontal check except the two for loops have switched
places
one=0;
two=0;
for(int c=1;c<col-1;c++){
one =0;
two =0;
for(int r=1;r<row-1;r++){
if(gamearray[r][c]==Player1){
one++;
two = 0;
if(one==connect){
return 1;
}
}
if(gamearray[r][c]==Player2){
two++;
one = 0;
if(two==connect){
return 1;
}
}
if(gamearray[r][c]=='*'){
one=0;
two=0;
}

}
}

//diagonal check going from top to down


one=0;
two=0;
for(int c=1;c<col;c++){ //For loop for looking in the cols
for(int r=1;r<row;r++){ //For loop for looking in the rows
one = 0;
two = 0;
//For loop that makes it go diagonal and stops when it hits the
outside of the gamingboard
for(int d = 0;gamearray[r+d][c+d]==Player1 || gamearray[r+d][c+d
]==Player2 || gamearray[r+d][c+d]=='*';d++){
//cout << "(" <<r+d << "," << c+d << ")" << endl; //If you wa
nna see its checking pattan
//Checks for connections for player 1
if(gamearray[r+d][c+d]==Player1){
one++;
two = 0;
if(one==connect){
return 1;
}
}
//Checks for connections for player 1
if(gamearray[r+d][c+d]==Player2){
two++;
one = 0;
if(two==connect){
return 1;
}
}
//Checks if there is an empty it means that no one can win r
ight here
if(gamearray[r+d][c+d]=='*'){
one=0;
two=0;
}

//diagonal check going from down to top


//Almost the same as the other diagonals check it just uess r-d instead of r+d
one=0;
two=0;
for(int c=1;c<col;c++){
for(int r=1;r<row;r++){
one = 0;
two = 0;
for(int d = 0;gamearray[r-d][c+d]==Player1 || gamearray[r-d][c+d
]==Player2 || gamearray[r-d][c+d]=='*';d++){
// cout << "(" <<r-d << "," << c+d << ")" << endl;
if(gamearray[r-d][c+d]==Player1){
one++;
two = 0;
if(one==connect){
return 1;
}
}
if(gamearray[r-d][c+d]==Player2){
two++;
one = 0;
if(two==connect){
return 1;
}
}
if(gamearray[r-d][c+d]=='*'){
one=0;
two=0;
}

return 0; //Returns 0 if it didnt find a winner or a tie


}

int main(){

start: //Jumping statement if the user wann


a play again
//Creats the first gameboard
cout << string(50, '\n');
for(int r=1;r<row;r++){ //put in '*' in all the spots
for(int c=0;c<col;c++){
gamearray[r][c]='*';
}
}

for(int r=0;r<row-1;r++){ //put in '|' in all the spots


gamearray[r][0]='|';
}
for(int r=0;r<row-1;r++){ //put in '|' in all the spots
gamearray[r][col-1]='|';
}
for(int r=1;r<row;r++){ //put in '-' in all the spots
gamearray[row-1][r]='-';
}
for(int c=1;c<col-1;c++){ //put in 'numbers'
gamearray[0][c]=c+48;
}

gamearray[row-1][0]='+'; //Put in +
gamearray[row-1][col-1]='+';
cout << endl;

//Game code
int poneinput; //Input of player on
e
int ptwoinput; //Input of player to
w
//The welcome to the user
cout << "Welcome to a game of Connect-4!" << endl;
cout << "If you dont the rules of the game: Ask google!" << endl;
cout << "Player one is: " << Player1 << endl; //Telling what simbo
l user one got
cout << "Player two is: " << Player2 << endl; //Telling what simbo
l user one got
int width = 3; //width of the gameboard
//Displays the gameboard the first time
for(int r=0;r<row;r++){
cout << endl;
cout.width(width);
for(int c=0;c<col;c++){
cout.width(width);
cout<< gamearray[r][c];
}
}
cout << endl;

//While loop that displays the game - it is allways on..


while(1){
//Player one has to a col
cout << "Player one select from 1-7" << endl;
//Checks if player one put in an number that on the gameboard
//Else he/she gets to try again
tryagain:
cin >> poneinput;
if(poneinput<=0 || poneinput >col-2){
cout << "You didnt put in a valid number! try again..:" << endl;
goto tryagain;
}
//Checks in col is full
if(check(poneinput)){
cout << "Spot already taken.. try again:" << endl;
goto tryagain;
}
//if the col is not full and the players input was an accepted number it
gets put into play
gamearray[checkrow(poneinput)][poneinput] = Player1;
//Displays the gameboard again after the new input
displayarray();
//If player one won
if(checkforwinner()==1){
cout<< "We have a winner!! Congratulation to Player one!!" << en
dl;
break;
}
//if tie
else if(checkforwinner()==2){
cout<< "It is a tie.." << endl;
break;
}

//The same just for player two


cout << "Player two select from 1-7" << endl;
tryagain1:
cin >> ptwoinput;
if(ptwoinput<=0 || ptwoinput >col-2){
cout << "You didnt put in a valid number! try again..:" << endl;
goto tryagain1;
}
if(check(ptwoinput)){
cout << "Spot already taken.. try again:" << endl;
goto tryagain;
}
gamearray[checkrow(ptwoinput)][ptwoinput] = Player2;
displayarray();
if(checkforwinner()==1){
cout<< "We have a winner!! Congratulation to Player two!!" << en
dl;
break;
}
else if(checkforwinner()==2){
cout<< "It is a tie.." << endl;
break;
}

}
//After end game, the user get the option to play again or quit
char yesno;
cout << "Wanna play again? Y/N" << endl;
cin >> yesno;
if(yesno== 'y' || yesno == 'Y'){
goto start;
}
else{
cout <<"Okay then.. have a great day!";
return 0;
}

return 0;
}

You might also like