Survive the Maze: A Game of Strategy

In summary: Rows; i++) { for (int j = 0; j < numCols; j++) { cout << board[i][j]; } }}void game() { numMoves = 0; displayBoard(rand() % numRows + 'A' + rand() % numCols); }int main() { game();return 0;}
  • #1
FallArk
127
0
The instructions are given below:
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!
Example run 1:
SURVIVE 8 MORE MOVES!
_ _ _ _ _
_ _ * _ _
_ _ _ _ U
_ _ _ * _
* _ _ _ *
Enter direction (N/S/E/W): W
SURVIVE 7 MORE MOVES!
_ _ _ _ _
_ _ * _ _
_ _ _ U *
_ _ _ * _
* _ _ _ *
Enter direction (N/S/E/W): W
SURVIVE 6 MORE MOVES!
_ _ _ _ _
_ _ * _ _
_ _ U * *
_ _ _ * _
* _ _ _ *
Enter direction (N/S/E/W): W
SURVIVE 5 MORE MOVES!
_ _ _ _ _
_ _ * _ _
_ U * * *
_ _ _ * _
* _ _ _ *
Enter direction (N/S/E/W): W
SURVIVE 4 MORE MOVES!
_ _ _ _ _
_ _ * _ _
U * * * *
_ _ _ * _
* _ _ _ *
Enter direction (N/S/E/W): N
SURVIVE 3 MORE MOVES!
_ _ _ _ _
U _ * _ _
* * * * *
_ _ _ * _
* _ _ _ *
Enter direction (N/S/E/W): N
SURVIVE 2 MORE MOVES!
U _ _ _ _
* _ * _ _
* * * * *
_ _ _ * _
* _ _ _ *
Enter direction (N/S/E/W): E
SURVIVE 1 MORE MOVES!
* U _ _ _
* _ * _ _
* * * * *
_ _ _ * _
* _ _ _ *
Enter direction (N/S/E/W): E
*** YOU WIN! ***
Example run 2:
SURVIVE 5 MORE MOVES!
U _ _ _ _
_ _ _ _ _
_ * _ _ _
_ _ _ _ _
_ _ * _ *
Enter direction (N/S/E/W): N
YOU FELL OFF THE BOARD!
*** YOU LOSE! ***
Example run 3:
SURVIVE 11 MORE MOVES!
_ _ * _ U
_ _ _ * _
_ _ * _ _
_ _ _ _ *
_ _ * _ _
Enter direction (N/S/E/W): W
SURVIVE 10 MORE MOVES!
_ _ * U *
_ _ _ * _
_ _ * _ _
_ _ _ _ *
_ _ * _ _
Enter direction (N/S/E/W): W
BANG! You're dead!
*** YOU LOSE! ***

I'm thinking about using array to print out the board and then assign different values to display the bomb and where the user is. Then use a switch statement to make all the potential if statements i will use much cleaner. Am I on the right track at least?
But I don't know how to code it that when the "U" moves and turn the previous location to a "*". I also don't know how to check if the user win or lose.
 
Technology news on Phys.org
  • #2
I think you are going to have to go into more detail (some code would be nice) if you expect us to be able to help you. Just my opinion, of course.

For the board you might want to consider a two-dimensional [m]char[/m] array. Initialize it to contain all [m]_[/m] then, using [m]rand()[/m], place the user and the bombs. Determine the number of turns the user must survive for (again using [m]rand[/m]), then enter a loop that prints the board, gets each user move, updates the positions of the bombs and the user and makes sure the user stays on the board. As I may have missed some details I'll leave it to you to come up with some code and we can have a look. Happy coding!
 
  • #3
greg1313 said:
I think you are going to have to go into more detail (some code would be nice) if you expect us to be able to help you. Just my opinion, of course.

For the board you might want to consider a two-dimensional [m]char[/m] array. Initialize it to contain all [m]_[/m] then, using [m]rand()[/m], place the user and the bombs. Determine the number of turns the user must survive for (again using [m]rand[/m]), then enter a loop that prints the board, gets each user move, updates the positions of the bombs and the user and makes sure the user stays on the board. As I may have missed some details I'll leave it to you to come up with some code and we can have a look. Happy coding!

This is what I have so far :
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

const int numRows = 5, numCols = 5;
int numMoves = 0;
int row, col;

void displayBoard(char board[numRows][numCols]) {
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			cout << board[row][col] << " _ ";
		}
		cout << endl;
	}
}

