(C++ only) Do not use any pointers or class please! Need code revision. So, this
ID: 3724056 • Letter: #
Question
(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; }
Explanation / Answer
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
// Assigns constant values
const int RSIZE = 6;
const int CSIZE = 7;
const char BLANK = '.';
const char P1 = 'X';
const char P2 = 'O';
// Prototype of functions
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]);
void writeFile(char board[RSIZE][CSIZE]);
void readFile(char board[RSIZE][CSIZE]);
// Main function definition
int main()
{
// Declares a character array for board
char board[RSIZE][CSIZE];
// Loops till number of rows
for(int i=0; i< RSIZE; i++)
{
// Loops till number of columns
for(int j=0; j < CSIZE; j++)
{
// Assigns blank in each cell
board[i][j] = BLANK;
}// End of inner loop
}// End of outer loop
string message = "";
char player = P2;
// Loops till winner or 's' or 'S'
while(!winner(board))
{
// Sets the player turn
if(player == P2 && message == "") // rotate player turns
{
player = P1;
}
else if(player == P1 && message == "")
{
player = P2;
}
// Calls the function display the board
print(board);
cout << message << endl;
message = "";
cout << "Which column do you wish to play in? Or (s)ave/(l)oad? ";
char ans;
cin >> ans;
// Checks if the ans is 's' or 'S'
if(ans == 's' || ans == 'S')
{
cout<<" Saving the file";
// Calls the function to write board contents in file
writeFile(board);
exit(0);
}// End of while loop
// Checks if the ans is 'l' or 'L'
else if(ans == 'l' || ans == 'L')
{
// Calls the function to read file contents and store it in board
readFile(board);
// Calls the function to display board
print(board);
cout << "Which column do you wish to play in? Or (s)ave/(l)oad? ";
}// End of else
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...";
}
}// End of while loop
// Calls the function display the board
print(board);
cout << endl;
// Checks if the winner is player one
if(winner(board) == P1)
{
cout << "The mighty " << P1 << "s reign supreme! ";
}
// Otherwise checks if the winner is player two
else if(winner(board) == P2)
{
cout << "The " << P2 << "s have completed world domination! ";
}
// Checks for draw
else if(winner(board) == BLANK)
{
cout << "Zzzzzzzzzzzzz... draw ";
}
else
{
cout << "Derp... something went wrong!";
}
}// End of main function
// Function to check the winner
char winner(char board[RSIZE][CSIZE])
{
int blanks = 0;
// Loops till number of rows
for(int i=0; i< RSIZE; i++)
{
// Loops till number of columns
for(int j=0; j < CSIZE; j++) // loop through all cells on board
{
if(board[i][j] == BLANK) // count how many blanks
{
blanks++;
}// End of if condition
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;
}// End of if condition
}// End of for loop for variable k
}// End of for loop for variable j
}// End of for loop for variable i
if(blanks == 0) // board full and no winner
{
return BLANK;
}
else // still going!
{
return 0;
}
}// End of function
// Function to check whether 4 symbols in a row then return true
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;
}// End of while condition
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;
}// End of while condition
return sum >= 4;
}// End of function
// Function to play the game
string play(char board[RSIZE][CSIZE], int col, char who)
{
// Loops reverse order order of row size till zero
for(int i=RSIZE-1; i >= 0; i--)
{
// Checks if the board i and col index position is blank
if(board[i][col] == BLANK)
{
// Stores the who value in board i and col index position
board[i][col] = who;
return "";
}// End of if condition
}// End of for loop
return "That column is full (idiot).";
}// End of function
// Function to print the board
void print(char board[RSIZE][CSIZE])
{
cout << " ";
// Loops till number of rows
for(int i=0; i < RSIZE; i++)
{
// Loops till number of columns
for(int j=0; j < CSIZE; j++)
{
// Display the board contents
cout << board[i][j];
}// End of inner for loop
// Displays new line
cout << endl;
}// End of outer for loop
// Loops 8 times
for(int i=1;i<8;i++)
{
cout << i;
}// End of for loop
cout << endl;
}// End of function
// Function to write the board contents in the file
void writeFile(char board[RSIZE][CSIZE])
{
ofstream wFile;
// Opens the file for writing
wFile.open("save.txt");
// Loops till number of rows
for(int i=0; i < RSIZE; i++)
{
// Loops till number of columns
for(int j=0; j < CSIZE; j++)
{
// Writes board i and j index position data in file
wFile<<board[i][j];
}// End of inner for loop
// Writes now line character
wFile<<endl;
}// End of outer for loop
// Close the file
wFile.close();
}// End of function
// Function to read the file contents and store it in board matrix
void readFile(char board[RSIZE][CSIZE])
{
ifstream rFile;
// Opens the file for reading
rFile.open("save.txt");
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!rFile.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file!";
return;
}// End of if condition
// Loops till number of rows
for(int i=0; i < RSIZE; i++)
{
// Loops till number of columns
for(int j=0; j < CSIZE; j++)
{
// Reads data from file and stores it in board i and j index position
rFile>>board[i][j];
}// End of inner for loop
}// End of ouster for loop
// Close the file
rFile.close();
}// End of function
Sample Output 1:
.......
.......
.......
O......
O......
XX.....
1234567
Not a valid move, try again...
Which column do you wish to play in? Or (s)ave/(l)oad?
Saving the file.
Sample Output 2:
.......
.......
.......
O......
O......
XX.....
1234567
Which column do you wish to play in? Or (s)ave/(l)oad?
l
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.