#include “stdafx.h”
#include
using namespace std;
void menu();
void refresh( bool gameover ); //Used to be grid, renamed it to more appropriate
void highscore(); //How is highscore going to work in connect four?
void exit();
void turn(); //Calculates a pebble dropping down to empty slot
int grid[7][6]; //Holds each players pebbles in 2d grid
int whichturn; //Remembers which turn it is
void checkwin( int lastturn ); //Checks if any player won
void playerwins( int player ); //Performs actions when someone does win
void main()
{
menu();
}
void menu()
{
int user_option;
cout<<" Welcome to ConnectFour! This is the main menu! Enter the option of your choice to continue!";
cout<<endl;
cout<<“1. Start new game!”;
cout<<endl;
cout<<“2. View high scores”;
cout<<endl;
cout<<“3. Exit game and return to desktop”;
cout<<endl;
cout<<endl;
cout<<"Please enter your choice: ";
//This fuction shall now perform actions needed to set up a new game.
for ( int i=0; i<8; i++ )
{
for ( int j=0; j<7; j++)
{
grid[i][j]=0;
}
}
//All grids emptied
whichturn = 1; //Player 1 always starts first
cin>>user_option;
if (user_option == 1)
{
refresh(false);
}
if (user_option == 2)
{
highscore();
}
if (user_option == 3)
{
exit();
}
}
void refresh( bool gameover )
{
int i;
/*char columnarray1[7]={’"’,’"’,’"’,’"’,’"’,’"’,’"’};
char columnarray2[7]={’"’,’"’,’"’,’"’,’"’,’"’,’"’};
char columnarray3[7]={’"’,’"’,’"’,’"’,’"’,’"’,’"’};
char columnarray4[7]={’"’,’"’,’"’,’"’,’"’,’"’,’"’};
char columnarray5[7]={’"’,’"’,’"’,’"’,’"’,’"’,’"’};
char columnarray6[7]={’"’,’"’,’"’,’"’,’"’,’"’,’"’};
char columnarray7[7]={’"’,’"’,’"’,’"’,’"’,’"’,’"’};
*/
cout<<" ConnectFour, player “<<whichturn<<” turn."<<endl;
for(i=1; i<7; i++)
{
cout<<"| “;
for ( int j=1; j<8; j++ )
{
if ( grid[j][i] == 0 ) cout<<’”’; //if grid[x][y] is zero, slot is empty
if ( grid[j][i] == 1 ) cout<<‘X’; //slot is owned by player1 pebble
if ( grid[j][i] == 2 ) cout<<‘O’; //slot is owned by player2 pebble
cout<<" | “;
//cout<<”| “<<columnarray1[i]<<” | “<<columnarray2[i]<<” | “<<columnarray3[i]<<” | “<<columnarray4[i]<<” | “<<columnarray5[i]<<” | “<<columnarray6[i]<<” | “<<columnarray7[i]<<” | \n -------------------------------------------------"<<endl;
}
cout<<endl;
}
if ( !gameover ) cout<<"| 1 | 2 | 3 | 4 | 5 | 6 | 7 |"<<endl<<endl<<"Player 1 = X || Player 2 = O || Empty Slot = “”<<endl;
if (gameover) cout<<endl;
if ( !gameover ) turn(); //gameover input added so you can draw the grid without prompting players to play
}
void turn()
{
int slot;
cin>>slot; //asks for a number from 1 to 7
if ( slot<1 || slot>7 )
{
cout<<“Invalid row. Try again!”<<endl;
turn();
return;
}
for ( int i=6; i>0; i-- )
{
if ( grid[slot][i] == 0 )
{
if ( whichturn == 1 )
{
grid[slot][i] = 1;
whichturn = 2;
checkwin(1);
return;
}
if ( whichturn == 2 )
{
grid[slot][i] = 2;
whichturn = 1;
checkwin(2);
return;
}
}
}
cout<<“This row is full. Try again!”<<endl;
turn();
}
void checkwin( int lastturn )
{
int hit=0;
//Checking four vertical slots
for ( int i=1; i<8; i++ )
{
for ( int j=1; j<7; j++ )
{
if ( grid[i][j]==lastturn )
{
hit++;
if ( hit==4 )
{
playerwins(lastturn);
return;
}
}
else hit=0;
}
hit=0;
}
//Checking four horizontal slots
for ( int i=1; i<7; i++ )
{
for ( int j=1; j<8; j++ )
{
if ( grid[j][i]==lastturn )
{
hit++;
if ( hit==4 )
{
playerwins(lastturn);
return;
}
}
else hit=0;
}
hit=0;
}
//Checking four horizontal slots from top-left to bottom-right
for ( int i=3; i>0; i-- )
{
for ( int j=1; j<7; j++ )
{
if ( grid[j][i+(j-1)]==lastturn )
{
hit++;
if ( hit==4 )
{
playerwins(lastturn);
return;
}
}
else hit=0;
if ( i+(j-1) == 6 ) break;
}
hit=0;
}
for ( int i=2; i<5; i++ )
{
for ( int j=1; j<7; j++ )
{
if ( grid[i+(j-1)][j]==lastturn )
{
hit++;
if ( hit==4 )
{
playerwins(lastturn);
return;
}
}
else hit=0;
if ( i+(j-1) == 7 ) break;
}
hit=0;
}
//Checking four horizontal slots from bottom-left to top-right
for ( int i=4; i<7; i++ )
{
for ( int j=1; j<7; j++ )
{
if ( grid[j][i-(j-1)]==lastturn )
{
hit++;
if ( hit==4 )
{
playerwins(lastturn);
return;
}
}
else hit=0;
if ( i-(j-1) == 1 ) break;
}
hit=0;
}
for ( int i=2; i<5; i++ )
{
for ( int j=6; j>0; j-- )
{
if ( grid[i-(j-6)][j]==lastturn )
{
hit++;
if ( hit==4 )
{
playerwins(lastturn);
return;
}
}
else hit=0;
if ( i-(j-6) == 7 ) break;
}
hit=0;
}
refresh(false);
}
void playerwins( int player ) //someone won!
{
refresh(true);
cout<<“I be damned! Player “<<player<<” won!”<<endl;
cout<<“Type any key then press enter to start again.”<<endl;
char decision;
cin>>decision;
main();
}
void highscore()
{
}
void exit()
{
}