int main() {
	char board[numRows][numCols];
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			board[row][col] = '_';
		}
	}
	srand(time(0));
    numMoves = (rand() % 8) + 5;

    cout << "SURVIVE " << numMoves << " MORE MOVES!" << endl;
		displayBoard(board);

}
How should I make the position of "U" different each time and after it moves turn it into a "*"? Thanks! I'm really new to C++ and sometimes I just can't see the obvious
 
  • #4
Here's a portion of what I came up with.

Code:
        i = 0;
	while (i < moves) {

	cout << "SURVIVE " << moves - i << " MORE MOVES!" << endl;
	drawBoard();
	cout << "Enter direction (n/s/e/w): ";
	cin >> direction;

	switch (direction) {

		case 'n':
			if (userRow - 1 < 0) 
				endGame(OFF_BOARD);
			else if (board[userRow - 1][userCol] == '*')
					endGame(BOMB);
			else {
				board[userRow][userCol] = '*';
				board[userRow - 1][userCol] = 'U';
				userRow = userRow - 1;
				++i;
			}
			break;

I used several functions to code the entire assignment, resulting in a main() (a portion of which is shown above) that is pretty straightforward.
 
  • #5
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

const int numRows = 5, numCols = 5;
int numMoves = 0;
int i = 0;
char userDirection;
int bombrow = 0;
int bombcol = 0;
int usercol = 0;
int userrow = 0;
void displayBoard(char board[numRows][numCols]) {
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			cout << board[row][col] << " ";
		}
		cout << endl;
	}
}

int main() {
	char board[numRows][numCols];
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			board[row][col] = '_';
		}
	}
	srand(time(0));
	numMoves = (rand() % 8) + 5;
	bombrow = (rand() % 5);
	bombcol = (rand() % 5);
	userrow = (rand() % 5);
	usercol = (rand() % 5);
	cout << "SURVIVE " << numMoves << " MORE MOVES!" << endl;
	char answer;
	int row, col;
	for (int x = 0; x = (rand() % 5) + 3; ++x) {
        board[bombrow][bombcol] = '*';
	}
	board[userrow][usercol] = 'U';
	displayBoard(board);
    while (i < numMoves) {
        cout << "SURVIVE " << numMoves - i << " MORE MOVES!" << endl;
        displayBoard(board);
        cout << "Enter direction (N/S/E/W): ";
        cin >> userDirection;

        switch (userDirection) {
    case 'N':
        if (row - 1 < 0){
            cout << "YOU FELL OFF THE BOARD!" <<endl;
            cout << "*** YOU LOSE! ***";
            return 0;
        }
        else if (board[row - 1][col] == '*') {
            cout << "BANG! You're dead!" << endl;
            cout << "*** YOU LOSE! ***";
            return 0;
        }
        else {
            board[row][col] = '*';
				board[row - 1][col] = 'U';
				row = row - 1;
				++i;
        }
        break;        }
    }

}
This is what I have right now. I tried to use random number generated to assign where the bombs would be, but nothing is displayed, even the board. What would be the problems(there are probably plenty stupid things going on). Thank you!
 
  • #6
I should probably let Greg answer this, but he might be busy:

Code:
 for (int x = 0; x = (rand() % 5) + 3; ++x) {
               board[bombrow][bombcol] = '*';
}

