Dungeon crawler help

Everytime i press 'r', it does not move the 'P' char to the right like i want it to. Not sure what im doing wrong.

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
 
1

  #include<iostream>
#include<cstdlib>
#include<string>
using namespace std;






int main() {
	char play;
	char move;
	cout << "play?" << endl;
	cin >> play;
	bool validinput = true;
	bool gameon = false;
	
	
	
	
	if (play = 'y') {
		
		
		do {
			
char grid[10][10] = { { '*','*','*','*','*','*','*','*','*','*', },
			{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
			{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
			{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
			{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
			{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
			{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
			{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
			{ '*',' ',' ',' ',' ',' ',' ',' ',' ','*', },
			{ '*','*','*','*','*','*','*','*','*','*' } };


			char player = 'P', trap = 'X', goal = 'G', move;
			int x = 1, y = 1;
			grid[x][y] = player;
			grid[5][4] = trap;
			grid[8][8] = goal;


			for (int row = 0; row < 10; row++) {

			for (int column = 0; column < 10; column++) {
					cout << grid[row][column];
				}
				cout << endl;
			}


			
			do {

				cout << "move" << endl;
				

				cin >> move;

				if (move == 'r') {
					grid[x + 1][y] = player;
				}
			} while (!validinput);
		} while (!gameon);
	}

	system("pause");
	return 0;

}
@fivestar

In your do/while loop, you reinitialize the grid, and place the player back at the starting location. I corrected the coding a bit, and added in the four directional type moves. Checking also for trap or exit. This should get you set in the right direction..

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
#include<iostream>
#include<cstdlib>
#include<string>

using namespace std;

int main() 
{
	char play;
	char move;
	cout << "play?" << endl;
	cin >> play;
	bool validinput = true;
	bool gameon = false;
	char grid[10][10] = { 
	{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', },
	{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
	{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
	{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
	{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
	{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
	{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
	{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
	{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', },
	{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' }
	};

	char player = 'P', trap = 'X', goal = 'G';
	int x = 1, y = 1;
	grid[x][y] = player;
	grid[5][4] = trap;
	grid[8][8] = goal;
	// Above initializes variables
	do
	{
	if (play == 'y')
	{
		for (int row = 0; row < 10; row++)
		{
			for (int column = 0; column < 10; column++)
			{
				cout << grid[row][column];
			}
			cout << endl;
		}
			cout << "move" << endl;
			cin >> move;
			move = tolower(move);
			if (move == 'r') // Right
			{
				grid[x][y] = ' ';// Remove player from grid location
				y++; // Changes locations
				if(grid[x][y] == 'X') // Check if trap in new location
				{
					cout << "You died in the trap" << endl; // If yes, player dies
					// and game ends
				    gameon = true;
				}
				if (grid[x][y] == 'G') // Check if location is exit
				{
					cout << "You found the exit!!" << endl; // If yes, player wins
					// and game ends
					gameon = true;
				}
				grid[x][y] = player; // If not, move player to new location
			}
			if (move == 'd') // Down
			{
				grid[x][y] = ' ';
				x++;
				if (grid[x][y] == 'X')
				{
					cout << "You died in the trap" << endl;
					gameon = true;
				}
				if (grid[x][y] == 'G')
				{
					cout << "You found the exit!!" << endl;
					gameon = true;
				}
				grid[x][y] = player;
			}
			if (move == 'l') // Left
			{
				grid[x][y] = ' ';
				y--;
				if (grid[x][y] == 'X')
				{
					cout << "You died in the trap" << endl;
					gameon = true;
				}
				if (grid[x][y] == 'G')
				{
					cout << "You found the exit!!" << endl;
					gameon = true;
				}
				grid[x][y] = player;
			}
			if (move == 'u') // Up
			{
				grid[x][y] = ' ';
				x--;
				if (grid[x][y] == 'X')
				{
					cout << "You died in the trap" << endl;
					gameon = true;
				}
				if (grid[x][y] == 'G')
				{
					cout << "You found the exit!!" << endl;
					gameon = true;
				}
				grid[x][y] = player;
			}
		} 
	} while (!gameon);

	system("pause");
	return 0;
}
OHHH!!. Earlier this guy told me to put the grid in the loop. Am i just supposed to put the for loop part of the grid into the do while loop?
I appreciate the comments you put in, but can you explain lines 51 and 52. I dont completely understand those.
OHHH!!. Earlier this guy told me to put the grid in the loop. Am i just supposed to put the for loop part of the grid into the do while loop?

No. The guy told you if you want the updated grid to be displayed in the loop, and the grid is not being displayed in the loop, then you should probably display the grid in the loop. Not, put the grid in the loop.

http://www.cplusplus.com/forum/beginner/190033/#msg919653
Last edited on
Lol. Im still pretty new to this. My bad.
@fivestar

I appreciate the comments you put in, but can you explain lines 51 and 52. I dont completely understand those.


Line 51 - Removes the 'P' from the grid. Otherwise, a trail of the letter 'P' would show.
Line 52 - Increases the direction your character is located in the grid. Moving right, letter' r' you move to a higher number location in the column, y variable increases, while pressing 'd', increases the row. Hence increasing the x variable.
Thanks a lot.
Topic archived. No new replies allowed.