#include #include #include #include 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<<"*******************************************************************************"<>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!!"<>query; cout<>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<>filename; isSaved = saveGame(gs, filename); if(isSaved == true) { cout<>query; cout<>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<>filename; isSaved = saveGame(gs, filename); if(isSaved == true) { cout<>query; cout<>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<>filename; isSaved = saveGame(gs, filename); if(isSaved == true) { cout<>filename; isLoaded = loadGame(gs,filename); if(isLoaded == true) { cout<<"Game Loaded!!"<>suboption; if(suboption == 'y') unbroken = true; else if(suboption == 'n') { cout<<"Thanks for playing connect four !! The game of the clever people !!"<= 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: "<>userInput; cout< 8 || userInput < 0) { cout<<"Input entered is invalid, please try again."< 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."<>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; }