This is an infinite loop! Your terminating condition is x = (rand()%5)+3; the value on the right hand side of the assignment = is always non-zero. Now as a boolean expression, the assignment operator = has value what is assigned, in this case a non-zero value or true. Ergo, infinite loop. (Aside; friendlier languages like Java won't let you do this.)

Presumably, what this is supposed to do is put a random number of bombs (stars) into the board at random positions. Here's a flawed way to do this:

Code:
int bombCount = rand()%5+3;
for (i=0;i<bombCount;i++) {
   bombrow=rand()%5;
   bombcol=rand()%5;
   board[bombrow][bombcol]='*';
}

The above loop does execute bombCount number of times, but there is no guarantee that all the positions, randomly generated, are different. So there will be at least one star put into board, but possibly fewer than bombCount number.
This will get you started, but be aware that a different technique must be used to guarantee the placement of exactly bombCount bombs.

Finally, in your displayBoard function, the specifications say the components are printed consecutively with no spaces between. You output a space between components
 
  • #7
johng said:
I should probably let Greg answer this, but he might be busy:

Code:
 for (int x = 0; x = (rand() % 5) + 3; ++x) {
               board[bombrow][bombcol] = '*';
}

This is an infinite loop! Your terminating condition is x = (rand()%5)+3; the value on the right hand side of the assignment = is always non-zero. Now as a boolean expression, the assignment operator = has value what is assigned, in this case a non-zero value or true. Ergo, infinite loop. (Aside; friendlier languages like Java won't let you do this.)

Presumably, what this is supposed to do is put a random number of bombs (stars) into the board at random positions. Here's a flawed way to do this:

Code:
int bombCount = rand()%5+3;
for (i=0;i<bombCount;i++) {
   bombrow=rand()%5;
   bombcol=rand()%5;
   board[bombrow][bombcol]='*';
}

The above loop does execute bombCount number of times, but there is no guarantee that all the positions, randomly generated, are different. So there will be at least one star put into board, but possibly fewer than bombCount number.
This will get you started, but be aware that a different technique must be used to guarantee the placement of exactly bombCount bombs.

Finally, in your displayBoard function, the specifications say the components are printed consecutively with no spaces between. You output a space between components

How did I not see that? Thank you so much!
 
  • #8
johng said:
I should probably let Greg answer this, but he might be busy:

Code:
 for (int x = 0; x = (rand() % 5) + 3; ++x) {
               board[bombrow][bombcol] = '*';
}

This is an infinite loop! Your terminating condition is x = (rand()%5)+3; the value on the right hand side of the assignment = is always non-zero. Now as a boolean expression, the assignment operator = has value what is assigned, in this case a non-zero value or true. Ergo, infinite loop. (Aside; friendlier languages like Java won't let you do this.)

Presumably, what this is supposed to do is put a random number of bombs (stars) into the board at random positions. Here's a flawed way to do this:

Code:
int bombCount = rand()%5+3;
for (i=0;i<bombCount;i++) {
   bombrow=rand()%5;
   bombcol=rand()%5;
   board[bombrow][bombcol]='*';
}

The above loop does execute bombCount number of times, but there is no guarantee that all the positions, randomly generated, are different. So there will be at least one star put into board, but possibly fewer than bombCount number.
This will get you started, but be aware that a different technique must be used to guarantee the placement of exactly bombCount bombs.

Finally, in your displayBoard function, the specifications say the components are printed consecutively with no spaces between. You output a space between components

I fixed the infinite loop but it only assigned one bomb, I ran it a couple times and there is only one bomb. I don't know what went wrong.
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

const int numRows = 5, numCols = 5;
int numMoves = 0;
int i = 0;
char userDirection;
int bombrow = 0;
int bombcol = 0;
int usercol = 0;
int userrow = 0;
int bombcount = 0;
void displayBoard(char board[numRows][numCols]) {
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			cout << board[row][col] << " ";
		}
		cout << endl;
	}
}

int main() {
	char board[numRows][numCols];
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			board[row][col] = '_';
		}
	}
	srand(time(0));
	numMoves = (rand() % 8) + 5;
	bombcount = (rand() % 5) + 3;
	cout << "SURVIVE " << numMoves << " MORE MOVES!" << endl;
	int row, col;
	for (int x = 0; x < bombcount; ++x) {
        board[bombrow][bombcol] = '*';
	}
	displayBoard(board);
    while (i < numMoves) {
        cout << "SURVIVE " << numMoves - i << " MORE MOVES!" << endl;
        displayBoard(board);
        cout << "Enter direction (N/S/E/W): ";
        cin >> userDirection;

        switch (userDirection) {
    case 'N':
        if (row - 1 < 0){
            cout << "YOU FELL OFF THE BOARD!" <<endl;
            cout << "*** YOU LOSE! ***";
            return 0;
        }
        else if (board[row - 1][col] == '*') {
            cout << "BANG! You're dead!" << endl;
            cout << "*** YOU LOSE! ***";
            return 0;
        }
        else {
            board[row][col] = '*';
				board[row - 1][col] = 'U';
				row = row - 1;
				++i;
        }
        break;        }
    }

}
 
  • #9
My advice is to write a random number function that returns a random number between a given range.

Code:
int randomNumber(int low, int high) {

// code

}

... and then

Code:
        // place bombs
        numBombs = randomNumber(MIN_BOMBS, MAX_BOMBS);
	i = 0;
	while (i < numBombs) {
		row = randomNumber(0, NUM_ROWS - 1);
		col = randomNumber(0, NUM_COLS - 1);
			
		if (board[row][col] == '_') {
			board[row][col] = '*';
			++i;
		}

	}
 
  • #10
FallArk said:
I fixed the infinite loop but it only assigned one bomb, I ran it a couple times and there is only one bomb. I don't know what went wrong.
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

