Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(C++ only) Do not use any pointers or class please! Need code revision. So, this code is the game called Connected4. The general purpose of
(C++ only) Do not use any pointers or class please! Need code revision. So, this code is the game called Connected4. "The general purpose of Connect-4 is to be the first person to get 4-in-a-row. This game has historically been played where you slid chips into columns, so the play is always the lowest available row in the column." There are two things to revise. If you type s(save) or l(load), the program wont work because this code doesnt have those parts. So you need to write a code for saving the game and loading the game.(Two parts) Here's some restriction. For saving part : Add functionality so the user can type s to save the game. When this save option is selected, you should put the board exactly as it is shown into the file save.txt. If save.txt already exists, override it with the current board. For loading part : Add functionality to either the base connectFour.cpp or your answer to part A to press l to load a game from save.txt. If this file does not exist, you should not change the current board. Otherwise, you should change the board and let the user play the saved board normally. Note: You may assume the the save.txt is in a valid board for the game code ------------------------------------------------------------------------------------------------ #include using namespace std; const int RSIZE = 6; const int CSIZE = 7; const char BLANK = '.'; const char P1 = 'X'; const char P2 = 'O'; char winner(char board[RSIZE][CSIZE]); string play(char board[RSIZE][CSIZE], int col, char who); bool fourInARow(char board[RSIZE][CSIZE], int i, int j, int dr, int dc); void print(char board[RSIZE][CSIZE]); int main() { char board[RSIZE][CSIZE]; for(int i=0; i< RSIZE; i++) { for(int j=0; j < CSIZE; j++) { board[i][j] = BLANK; } } string message = ""; char player = P2; while( !winner(board)) { if(player == P2 && message == "") // rotate player turns { player = P1; } else if(player == P1 && message == "") { player = P2; } print(board); cout << message << endl; message = ""; cout << "Which column do you wish to play in? Or (s)ave/(l)oad? "; char ans; cin >> ans; string dump; getline(cin, dump); if('1' <= ans && ans <= '7') { message = play(board,ans-'1', player); } else { message = "Not a valid move, try again..."; } } print(board); cout << endl; if(winner(board) == P1) { cout << "The mighty " << P1 << "s reign supreme! "; } else if(winner(board) == P2) { cout << "The " << P2 << "s have completed world domination! "; } else if(winner(board) == BLANK) { cout << "Zzzzzzzzzzzzz... draw "; } else { cout << "Derp... something went wrong!"; } } char winner(char board[RSIZE][CSIZE]) { int blanks = 0; for(int i=0; i< RSIZE; i++) { for(int j=0; j < CSIZE; j++) // loop through all cells on board { if(board[i][j] == BLANK) // count how many blanks { blanks++; } for(int k=1; k < 5; k++) // check all four directions (up/down, left/right, up-right/down-left, up-left/down-right). { int dr=(k%2), dc=(k/2); // dr & dc change in row/column. so up/down means dr=1, dc=0 if(board[i][j] != BLANK && fourInARow(board,i,j,dr,dc)) // if 4 in a row (or more) return which player won (char) { return board[i][j]==P1?P1:P2; } } } } if(blanks == 0) // board full and no winner { return BLANK; } else // still going! { return 0; } } bool fourInARow(char board[RSIZE][CSIZE], int i, int j, int dr, int dc) { int sum=1; int ci, cj; ci=i+dr; cj=j+dc; while(ci < RSIZE && ci >= 0 && cj < CSIZE && cj >= 0 && board[i][j] == board[ci][cj]) // in the board bounds and in a streak { sum++; ci += dr; cj += dc; } ci=i-dr; cj=j-dc; while(ci < RSIZE && ci >= 0 && cj < CSIZE && cj >= 0 && board[i][j] == board[ci][cj]) // count in the opposite direction too { sum++; ci -= dr; cj -= dc; } return sum >= 4; } string play(char board[RSIZE][CSIZE], int col, char who) { for(int i=RSIZE-1; i >= 0; i--) { if(board[i][col] == BLANK) { board[i][col] = who; return ""; } } return "That column is full (idiot)."; } void print(char board[RSIZE][CSIZE]) { cout << " "; for(int i=0; i < RSIZE; i++) { for(int j=0; j < CSIZE; j++) { cout << board[i][j]; } cout << endl; } for(int i=1;i<8;i++) { cout << i; } cout << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started