Beginners Exercise, move traps around

closed account (EwCjE3v7)
I am doing the following exercise


Make a program that outputs a simple grid based gameboard to the screen using either numbers or characters.
i.e.

. . . . . . . . . .
. G . . . . . . . .
. . . . . . T . . .
. . . . . . . . . .
. . . . T . . . . .
. . . . . . T . . .
. . . . . . . . . X


or

0 0 0 0 0 0 0 0 0 0
0 5 0 0 6 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 7 0 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 0 0 4


Allow the user (marked by G in the example) to move either up, down, left, or right each turn. If the player steps on a trap then they lose. If the make it to the treasure 'X' then they win.

★★ Add enemies that move randomly in any direction once per turn. (enemies just like traps cause the player to lose if touched)

HINT: Don't let the player move off the gameboard! You program will crash if they move off the top or bottom of the board!
(the same holds true for enemies)


from http://www.cplusplus.com/forum/articles/12974/ but I have now clue how do the what's in the bold. My code is below and I would think that you would you rand in some way. Can anyone help me out and show me maybe how they would do it. Thank.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>
#include <cstdlib>
#include <time.h>
#include "conio.h" // getche() get char input
using namespace::std;


int main()
{
	char t = 'o';
    char board[7][19] = {{'-','-','-','-','-',t,'-',t,'-','-','-',t,'-',t,'-','-','-','-','-'},  // our board
                         {'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',t,'-','-'},
                         {'-','-','-','-','-','-',t,'-',t,'-','-','-','-','-',t,'X','-','-',t},
                         {'-',t,'-','-','-','-','-','-','-','-','-','-','-',t,'-','-','-',t,'-'},
                         {t,'-','-',t,'-','-','-',t,'-','-','-','-','-','-','-',t,'-','-','-'},
                         {'-','-','-','-','-',t,'-','-',t,'-','-','-','-','-','-','-',t,'-','-'},
                         {'-','P','-','-',t,'-',t,'-','-',t,'-',t,'-','-','-',t,'-','-','-'}};

	cout << "Get P to X using WASD. \n\n" << endl; // clear screen

	for (int i = 0; i != 7; ++i) { // print board
		for (int j = 0; j != 19; ++j)
			cout << board[i][j];

		cout << endl;
	}

	char move = 'q'; // getting next move from user

	int y = 6, x = 1; // where player spawns

	while (move = getche()) { // get next move
		board[y][x] = '-'; // previous position 
		cout << "Get P to X using WASD. \n\n" << endl; // clear screen TERMINAL ONLY

		switch (move) { // move our player position
			case 'w':
			case 'W':
				if (y > 0)
					y--;
				break;

			case 's':
			case 'S':
				if (y < 7)
					y++;
				break;

			case 'a':
			case 'A':
				if (x > 0)
					x--;
				break;

			case 'd':
			case 'D':
				if (x < 19)
					x++;
				break;

			default:
				cout << "Invalid input, try again." << endl;
				break;
		}

		if (board[y][x] == t) { // if player fell into a hole
			cout << "Game over, you fell into a hole." << endl;
			return 0;
		} else if (board[y][x] == 'X') { // if player found the treasure
			cout << "You found the treasure. HURRAY!!!" << endl;
			return 0;
		}

		board[y][x] = 'P'; // move player

		for (int i = 0; i != 7; ++i) { // print out our board
			for (int j = 0; j != 19; ++j)
				cout << board[i][j];

			cout << endl;
		}
	}
	return 0;
}


// EDITED TO WORK ON OTHER OSs
Last edited on
closed account (SECMoG1T)
It means you and some objects that will be moving in a randomized manner through you array but you traps remain fixe

eg

---------
t---p---t
--t-e--e-
-e--t---t
---e---x-

---------
t--p---et
--t--e---
e-e-t---t
-------x-

---p---e-
t--------t
e-t---e--
----t----t
--e----x-



Something Like that
closed account (EwCjE3v7)
// EDITED

Okay I think I've done it, thanks andy for that and I think I've added a to many enemies and traps but here is the code, have a shot at it if you like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Keiboard keis \ |
#include <iostream>
#include <vector>
#include <cstdlib>
#include <time.h>
#include "conio.h" // getche() get char input
using namespace::std;

/*
NOTE: This will only compile on LINUX due to the following line 'cout << "\033[2J\033[1;1H"'
*/