const int numRows = 5, numCols = 5;
int numMoves = 0;
int i = 0;
char userDirection;
int bombrow = 0;
int bombcol = 0;
int usercol = 0;
int userrow = 0;
int bombcount = 0;
void displayBoard(char board[numRows][numCols]) {
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			cout << board[row][col] << " ";
		}
		cout << endl;
	}
}

int main() {
	char board[numRows][numCols];
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			board[row][col] = '_';
		}
	}
	srand(time(0));
	numMoves = (rand() % 8) + 5;
	bombcount = (rand() % 5) + 3;
	cout << "SURVIVE " << numMoves << " MORE MOVES!" << endl;
	int row, col;
	for (int x = 0; x < bombcount; ++x) {
        board[bombrow][bombcol] = '*';
	}
	displayBoard(board);
    while (i < numMoves) {
        cout << "SURVIVE " << numMoves - i << " MORE MOVES!" << endl;
        displayBoard(board);
        cout << "Enter direction (N/S/E/W): ";
        cin >> userDirection;

        switch (userDirection) {
    case 'N':
        if (row - 1 < 0){
            cout << "YOU FELL OFF THE BOARD!" <<endl;
            cout << "*** YOU LOSE! ***";
            return 0;
        }
        else if (board[row - 1][col] == '*') {
            cout << "BANG! You're dead!" << endl;
            cout << "*** YOU LOSE! ***";
            return 0;
        }
        else {
            board[row][col] = '*';
				board[row - 1][col] = 'U';
				row = row - 1;
				++i;
        }
        break;        }
    }

}

You're not changing the values for [m]bombrow[/m] and [m]bombcol[/m].
 
  • #11
greg1313 said:
You're not changing the values for [m]bombrow[/m] and [m]bombcol[/m].

Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

const int numRows = 5, numCols = 5;
int numMoves = 0;
int i = 0;
char userDirection;
int bombrow = 0;
int bombcol = 0;
int usercol = 0;
int userrow = 0;
int bombcount = 0;
int main() {
	char board[numRows][numCols];
	srand(time(0));
	numMoves = (rand() % 8) + 5;
	bombcount = (rand() % 5) + 3;
	usercol = rand() % 5;
	userrow = rand() % 5;
	cout << "SURVIVE " << numMoves << " MORE MOVES!" << endl;
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			board[row][col] = '_';
		}
	}
	for (int x = 0; x < bombcount; ++x) {
        bombcol = rand() % 5;
        bombrow = rand() % 5;
        board[bombrow][bombcol] = '*';
	}
	while ((board[userrow][usercol] != '*') && board[userrow][usercol] == '_') {
        board[userrow][usercol] = 'U';
		}
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			cout << board[row][col] << " ";
		}
		cout << endl;
	}
}
This what I have done so far, the bombs are displayed properly and randomly. Thanks for the help! I will keep on working on it!
 
  • #12
How should I modify the for loop so that it will replace 'U' with '*'? I don't really know how to do it.
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

const int numRows = 5, numCols = 5;
int x = 1;
int numMoves = 0;
char userDirection;
int bombrow = 0;
int bombcol = 0;
int usercol = 0;
int userrow = 0;
int bombcount = 0;
int z = 0;

int main() {
	char board[numRows][numCols];
	srand(time(0));
	numMoves = (rand() % 8) + 5;
	bombcount = (rand() % 5) + 3;
	usercol = rand() % 5;
	userrow = rand() % 5;
	cout << "SURVIVE " << numMoves << " MORE MOVES!" << endl;
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			board[row][col] = '_';
		}
	}
	for (int x = 0; x < bombcount; ++x) {
        bombcol = rand() % 5;
        bombrow = rand() % 5;
        board[bombrow][bombcol] = '*';
	}
	while ((board[userrow][usercol] != '*') && board[userrow][usercol] == '_') {
        board[userrow][usercol] = 'U';
		}
	for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			cout << board[row][col] << " ";
		}
		cout << endl;
	}
	for (int i = 0; i < numMoves; ++i) {
	cout << "Enter direction (N/S/E/W): ";
	cin >> userDirection;
        switch (userDirection) {
        case 'W':
            board[userrow][usercol] = '*';
            board[userrow][usercol - x] = 'U';
            break;
        case 'E':
            board[userrow][usercol] = '*';
            board[userrow][usercol + x] = 'U';
            break;
        case 'N':
            board[userrow][usercol] = '*';
            board[userrow - x][usercol] = 'U';
            break;
        case 'S':
            board[userrow][usercol] = '*';
            board[userrow + x][usercol] = 'U';
            break;
        }
        ++z;
        ++x;
        for (int row = 0; row < numRows; row++) {
		for (int col = 0; col < numCols; col++) {
			cout << board[row][col] << " ";
		}
		cout << endl;
	}
	cout << "SURVIVE " << numMoves - z << " MOVES!" << endl;
}
}
 
  • #13
