Dynamic variables

If I declare a local variable and give it a value, can I change that value or make it dynamic? For example, in my dungeon crawler game, I want the player to start at grid[x][y] and I set x = 0 and y = 0 when I declared them. But when the player chooses to move up, down, left, or right, those values of x and y need to change obviously.

I posted my code below and bolded the parts im referring to.
Thanks so much for everyone's help

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Dungeon Crawler.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	string imput;
	char player = 'G', trap = 'T', goal = 'X';
	char grid[7][10];
	int row, col, x = 0, y = 0, skip = 0;
	void showgrid();
	
	for(row = 0; row < 7; row++)// creates the grid 
	{
		for (col = 0; col < 10; col++)
		grid[row][col] = '.';
		
	}

	
	grid[x][y] = player; //Gives player's beginning position 
	grid[2][3] = trap; //Gives trap positions
	grid[3][6] = trap;
	grid[4][9] = trap;
	grid[5][4] = trap;
	grid[6][8] = trap;
	grid[6][9] = goal; //Gives finishing position
 
	void showgrid();
	{
	for (row = 0; row < 7; row++)  // Shows grid
	{
		for (col = 0; col < 10; col++)
			cout << grid[row][col];
			cout << "\n";
		
	}
	}

	while (grid[6][9] != player)
	{
		cout << "In which direction do you want to move?\n";
			cout << "up, down, left, right, or quit\n";
			cin >> imput;
			

			/*do
			{
				cout << "Please enter a valid move: up, down, left, right, quit\n";
				cin >> imput;
			}
			while(imput != "up" || imput != "down" || imput != "left" || imput != "right" || imput != "quit");*/ //Error catching if user doesn't imput valid move
	
		if(imput == "up")
		{
			for ( col = 0; col < 10; col++)
			{
				if( y == 0 && x > -1 && x < 10)
				{
					cout << "You cannot move up from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x][y - 1] = player; //re-locates player's position if move is valid
				}
			}
		}
		
		if(imput == "down")
		{
			for ( col = 0; col < 10; col++)
			{
				if(y == 6 && x > -1 && x < 10)
				{
					cout << "You cannot move down from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x][y + 1] = player; //re-locates player's position if move is valid

				}
			}
		}

		if(imput == "left")
		{
			for ( row = 0; row < 7; row++)
			{
				if( y > -1 && y < 6 && x == 0)
				{
					cout << "You cannot move left from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x - 1][y] = player; //re-locates player's position if move is valid
				}
			}
		}

		if(imput == "right")
		{
			for ( row = 0; row < 7; row++)
			{
				if( y > -1 && y < 10 && x == 9)
				{
					cout << "You cannot move right from that position\n";
					
				}
				else
				{
					grid[x][y] = '.';
					grid[x + 1][y] = player; //re-locates player's position if move is valid
				}
			}
		}
	}
//
//		if(imput == "quit") //Quits game if player chooses this command
//		{
//			break;
//		}
//
//		//The following is what happens if the player lands on one of the traps
//		if(grid[2][3] == player || grid[3][6] == player || grid[4][9] == player || grid[5][4] == player || grid[6][8] == player)
//		{
//			cout << "It's a trap!\n";
//			break;
//		}
//
//	for (row = 0; row < 7; row++)  // re-shows the grid 
//	{
//		for (col = 0; col < 10; col++)
//			cout << grid[row][col];
//
//		cout << "\n";
//	}
//
//	skip = 0;
//
//	if(grid[6][9] == player) //What happens if player successfully makes it to the goal
//	{
//		cout << "You successfully escaped the dungeon!\n";
//	}


	return 0;
}
> But when the player chooses to move up, down, left, or right,
> those values of x and y need to change obviously.
So change them.
1
2
3
4
5
switch( direction ){
case UP:
   ++y; break;
//...
}


¿what's the purpose of the loop in 62 ?
The loop in line 62 forms the maximum and minimum number of rows, so there are 6 rows. I can make a switch statement and have 4 cases for up, down, left, right. Is that what your saying?
This is the loop I'm referring to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
		if(imput == "up")
		{
			for ( col = 0; col < 10; col++)
			{
				if( y == 0 && x > -1 && x < 10)
				{
					cout << "You cannot move up from that position\n";
					cout << "In which direction do you want to move?\n";
					cout << "up, down, left, right, or quit\n";
					cin >> imput;
				}
				else
				{
					grid[x][y] = '.';
					grid[x][y - 1] = player; //re-locates player's position if move is valid
				}
			}
		}
Let's say that the condition in the `if' is false,
then it will execute the `else' block, ten times.


> I can make a switch statement and have 4 cases for up, down, left, right. Is that what your saying?
No. If you want for the variables to change, you need to change them.
You need to update them according to the movement.
If the if statement is false, then it will do the else part. I want to do the action once not ten times. How did you get ten times?

Is there any way I can call on each case of the switch statement from the "else" part? For example, under "else", I can put "case A" and it does that? Is that possible?
How did you get ten times?

You're running this in a loop: for ( col = 0; col < 10; col++)
Hence, ten times.

Is there any way I can call on each case of the switch statement from the "else" part?

What "else part"?
Can I call on the cases of a switch statement under the 'else' conditions of each 'if statement' in my code. Like the else part in line 121.
switch cases aren't functions, so no, you can't call them.
Which is about the right cue, because so far, your program consists only of a single function which is far too long.
For my for loop that you posted above, I thought that it is basically saying "for these values, this happens". I did not think that those values represent the number of times the condition happens. I was trying to program something that would repeat once every imput of "up, down, left, right".
Topic archived. No new replies allowed.