Grid movement

This is the "game" that i am learning to create.
Dungeon Crawl
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

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 game board! You program will crash if they move off the top or bottom of the board!
(the same holds true for enemies)

Here is the code below i use for the movement but every time i enter 1,2,3,4(i will change them later to a,s,d,w) i got duplicates of the '*' character and the old ones does not remove, also when i enter 5 or more i got lot's of lines of Wrong input and then program ends, if i use break it shows only once but still it ends.
Any advise you give me would be helpful.
thanks.




#include <iostream>
using namespace std;

int main()
{

char character = '*';
int x = 0, y = 3;
int lastx, lasty;
char grid[5][5] = { { '1', '2', '3', '4', '5' },
{ '1','2','3','4','5' },
{'1','2','3','4','5'},
{'1','2','3','4','5'},
{'1','2','3','4','5'}};
grid[x][y] = character;
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++)
{
cout << grid[i][j];
cout << " ";
}

cout << endl;
}
cout << endl;

while (1 == 1)

{
int a;
cin >> a;
asd:
if (a == 1)lasty = y ++;
if (a == 2) lasty = y --;
if (a == 3) lastx = x ++;
if (a == 4) lastx = x --;
while (a != 1 && a != 2 && a != 3 && a != 4) //If or while has the same effect
{
cout << "Wrong input" << endl;
break;
goto asd;
}

grid[lastx][lasty] = character;
system("cls");
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++)
{
cout << grid[i][j];
cout << " ";
}
cout << endl;
}
cout << endl;
}


return 0;
}
First, avoid goto, you seem to be getting confused already about the flow that your programming is taking due to that.
Your first problem is that even after the goto takes you back, the "a" variable is still set and it will just repeat until the while loop becomes false.

You could have something like this instead
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a = -1;
do 
{
    cin >> a;
    if (a == 1) lasty = y ++; //note that y++ returns the pre-incremented value of y,
                              //then increments y
    else if (a == 2) lasty = y --;
    else if (a == 3) lastx = x ++;
    else if (a == 4) lastx = x --;

    if (a < 1 || a > 4)
    {
        cout << "Wrong input" << endl;
    }
} while (a < 1 || a > 4);


i got duplicates of the '*' character and the old ones does not remove

There's nothing in the rest of your code that resets the old position back to an empty space. You only use lastx/y, but not the modified x/y to know what's changed.

Also, you should use code tags, it makes your code a lot more readable, click the "<>" box to the right of the text window when you edit a post.
Last edited on
Also consider a switch statement. It can generate faster code and it makes it clear that you're doing different things depending on the value of a.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch(a) {
case 1:
    lasty = y++;
    break;
case 2:
    lasty = y--;
    break;
case 3:
    lastx = x++;
    break;
case 4:
    lastx = x--;
    break;
default:
    cout << "Wrong input" << end;
    break;
}
sorry for the late reply, i would try like you wrote then i shall reply whatever i get.
thanks
Topic archived. No new replies allowed.