int main()
{

    char board[7][19] = {{'-','-','-','-','e','o','-','o','-','-','-','o','-','o','-','e','-','-','e'},  // our board
                         {'-','-','e','-','-','-','-','-','-','-','-','-','-','-','-','-','o','-','-'},
                         {'e','-','-','-','-','e','o','e','o','e','-','-','-','-','o','X','-','-','o'},
                         {'-','o','-','-','-','-','-','-','-','-','-','-','-','o','-','e','-','o','-'},
                         {'o','-','-','o','-','-','e','o','-','-','-','-','-','-','-','o','-','e','-'},
                         {'-','-','-','-','-','o','-','-','o','-','-','e','-','e','-','-','o','-','e'},
                         {'-','P','-','e','o','-','o','-','-','o','-','o','-','-','-','o','-','-','-'}};

	cout << "\033[2J\033[1;1H" << "Get P to X using WASD. \n\n" << endl; // clear screen

	for (int i = 0; i != 7; ++i) { // print board
		for (int j = 0; j != 19; ++j)
			cout << board[i][j];

		cout << endl;
	}

	char move = 'q'; // getting next move from user

	int y = 6, x = 1; // where player spawns

	while (move = getche()) { // get next move
		board[y][x] = '-'; // reset previous position
		cout << "\033[2J\033[1;1H" << "Get P to X using WASD. \n\n" << endl; // clear screen TERMINAL ONLY

		switch (move) { // move our player position
			case 'w':
			case 'W':
				if (y > 0)
					y--;
				break;

			case 's':
			case 'S':
				if (y < 6)
					y++;
				break;

			case 'a':
			case 'A':
				if (x > 0)
					x--;
				break;

			case 'd':
			case 'D':
				if (x < 18)
					x++;
				break;

			default:
				cout << "Invalid input, try again." << endl;
				break;
		}

		if (board[y][x] == 'o') { // if player fell into a hole
			cout << "Game over, you fell into a whole." << endl;
			return 0;
		} else if (board[y][x] == 'e') {

		} else if (board[y][x] == 'X') { // if player found the treasure
			cout << "You found the treasure. HURRAY!!!" << endl;
			return 0;
		}

		board[y][x] = 'P'; // move player

		// move traps

		srand (time(NULL));
		unsigned nextMove = rand() % 4 + 1;
		cout << nextMove << endl;
		for (int y = 0; y != 7; ++y) {
			for (int x = 0; x != 19; ++x){

				if (board[y][x] == 'e') {
					if (nextMove == 1 && y > 0) {
						if (board[y-1][x] == '-') {
							board[y][x] = '-';
							board[y-1][x] = 'e';
						} else if (board[y-1][x] == 'P') {
							cout << "Game over, an enemy has killed you!" << endl;
						}

					} else if (nextMove == 2 && y < 6) {
						if (board[y+1][x] == '-') {
							board[y][x] = '-';
							board[y+1][x] = 'e';
						} else if (board[y+1][x] == 'P') {
							cout << "Game over, an enemy has killed you!" << endl;
						}

					} else if (nextMove == 3 && x > 0) {
						if (board[y][x-1] == '-') {
							board[y][x] = '-';
							board[y][x-1] = 'e';
						} else if (board[y][x-1] == 'P') {
							cout << "Game over, an enemy has killed you!" << endl;
						}

					} else if (nextMove == 4 && x < 18) {
						if (board[y][x+1] == '-') {
							board[y][x] = '-';
							board[y][x+1] = 'e';
						} else if (board[y][x+1] == 'P') {
							cout << "Game over, an enemy has killed you!" << endl;
						}
					}
					
				}
			}
		}

		for (int i = 0; i != 7; ++i) { // print out our board
			for (int j = 0; j != 19; ++j)
				cout << board[i][j];

			cout << endl;
		}
	}
	return 0;
}
Last edited on
closed account (SECMoG1T)
Thets exactly what I meant good job lol, you can add as many enemies as you like but make sure
that thy don't make the game too hard for the person playing ,if the srand (time(NULL));
is within your loop move it to line 17 to avoid seeding the same value over and over again.
Last edited on
closed account (EwCjE3v7)
Thanks andy, and yes you would have to make it so it can be won, and about the stand thing it will have to be there because it needs to have a different value every move.
What he is saying is that srand() needs to NOT be inside the loop. You only need to seed the RNG once during the program.
Topic archived. No new replies allowed.