Hi FallArk. See the code in post #4. :D
 
  • #14
greg1313 said:
Hi FallArk. See the code in post #4. :D

I kind of figured this out on my own, but then I probably made some stupid mistakes again... Every time I ran it, it will tell me it ran into some issues and stopped working. I think I have the right idea though. Again, thanks for helping me and putting up with my stupidity. :p
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int numMoves = 0;
char userDirection;
int bombrow = 0;
int bombcol = 0;
int usercol = 0;
int userrow = 0;
int bombcount = 0;
int row;
int col;
string BOMB = "BANG! You're dead!";
string OFF_BOARD = "YOU FELL OFF THE BOARD!";

void printBoard (char board[5][5]) {
    for (row = 0; row < 5; row++) {
		for (col = 0; col < 5; col++) {
			cout << board[row][col] << " ";
		}
		cout << endl;
	}
}

string endGame1 (string BOMB) {
    cout << BOMB << endl << "*** YOU LOSE! ***";
    return 0;
}

string endGame2 (string OFF_BOARD) {
    cout << OFF_BOARD << endl << "*** YOU LOSE! ***";
    return 0;
}

int main() {
	char board[5][5];
	srand(time(0));
	numMoves = (rand() % 8) + 5;
	bombcount = (rand() % 5) + 3;
	usercol = rand() % 5;
	userrow = rand() % 5;

	for (row = 0; row < 5; row++) {
		for (col = 0; col < 5; col++) {
			board[row][col] = '_';
		}
	}
	for (int x = 0; x < bombcount; ++x) {
        bombcol = rand() % 5;
        bombrow = rand() % 5;
        board[bombrow][bombcol] = '*';
	}
	while ((board[userrow][usercol] != '*') && board[userrow][usercol] == '_') {
        board[userrow][usercol] = 'U';
		}
    int i = 0;
    while (i < numMoves) {
        cout << "SURVIVE " << numMoves - i << " MORE MOVES!" << endl;
        printBoard(board);
        cout << "Enter direction (N/S/E/W): ";
        cin >> userDirection;

        switch (userDirection) {
		case 'N':
			if (row - 1 < 0)
				endGame2(OFF_BOARD);
			else if (board[row - 1][col] == '*')
					endGame1(BOMB);
			else {
				board[row][col] = '*';
				board[row - 1][col] = 'U';
				row = row - 1;
				++i;
			}
			break;
        case 'S':
            if (row + 1 > 4)
                endGame2(OFF_BOARD);
            else if (board[row + 1][col] == '*')
                endGame1(BOMB);
            else {
                board[row][col] = '*';
                board[row + 1][col] = 'U';
                row = row + 1;
                ++i;
            }
            break;
        case 'W':
            if (col - 1 < 0)
                endGame2(OFF_BOARD);
            else if (board[row][col - 1] == '*')
                endGame1(BOMB);
            else {
                board[row][col] = '*';
                board[row][col - 1] = 'U';
                col = col - 1;
                ++i;
            }
            break;
        case 'E':
            if (col + 1 > 4)
                endGame2(OFF_BOARD);
            else if (board[row][col + 1] == '*')
                endGame1(BOMB);
            else {
                board[row][col] = '*';
                board[row][col + 1] = 'U';
                col = col + 1;
                ++i;
            }
            break;
        }
        }
    }
 
  • #15
I did it ! i figured it out! All I have to do is introduce another set of variables and assign the index value of 'U' to it!
Although if the user lost the game, it prints out why but then told me it had logic error, it didn't terminate correctly.
update: sometimes 'U' is not assigned. Help!

Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int numMoves = 0;
char userDirection;
int bombrow = 0;
int bombcol = 0;
int usercol = 0;
int userrow = 0;
int bombcount = 0;
int Row;
int Col;
string BOMB = "BANG! You're dead!";
string OFF_BOARD = "YOU FELL OFF THE BOARD!";

void printBoard (char board[5][5]) {
    for (int row = 0; row < 5; row++) {
		for (int col = 0; col < 5; col++) {
			cout << board[row][col] << " ";
		}
		cout << endl;
	}
}

