PLEASE ANSWER BEFORE 11:30 pm!! This is all in c++ (using namespace std) PROBLEM
ID: 3672876 • Letter: P
Question
PLEASE ANSWER BEFORE 11:30 pm!!
This is all in c++ (using namespace std)
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
My code so far
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
const int numRows = 5, numCols = 5;
void displayBoard(char board[numRows][numCols]) {
// display the board
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
}
int main() {
srand(time(0));
char board[numRows][numCols];
// Initialize board
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
board[row][col] = '_';
}
}
// prompt for board values
int row, col, i;
char direction;
char Bomb = '*';
int numBombs = 0;
int numMoves = 0;
numBombs = ((rand() % 5) + 3);
numMoves = ((rand() % 8) + 5);
char User[numMoves];
for (i = 0; i< numBombs; ++i){
row = ((rand() % 5) + 1);
col = ((rand() % 5) + 1);
board[row ][col] = Bomb;
// display the board
}
for(int j = 0; j< numMoves; ++j){
User[j] = 'U';
cout << "Survive " << numMoves-j << " more moves" << endl;
board[row-1 ][col-1] = User[j];
displayBoard(board);
if(User[j] == board[row-1 ][col-1]){
cout << "Enter direction (N/S/E/W): ";
cin >> direction;
if(direction == 'N'){
row = row-1;
User[j] = '*';
}
if(direction == 'S'){
row = row+1;
User[j] = '*';
}
if(direction == 'E'){
col = col+1;
User[j] = '*';
}
if(direction == 'W'){
col = col-1;
User[j] = '*';
}
}
else{
cout<< "You lose";
}
}
return 0;
}
1. Introduction 2. Variables/ Assignments 3. Branches 4. Loops 5. Arrays /VectorsExplanation / Answer
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
const int numRows = 5, numCols = 5;
void displayBoard(char board[numRows][numCols]) {
// display the board
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
}
int main() {
srand(time(0));
char board[numRows][numCols];
// Initialize board
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
board[row][col] = '_';
}
}
// prompt for board values
int row, col, i;
char direction;
char Bomb = '*';
int numBombs = 0;
int numMoves = 0;
numBombs = ((rand() % 5) + 3);
numMoves = ((rand() % 8) + 5);
char User[numMoves];
for (i = 0; i< numBombs; ++i){
row = ((rand() % 5) + 1);
col = ((rand() % 5) + 1);
board[row ][col] = Bomb;
// display the board
}
for(int j = 0; j< numMoves; ++j){
User[j] = 'U';
cout << "Survive " << numMoves-j << " more moves" << endl;
board[row-1 ][col-1] = User[j];
displayBoard(board);
if(User[j] == board[row-1 ][col-1]){
cout << "Enter direction (N/S/E/W): ";
cin >> direction;
if(direction == 'N'){
row = row-1;
User[j] = '*';
}
if(direction == 'S'){
row = row+1;
User[j] = '*';
}
if(direction == 'E'){
col = col+1;
User[j] = '*';
}
if(direction == 'W'){
col = col-1;
User[j] = '*';
}
}
else{
cout<< "You lose";
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.