I NEED ANSWER WITHIN NEXT 3 hours This is all in c++ (using namespace std) We di
ID: 3677515 • Letter: I
Question
I NEED ANSWER WITHIN NEXT 3 hours
This is all in c++ (using namespace std)
We did a program last week called assignment 5b. I will show you the problem statement for that and my solution. This week we have to modify the program using functions. This is where I am confused. I will show last weeks porblem and solution first:
PROBLEM STATEMENT: Write a generalized program that mimics the following sample runs. Be sure not to hard code the solutions; the program must work in the general case (what is the general case here?). User input is shown as underlined.
In this game, you must survive a certain number of moves (a random number between 5 and 12). You survive a move by moving on to a square that does not contain a bomb (‘*’). You can move North, South, East or West. Your position is shown by the letter “U”. Each time you move from a spot, it turns into a bomb. A random number of bombs (between 3 and 7) are automatically placed when the game starts. If you fall off the edge, you lose! If you get blown up, you lose!
Here are some sample outputs
and another
This is the solution :
#include
#include
#include
#include
using namespace std;
const int NUMROWS = 5;
const int NUMCOLS = 5;
int main()
{
srand(time(NULL));
int NUMBOMBS = (rand() % 5) + 3;
int NUMPLAYS = (rand() % 8) + 5;
int currRow, currCol;
bool win = true;
char board[NUMROWS][NUMCOLS];
for( int r = 0; r< NUMROWS; r++) {
for (int c = 0; c < NUMCOLS; c++){
board[r][c] = '_';
}
}
for (int bomb = 0; bomb < NUMBOMBS; bomb++) {
board[rand() % NUMROWS][rand() % NUMCOLS] = '*';
}
currRow = rand() % NUMROWS;
currCol = rand () % NUMCOLS;
board[currRow][currCol] = 'U';
for (int play = NUMPLAYS; play >0; play--) {
cout << "Survive " << play << " MORE MOVES!" << endl;
for (int r = 0; r < NUMROWS; r++) {
for (int c = 0; c < NUMCOLS; c++) {
cout << board[r][c] << " ";
}
cout << endl;
}
board[currRow][currCol] = '*';
cout << "Enter direction (N/S/E/W): ";
char direction;
cin >> direction;
if(direction == 'N'){
currRow --;
}
if(direction == 'S'){
currRow++;
}
if(direction == 'E'){
currCol++;
}
if(direction == 'W'){
currCol--;
}
if ((currRow < 0) || (currRow > NUMROWS) || (currCol < 0) || (currCol > NUMCOLS)) {
cout << "YOU FELL OFF THE BOARD!" << endl;
win = false;
break;
}
if (board[currRow][currCol] == '*') {
cout << "BANG! You're dead!" << endl;
win = false;
break;
}
board[currRow][currCol] = 'U';
}
if (win)
cout << "*** YOU WIN! ***" << endl;
else
cout << "*** YOU LOSE! ***" << endl;
return 0;
}
Here is the problem statemnet for this week. We have to modify that program above using functions
Modify your solution to assignment 5B as follows:
Create a function called initBoard which is called once from main and which initializes all of the cells on the board to and underscore ('_').
Create a function called plantBombs which is called once from main and which sets the bombs in the cells.
Create a function called placeYou which is called once from main and which sets the player ('U') on the board.
Create a function called showBoard which is called from main and which displays the board on the screen.
Create a function called movePlayer which is called from main and which asks the user for a direction and then moves the 'U' in that direction.
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<time.h>
//#include
using namespace std;
const int NUMROWS = 5;
const int NUMCOLS = 5;
//Create a function called initBoard which is called once from main and which initializes all of the cells on the board to and underscore ('_').
void initBoard(char board[][NUMCOLS]) {
for (int r = 0; r < NUMROWS; r++) {
for (int c = 0; c < NUMCOLS; c++) {
board[r][c] = '_';
}
}
}
//Create a function called plantBombs which is called once from main and which sets the bombs in the cells.
void plantBombs(char board[][NUMCOLS], int NUMBOMBS) {
for (int bomb = 0; bomb < NUMBOMBS; bomb++) {
board[rand() % NUMROWS][rand() % NUMCOLS] = '*';
}
}
//Create a function called placeYou which is called once from main and which sets the player ('U') on the board.
void placeYou(char board[][NUMCOLS], int currRow, int currCol) {
board[currRow][currCol] = 'U';
}
//Create a function called showBoard which is called from main and which displays the board on the screen
void showBoard(char board[][NUMCOLS]) {
for (int r = 0; r < NUMROWS; r++) {
for (int c = 0; c < NUMCOLS; c++) {
cout << board[r][c] << " ";
}
cout << endl;
}
}
//Create a function called movePlayer which is called from main and which asks the user for a direction and then moves the 'U' in that direction.
bool movePlayer(char board[][NUMCOLS], int currRow, int currCol) {
bool win = false;
cout << "Enter direction (N/S/E/W): ";
char direction;
cin >> direction;
if (direction == 'N') {
currRow--;
}
if (direction == 'S') {
currRow++;
}
if (direction == 'E') {
currCol++;
}
if (direction == 'W') {
currCol--;
}
if ((currRow < 0) || (currRow > NUMROWS) || (currCol < 0)
|| (currCol > NUMCOLS)) {
cout << "YOU FELL OFF THE BOARD!" << endl;
win = false;
}
if (board[currRow][currCol] == '*') {
cout << "BANG! You're dead!" << endl;
win = false;
}
board[currRow][currCol] = 'U';
return win;
}
int main() {
srand(time(NULL));
int NUMBOMBS = (rand() % 5) + 3;
int NUMPLAYS = (rand() % 8) + 5;
int currRow, currCol;
bool win = true;
char board[NUMROWS][NUMCOLS];
initBoard(board);
plantBombs(board, NUMBOMBS);
currRow = rand() % NUMROWS;
currCol = rand() % NUMCOLS;
placeYou(board, currRow, currCol);
for (int play = NUMPLAYS; play > 0; play--) {
cout << "Survive " << play << " MORE MOVES!" << endl;
showBoard(board);
board[currRow][currCol] = '*';
win = movePlayer(board, currRow, currCol);
if(!win)
break;
}
if (win)
cout << "*** YOU WIN! ***" << endl;
else
cout << "*** YOU LOSE! ***" << endl;
return 0;
}
---ouput---
Survive 10 MORE MOVES!
_ _ _ _ _
U _ _ _ _
_ _ * _ _
_ _ * _ _
_ _ _ _ _
Enter direction (N/S/E/W): S
Survive 9 MORE MOVES!
_ _ _ _ _
* _ _ _ _
U _ * _ _
_ _ * _ _
_ _ _ _ _
Enter direction (N/S/E/W): E
Survive 8 MORE MOVES!
_ _ _ _ _
* _ _ _ _
* U * _ _
_ _ * _ _
_ _ _ _ _
Enter direction (N/S/E/W): S
Survive 7 MORE MOVES!
_ _ _ _ _
* _ _ _ _
* * * _ _
_ U * _ _
_ _ _ _ _
Enter direction (N/S/E/W): S
Survive 6 MORE MOVES!
_ _ _ _ _
* _ _ _ _
* * * _ _
_ * * _ _
_ U _ _ _
Enter direction (N/S/E/W): Survive 5 MORE MOVES!
_ _ _ _ _
* _ _ _ _
* * * _ _
_ * * _ _
_ * _ _ _
Enter direction (N/S/E/W): YOU FELL OFF THE BOARD!
*** YOU LOSE! ***
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.