string endGame1 (string BOMB) {
    cout << BOMB << endl;
    cout << "*** YOU LOSE! ***" << endl;
    return 0;
}

string endGame2 (string OFF_BOARD) {
    cout << OFF_BOARD << endl;
    cout << "*** YOU LOSE! ***" << endl;
    return 0;
}

int main() {
	char board[5][5];
	srand(time(0));
	numMoves = (rand() % 8) + 5;
	bombcount = (rand() % 5) + 3;
	usercol = rand() % 5;
	userrow = rand() % 5;

	for (int row = 0; row < 5; row++) {
		for (int col = 0; col < 5; col++) {
			board[row][col] = '_';
		}
	}
	for (int x = 0; x < bombcount; ++x) {
        bombcol = rand() % 5;
        bombrow = rand() % 5;
        board[bombrow][bombcol] = '*';
	}
	while ((board[userrow][usercol] != '*') && board[userrow][usercol] == '_') {
        board[userrow][usercol] = 'U';
        Col = usercol;
        Row = userrow;
		}
    int i = 0;
    while (i < numMoves) {
        cout << "SURVIVE " << numMoves - i << " MORE MOVES!" << endl;
        printBoard(board);
        cout << "Enter direction (N/S/E/W): ";
        cin >> userDirection;

        switch (userDirection) {
		case 'N':
			if (Row - 1 < 0)
				endGame2(OFF_BOARD);
			else if (board[Row - 1][Col] == '*')
					endGame1(BOMB);
			else {
				board[Row][Col] = '*';
				board[Row - 1][Col] = 'U';
				Row = Row - 1;
				++i;
			}
			break;
        case 'S':
            if (Row + 1 > 4)
                endGame2(OFF_BOARD);
            else if (board[Row + 1][Col] == '*')
                endGame1(BOMB);
            else {
                board[Row][Col] = '*';
                board[Row + 1][Col] = 'U';
                Row = Row + 1;
                ++i;
            }
            break;
        case 'W':
            if (Col - 1 < 0)
                endGame2(OFF_BOARD);
            else if (board[Row][Col - 1] == '*')
                endGame1(BOMB);
            else {
                board[Row][Col] = '*';
                board[Row][Col - 1] = 'U';
                Col = Col - 1;
                ++i;
            }
            break;
        case 'E':
            if (Col + 1 > 4)
                endGame2(OFF_BOARD);
            else if (board[Row][Col + 1] == '*')
                endGame1(BOMB);
            else {
                board[Row][Col] = '*';
                board[Row][Col + 1] = 'U';
                Col = Col + 1;
                ++i;
            }
            break;
        }
        }
        if (numMoves - i == 0) {
            cout << "*** YOU WIN! ***";
            return 0;
        }
    }
 
Last edited:
  • #16
Let's take a different approach. You need to organize your code, minimize the amount of variables you use and take advantage of some of the features of C++.

I'm going to post a template - debugging your code and posting alternatives isn't going to make your program easier to follow. Anyway, here's the template:

Code:
using namespace std;

const int NUM_ROWS = 5, NUM_COLS = 5, MIN_BOMBS = 3, MAX_BOMBS = 7, MIN_MOVES = 5, MAX_MOVES = 12, OFF_BOARD = 0, BOMB = 1, WIN = 2;
char board[NUM_ROWS][NUM_COLS];

// returns random number in range low, high
int randomNumber(int low, int high) {

// place code here

}

void endGame(int status) {

	if (status == OFF_BOARD)
		cout << endl << "YOU FELL OFF THE BOARD!" << endl << "*** YOU LOSE! ***" << endl;

	if (status == BOMB)
		cout << endl << "BANG! You're dead!" << endl << "*** YOU LOSE! ***" << endl;

	if (status == WIN)
		cout << "*** YOU WIN! ***" << endl;

	exit(status);

}
		

void drawBoard() {
		
	int i, j;
		
	for (i = 0; i < NUM_ROWS; i++) {
		for (j = 0; j < NUM_COLS; j++)
			cout << board[i][j] << ' ';

		cout << endl;
	}

	return;

}
		
