rendered paste body
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
const int ROW = 7;
const int COL = 8;
struct GameState
{
char board[ROW][COL];
char symbol;
char nextTurn;
};
void drawHeader();
void chooseOption();
void computerMove(GameState&);
void yourMove();
void initialiseBoard(char[][COL]);
int playerMove(char[][COL], char, int);
void drawBoard(char[][COL]);
bool saveGame(GameState, char []);
bool loadGame(GameState&, char []);
int checkWinner(GameState&);
int main()
{
drawHeader();
chooseOption();
return 0;
}
void chooseOption()
{
bool unbroken = true;
char option;
char suboption;
while(unbroken == true)
{
cout<<"*******************************************************************************"<<endl;
cout<<"**************************Chooose an option************************************"<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<"* a) Play against the computer *"<<endl;
cout<<"* b) Play against another player *"<<endl;
cout<<"* c) Load game *"<<endl;
cout<<"* d) Quit *"<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<endl;
cout<<"Enter your choice: ";
cin>>option;
GameState gs;
if(option != 'A' && option != 'a' && option != 'B' && option != 'b' && option != 'C' && option != 'c' && option != 'D' && option != 'd')
{
cout<<"Invalid option, please try again!!"<<endl;
}
if(option == 'a' || option == 'A')
{
cout<<"You are now playing vs the computer, your symbol is: 'O'. "<<endl;
char query = '?';
bool isSaved = false;
bool breaker2 = false;
char filename[20];
int move = -50;
bool continueComp = true;
bool isClear = true;
int winner = 0;
for(int x = 0; x < 7; x++)
{
for(int y = 0; y < 8; y++)
{
if(gs.board[x][y] == 'O' || gs.board[x][y] == 'X' )
{
isClear = false;
}
}
}
if(isClear == true)
{
initialiseBoard(gs.board);
}
drawBoard(gs.board);
while(continueComp == true)
{
move = playerMove(gs.board, gs.symbol = 'O', 1);
winner = checkWinner(gs);
if(winner == 1)
{
cout<<"Congratulations!! You've beaten the computer !!"<<endl;
continueComp = false;
}
if(winner == 3)
{
cout<<"Draw!!"<<endl;
continueComp = false;
}
gs.nextTurn = 1;
if(move == 0)
{
while(breaker2 == false)
{
cout<<"Do you wish to play again (y/n)?: ";
cin>>query;
cout<<endl<<endl;
if(query == 'y')
{
initialiseBoard(gs.board);
breaker2 = true;
}
if(query == 'n')
{
cout<<endl<<"Do you wish to save your game (y/n)?";
cin>>query;
if(query == 'y')
{
cout<<"Enter the file name (including extention .txt) for the game to be saved: ";
cin>>filename;
isSaved = saveGame(gs, filename);
if(isSaved == true)
{
cout<<endl<<endl<<"Game saved successfully!!"<<endl<<endl;
}
while(isSaved == false)
{
cout<<"ERROR: Failed to save under that file name. Please try again or type 'exit' to quit."<<endl;
cout<<"Enter the file name (including extention .txt) for the game to be saved: ";
cin>>filename;
isSaved = saveGame(gs, filename);
if(isSaved == true)
{
cout<<endl<<endl<<"Game saved successfully!!"<<endl<<endl;
}
if(filename == "exit")
{
isSaved = true;
}
}
initialiseBoard(gs.board);
}
breaker2 = true;
initialiseBoard(gs.board);
}
if(query != 'n' && query != 'y')
{
cout<<"Please enter a valid answer."<<endl;
}
}
continueComp = false;
}
if(move != 0)
{
drawBoard(gs.board);
computerMove(gs);
drawBoard(gs.board);
checkWinner(gs);
if(winner == 2)
{
cout<<"Sorry... you have been beaten by the computer."<<endl;
continueComp = false;
}
if(winner == 3)
{
cout<<"Draw!!"<<endl;
continueComp = false;
}
}
}
option == 'd';
}
if(option == 'b' || option == 'B')
{
cout<<"You have chosen to play vs another player..."<<endl;
cout<<"Player 1 = O"<<endl;
cout<<"Player 2 = X"<<endl;
char query = '?';
bool isSaved = false;
bool breaker2 = false;
char filename[20];
int move = -50;
bool continueGame = true;
bool isClear = true;
int winner = 0;
for(int x = 0; x < 7; x++)
{
for(int y = 0; y < 8; y++)
{
if(gs.board[x][y] == 'O' || gs.board[x][y] == 'X' )
{
isClear = false;
}
}
}
if(isClear == true)
{
initialiseBoard(gs.board);
gs.nextTurn = '1';
}
drawBoard(gs.board);
if(gs.nextTurn == '2')
{
move = playerMove(gs.board, gs.symbol = 'X', 2);
drawBoard(gs.board);
checkWinner(gs);
if(winner == 2)
{
cout<<"Player 2 wins!!"<<endl;
move = 0;
}
if(winner == 3)
{
cout<<"Draw!!"<<endl;
move = 0;
}
}
if(move == 0)
{
gs.nextTurn == '1';
initialiseBoard(gs.board);
}
if(move != 0)
{
while(continueGame == true)
{
move = playerMove(gs.board, gs.symbol = 'O', 1);
gs.nextTurn = 1;
winner = checkWinner(gs);
if(winner == 1)
{
cout<<"Player 1 wins!!"<<endl;
continueGame = false;
}
if(winner == 3)
{
cout<<"Draw!!"<<endl;
continueGame = false;
}
if(move == 0)
{
while(breaker2 == false)
{
cout<<"Do you wish to play again (y/n)?: ";
cin>>query;
cout<<endl<<endl;
if(query == 'y')
{
initialiseBoard(gs.board);
breaker2 = true;
}
if(query == 'n')
{
cout<<endl<<"Do you wish to save your game (y/n)?";
cin>>query;
if(query == 'y')
{
cout<<"Enter the file name (including extention .txt) for the game to be saved: ";
cin>>filename;
isSaved = saveGame(gs, filename);
if(isSaved == true)
{
cout<<endl<<endl<<"Game saved successfully!!"<<endl<<endl;
}
while(isSaved == false)
{
cout<<"ERROR: Failed to save under that file name. Please try again or type 'exit' to quit."<<endl;
cout<<"Enter the file name (including extention .txt) for the game to be saved: ";
cin>>filename;
isSaved = saveGame(gs, filename);
if(isSaved == true)
{
cout<<endl<<endl<<"Game saved successfully!!"<<endl<<endl;
}
if(filename == "exit")
{
isSaved = true;
}
}
initialiseBoard(gs.board);
}
breaker2 = true;
initialiseBoard(gs.board);
}
if(query != 'n' && query != 'y')
{
cout<<"Please enter a valid answer."<<endl;
}
}
continueGame = false;
initialiseBoard(gs.board);
}
if(move != 0)
{
drawBoard(gs.board);
move = playerMove(gs.board, gs.symbol = 'X', 2);
drawBoard(gs.board);
gs.nextTurn = '2';
winner = checkWinner(gs);
if(winner == 2)
{
cout<<"Player 2 wins!!"<<endl;
continueGame = false;
}
if(winner == 3)
{
cout<<"Draw!!"<<endl;
continueGame = false;
}
if(move == 0)
{
while(breaker2 == false)
{
cout<<"Do you wish to play again (y/n)?: ";
cin>>query;
cout<<endl<<endl;
if(query == 'y')
{
initialiseBoard(gs.board);
breaker2 = true;
}
if(query == 'n')
{
cout<<endl<<"Do you wish to save your game (y/n)?";
cin>>query;
if(query == 'y')
{
cout<<"Enter the file name (including extention .txt) for the game to be saved: ";
cin>>filename;
isSaved = saveGame(gs, filename);
if(isSaved == true)
{
cout<<endl<<endl<<"Game saved successfully!!"<<endl<<endl;
}
while(isSaved == false)
{
cout<<"ERROR: Failed to save under that file name. Please try again or type 'exit' to quit."<<endl;
cout<<"Enter the file name (including extention .txt) for the game to be saved: ";
cin>>filename;
isSaved = saveGame(gs, filename);
if(isSaved == true)
{
cout<<endl<<endl<<"Game saved successfully!!"<<endl<<endl;
}
if(filename == "exit")
{
isSaved = true;
}
}
initialiseBoard(gs.board);
}
breaker2 = true;
initialiseBoard(gs.board);
}
if(query != 'n' && query != 'y')
{
cout<<"Please enter a valid answer."<<endl;
}
}
continueGame = false;
initialiseBoard(gs.board);
}
}
}
}
option == 'd';
}
if(option == 'c' || option == 'C')
{
bool isLoaded = false;
char filename[20];
cout<<"Enter the file name (including extention .txt) for the game to be loaded: ";
cin>>filename;
isLoaded = loadGame(gs,filename);
if(isLoaded == true)
{
cout<<"Game Loaded!!"<<endl<<endl;
}
}
if(option == 'd' || option == 'D')
{
cout<<"Do you wish to play again?(y/n) ";
cin>>suboption;
if(suboption == 'y')
unbroken = true;
else if(suboption == 'n')
{
cout<<"Thanks for playing connect four !! The game of the clever people !!"<<endl;
unbroken = false;
}
}
}
}
void drawHeader()
{
cout<<"*******************************************************************************"<<endl;
cout<<"****************************Connect Four***************************************"<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<"Welcome to the connect four computer game.You can choose to play against another"<<endl;
cout<<"player or against the computer(not implemented in this version). In this game "<<endl;
cout<<"the first player to position four of his/her pieces on a line will win. the "<<endl;
cout<<"line can be horizontal, vertical or at an angle, but the 4 pieces must be next "<<endl;
cout<<"to each other"<<endl;
cout<<"*******************************************************************************"<<endl;
cout<<endl<<endl;
}
void initialiseBoard(char board[][COL])
{
for(int x = 0; x < ROW; x++)
{
for(int y = 0; y < COL; y++)
{
board[x][y] = '.';
}
}
}
void computerMove(GameState& gs)
{
char computerMark = 'X';
int num = 0;
srand(time(NULL));
bool tryagain = true;
cout<<"Computer makes a move..."<<endl;
while(tryagain == true)
{
num = (rand() % (8 - 1)) + 1;
bool subBreaker = false;
bool changeCheck = false;
int initialRow = 6;
while(subBreaker == false && initialRow >= 0)
{
if(gs.board[initialRow][num - 1] != '.')
{
initialRow--;
}
if(gs.board[initialRow][(num - 1)] == '.')
{
gs.board[initialRow][(num - 1)] = computerMark;
changeCheck = true;
subBreaker = true;
tryagain = false;
}
}
if(changeCheck == false)
{
tryagain = true;
}
}
cout<<"Computer move: "<<num<<endl;
}
int playerMove(char board[][COL], char symbol, int pn)
{
int userInput = -1;
char query = '?';
bool isSaved = false;
bool breaker = false;
bool breaker2 = false;
char filename[20];
while(breaker == false)
{
cout<<"Player "<<pn<<" move (1 - 8, 0 to quit): ";
cin>>userInput;
cout<<endl;
if(userInput > 8 || userInput < 0)
{
cout<<"Input entered is invalid, please try again."<<endl;
}
if(userInput == 0)
{
breaker = true;
}
if(userInput > 0 && userInput <= 8)
{
bool subBreaker = false;
bool changeCheck = false;
int initialRow = 6;
while(subBreaker == false && initialRow >= 0)
{
if(board[initialRow][userInput - 1] != '.')
{
initialRow--;
}
if(board[initialRow][(userInput - 1)] == '.')
{
board[initialRow][(userInput - 1)] = symbol;
changeCheck = true;
subBreaker = true;
breaker = true;
}
}
if(changeCheck == false)
{
cout<<"Column is already full, please re-enter your input."<<endl;
breaker = false;
}
}
}
return userInput;
}
void drawBoard(char board[][COL])
{
for(int x = 0; x < ROW; x++)
{
for(int y = 0; y < COL; y++)
{
cout<<board[x][y];
}
cout<<endl;
}
cout<<"12345678"<<endl;
}
bool saveGame(GameState gs, char fileName[])
{
ofstream outfile;
outfile.open(fileName);
if(!outfile)
{
return false;
}
else
{
for(int x = 0; x < 7; x++)
{
for(int y = 0; y < 8; y++)
{
outfile<<gs.board[x][y];
}
outfile<<"#";
}
outfile<<endl<<gs.symbol<<gs.nextTurn;
outfile.close();
return true;
}
}
bool loadGame(GameState& gs, char fileName[])
{
ifstream infile;
infile.open(fileName);
if(!infile)
{
cout<<"ERROR: File not found."<<endl<<endl;
return false;
}
else
{
for(int x = 0; x < 7; x++)
{
for(int y = 0; y < 8; y++)
{
infile>>gs.board[x][y];
}
infile.ignore(1, '#');
}
infile.ignore(1,'\n');
infile>>gs.symbol>>gs.nextTurn;
return true;
}
}
int checkWinner(GameState& gs)
{
int winner = 0;
/*
bool isDraw = true;
for(int x = 0; x < 7; x++)
{
for(int y = 0; y < 8; y++)
{
if((gs.board[x][y] == gs.board[x][y + 1] == gs.board[x][y + 2] == gs.board[x][y + 3] == 'O') || (gs.board[x][y] == gs.board[x][y - 1] == gs.board[x][y - 2] == gs.board[x][y - 3] == 'O'))
{
winner = 1;
}
if((gs.board[x][y] == gs.board[x][y + 1] == gs.board[x][y + 2] == gs.board[x][y + 3] == 'X') || (gs.board[x][y] == gs.board[x][y - 1] == gs.board[x][y - 2] == gs.board[x][y - 3] == 'X'))
{
winner = 2;
}
if((gs.board[x][y] == gs.board[x + 1][y] == gs.board[x + 2][y] == gs.board[x + 3][y] == 'O') || (gs.board[x][y] == gs.board[x - 1][y] == gs.board[x - 2][y] == gs.board[x - 3][y] == 'O'))
{
winner = 1;
}
if((gs.board[x][y] == gs.board[x + 1][y] == gs.board[x + 2][y] == gs.board[x + 3][y] == 'X') || (gs.board[x][y] == gs.board[x - 1][y] == gs.board[x - 2][y] == gs.board[x - 3][y] == 'X'))
{
winner = 2;
}
if((gs.board[x][y] == gs.board[x + 1][y + 1] == gs.board[x + 2][y + 2] == gs.board[x + 3][y + 3] == 'O') || (gs.board[x][y] == gs.board[x - 1][y + 1] == gs.board[x - 2][y + 2] == gs.board[x - 3][y + 3] == 'O'))
{
winner = 1;
}
if((gs.board[x][y] == gs.board[x + 1][y + 1] == gs.board[x + 2][y + 2] == gs.board[x + 3][y + 3] == 'X') || (gs.board[x][y] == gs.board[x - 1][y + 1] == gs.board[x - 2][y + 2] == gs.board[x - 3][y + 3] == 'X'))
{
winner = 2;
}
}
}
for(int x = 0; x < 7; x++)
{
for(int y = 0; y < 8; y++)
{
if(gs.board[x][y] == '.')
{
isDraw = false;
}
}
}
if(isDraw == true)
{
winner = 3;
}
*/
return winner;
}