int main () {

	int i, j, k, moves, numBombs, userPlaced, userRow, userCol;
	char direction;
		
	// initialize board		
	// place code here. use i and j for indices.

	// get number of bombs
	numBombs = randomNumber(MIN_BOMBS, MAX_BOMBS);
		
	// place bombs
        // use i, j to index, k to count the number of bombs that have been placed.

	// place user
        // use userPlaced, userRow, usercCol.

	moves = randomNumber(MIN_MOVES, MAX_MOVES);
	i = 0;
	while (i < moves) {

		cout << "SURVIVE " << moves - i << " MORE MOVES!" << endl;
		drawBoard();
		cout << "Enter direction (N/S/E/W): ";
		cin >> direction;

		switch (direction) {

			case 'N':
				if (userRow - 1 < 0) 
					endGame(OFF_BOARD);
				else if (board[userRow - 1][userCol] == '*')
					endGame(BOMB);
				else {
					board[userRow][userCol] = '*';
					board[userRow - 1][userCol] = 'U';
					userRow = userRow - 1;
					++i;
				}
				break;

			case 'S':
				if (userRow + 1 > NUM_ROWS - 1)
					endGame(OFF_BOARD);
				else if (board[userRow + 1][userCol] == '*')
					endGame(BOMB);
				else {
					board[userRow][userCol] = '*';
					board[userRow + 1][userCol] = 'U';
					userRow = userRow + 1;
					++i;
				}
				break;
				
			case 'E':
				if (userCol + 1 > NUM_COLS - 1)
						endGame(OFF_BOARD);
				else if (board[userRow][userCol + 1] == '*')
					endGame(BOMB);
				else {
					board[userRow][userCol] = '*';
					board[userRow][userCol + 1] = 'U';
					userCol = userCol + 1;
					++i;
				}
				break;
				
			case 'W':
				if (userCol - 1 < 0)
					endGame(OFF_BOARD);
				else if (board[userRow][userCol - 1] == '*')
					endGame(BOMB);
				else {
					board[userRow][userCol] = '*';
					board[userRow][userCol - 1] = 'U';
					userCol = userCol - 1;
					++i;
				}
				break;
	
			default:
				cout << "invalid direction" << endl;
				break;
		
		}

	}

	endGame(WIN);

}

Please, don't add any new variables.

Notice that [m]userRow[/m] and [m]userCol[/m] are used to track the user. The board is declared outside of any function so it is accessible to the functions that need it.
 
  • #17
greg1313 said:
Let's take a different approach. You need to organize your code, minimize the amount of variables you use and take advantage of some of the features of C++.

I'm going to post a template - debugging your code and posting alternatives isn't going to make your program easier to follow. Anyway, here's the template:

Code:
using namespace std;

const int NUM_ROWS = 5, NUM_COLS = 5, MIN_BOMBS = 3, MAX_BOMBS = 7, MIN_MOVES = 5, MAX_MOVES = 12, OFF_BOARD = 0, BOMB = 1, WIN = 2;
char board[NUM_ROWS][NUM_COLS];

// returns random number in range low, high
int randomNumber(int low, int high) {

// place code here

}

void endGame(int status) {

	if (status == OFF_BOARD)
		cout << endl << "YOU FELL OFF THE BOARD!" << endl << "*** YOU LOSE! ***" << endl;

	if (status == BOMB)
		cout << endl << "BANG! You're dead!" << endl << "*** YOU LOSE! ***" << endl;

	if (status == WIN)
		cout << "*** YOU WIN! ***" << endl;

	exit(status);

}
		

void drawBoard() {
		
	int i, j;
		
	for (i = 0; i < NUM_ROWS; i++) {
		for (j = 0; j < NUM_COLS; j++)
			cout << board[i][j] << ' ';

		cout << endl;
	}

	return;

}
		
int main () {

	int i, j, k, moves, numBombs, userPlaced, userRow, userCol;
	char direction;
		
	// initialize board		
	// place code here. use i and j for indices.

	// get number of bombs
	numBombs = randomNumber(MIN_BOMBS, MAX_BOMBS);
		
	// place bombs
        // use i, j to index, k to count the number of bombs that have been placed.

	// place user
        // use userPlaced, userRow, usercCol.

	moves = randomNumber(MIN_MOVES, MAX_MOVES);
	i = 0;
	while (i < moves) {

		cout << "SURVIVE " << moves - i << " MORE MOVES!" << endl;
		drawBoard();
		cout << "Enter direction (N/S/E/W): ";
		cin >> direction;

		switch (direction) {

			case 'N':
				if (userRow - 1 < 0) 
					endGame(OFF_BOARD);
				else if (board[userRow - 1][userCol] == '*')
					endGame(BOMB);
				else {
					board[userRow][userCol] = '*';
					board[userRow - 1][userCol] = 'U';
					userRow = userRow - 1;
					++i;
				}
				break;

			case 'S':
				if (userRow + 1 > NUM_ROWS - 1)
					endGame(OFF_BOARD);
				else if (board[userRow + 1][userCol] == '*')
					endGame(BOMB);
				else {
					board[userRow][userCol] = '*';
					board[userRow + 1][userCol] = 'U';
					userRow = userRow + 1;
					++i;
				}
				break;
				
			case 'E':
				if (userCol + 1 > NUM_COLS - 1)
						endGame(OFF_BOARD);
				else if (board[userRow][userCol + 1] == '*')
					endGame(BOMB);
				else {
					board[userRow][userCol] = '*';
					board[userRow][userCol + 1] = 'U';
					userCol = userCol + 1;
					++i;
				}
				break;
				
			case 'W':
				if (userCol - 1 < 0)
					endGame(OFF_BOARD);
				else if (board[userRow][userCol - 1] == '*')
					endGame(BOMB);
				else {
					board[userRow][userCol] = '*';
					board[userRow][userCol - 1] = 'U';
					userCol = userCol - 1;
					++i;
				}
				break;
	
			default:
				cout << "invalid direction" << endl;
				break;
		
		}

	}

	endGame(WIN);

}

Please, don't add any new variables.

Notice that [m]userRow[/m] and [m]userCol[/m] are used to track the user. The board is declared outside of any function so it is accessible to the functions that need it.

Thanks for the amazing help. But I still don't get how would I place the user while using [M]userRow and userCol[/M]
to keep track of the user.
 
  • #18
Set [m]userPlaced = 0[/m]. Then enter a [m]while[/m] loop like this: [m]while (!userPlaced) { ... }[/m].
Assign a random number between [m]0[/m] and [m]MAX_ROWS[/m] to [m]userRow[/m].
Assign a random number between [m]0[/m] and [m]MAX_COLS[/m] to [m]userCol[/m].

Then index the board, using [m]userRow, userCol[/m] and if [m]board[userRow][userCol] = '_'[/m], set [m]userPlaced = 1[/m]. If not, do nothing and let the loop repeat. Once you have the user's initial location you can continue with the rest of the program.

Then you've got the user's row and column location stored in [m]userRow[/m] and [m]userCol[/m].

Get it? Look at the [m]while (i < moves) { ... }[/m] loop in [m]main()[/m]. All of the action uses [m]userRow[/m] and [m]userCol[/m].
 
  • #19
greg1313 said:
Set [m]userPlaced = 0[/m]. Then enter a [m]while[/m] loop like this: [m]while (!userPlaced) { ... }[/m].
Assign a random number between [m]0[/m] and [m]MAX_ROWS[/m] to [m]userRow[/m].
Assign a random number between [m]0[/m] and [m]MAX_COLS[/m] to [m]userCol[/m].

Then index the board, using [m]userRow, userCol[/m] and if [m]board[userRow][userCol] = '_'[/m], set [m]userPlaced = 1[/m]. If not, do nothing and let the loop repeat. Once you have the user's initial location you can continue with the rest of the program.

Then you've got the user's row and column location stored in [m]userRow[/m] and [m]userCol[/m].

Get it? Look at the [m]while (i < moves) { ... }[/m] loop in [m]main()[/m]. All of the action uses [m]userRow[/m] and [m]userCol[/m].

Ah! I see now! Thank you so much!
 

FAQ: Survive the Maze: A Game of Strategy

What is the objective of "Survive the Maze: A Game of Strategy"?

The objective of "Survive the Maze: A Game of Strategy" is to navigate through a maze while avoiding obstacles and traps, and reach the end goal before your opponents do.

How many players can participate in "Survive the Maze: A Game of Strategy"?

The game can be played with 2-4 players.

How long does a typical game of "Survive the Maze: A Game of Strategy" last?

The duration of the game can vary depending on the players' strategies, but on average, it can last between 20-30 minutes.

Is "Survive the Maze: A Game of Strategy" suitable for all ages?

While the game involves strategic thinking and problem-solving, it can be enjoyed by players of all ages. However, younger children may require assistance from adults.

Can the maze pattern change in each round of "Survive the Maze: A Game of Strategy"?

Yes, the maze pattern is randomized in each round, making the game more challenging and unpredictable.

Similar threads

Replies
6
Views
3K
Replies
12
Views
3K
Replies
1
Views
4K
Replies
6
Views
1K
Replies
23
Views
957
Replies
36
Views
3K
Back
Top