### Dungeon Crawl Help

Hi all,

I've been working on this exercise from the beginners exercise and I'm stuck. Here's the problem:

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.

And I wrote this code for it:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156`` ``````#include #include int main() { int posX=0, posY=0, skip=0, i, k; char dungeon[6][10]; std::string input; for (i=0;i<=5;i++) { for (k=0;k<=9;k++) { dungeon[i][k] ='O'; } } std::cout << "Hello, welcome to the dungeon game created by xbili" << std::endl; std::cout << "I hope you'll enjoy! To move simply press left, right, up or down." << std::endl; std::cout << "Avoid the traps denoted by the 'Y' sign!" << std::endl; std::cout << "Get to the finishing point denoted by the 'x' sign!" << std::endl; std::cout << "Your character is represented by the 'T' sign." << std::endl; std::cout << "Enjoy!!!\n\n\n\n\n\n\n" << std::endl; char player = 'T'; char trap = 'Y'; char finish = 'X'; dungeon[0][0] = player; dungeon[2][5] = trap; dungeon[3][8] = trap; dungeon[4][1] = trap; dungeon[5][9] = finish; for (i=0;i<=5;i++) { for (k=0;k<=9;k++) { std::cout << dungeon[i][k]; } std::cout << std::endl; } do { do { std::cout << "Where do you want to move? Up, down, left or right?"; std::cin >> input; if (input!="up" && input!="down" && input!="right" && input!="left") { std::cout << "That is not a valid input, try again.\n"; } } while (input!="up" && input!="down" && input!="right" && input!="left"); if (input == "up") { for (k=0;k<=9;k++) { if (dungeon[0][k]==player) { std::cout << "You cannot move up from there! \n"; skip = 1; } } if (skip == 0) { dungeon[posY][posX] = 'O'; dungeon[posY - 1][posX] = player; } } if (input == "down") { for (k=0;k<=9;k++) { if (dungeon[6][k]==player) { std::cout << "You cannot move down from there! \n"; skip=1; } } if (skip == 0) { dungeon[posY][posX] = 'O'; dungeon[posY+1][posX] = player; } } if (input == "left") { for (i=0;i<=9;i++) { if (dungeon[i][0]==player) { std::cout << "You cannot move left from there!\n"; skip=1; } } if (skip==0) { dungeon[posY][posX] = 'O'; dungeon[posY][posX-1] = player; } } if (input == "right") { for (i=0;i<=9;i++) { if(dungeon[i][9]==player) { std::cout << "You cannot move right from there!\n"; skip=1; } } if (skip==0) { dungeon[posY][posX] = 'O'; dungeon [posY][posX+1] = player; } } if (dungeon[2][5]==player || dungeon[3][8]==player || dungeon[4][1]==player) { std::cout << "It's a trap!"; break; } for (i=0;i<=5;i++) { for (k=0;k<=9;k++) { std::cout << dungeon[i][k]; } std::cout << std::endl; } skip = 0; } while (dungeon[5][9]!=player); if (dungeon[5][9]==player) { std::cout << "Congratulations! You have escaped the maze.\n"; } std::cout << "Thanks for playing!\n"; return 0; } ``````

My problem here is that the program will execute just once. i.e. It will make my player move in that direction by one step, and then afterwards nothing would happen no matter the input.

Any help would be greatly appreciated. Thanks!
Line 6 80 you are accessing the array out of bounds. The last row has index 5.

You also need to update posX and posY when you move the player.
Last edited on
Hi Peter87,

I'm sorry but I don't understand what you meant by accessing array out of bounds in line 6, I'm just declaring variables in line 6.

Also, the code to update the posX and posY when I move the player is in lines 71, 89, 106 and 124 where I did it with:

 `` `` ``dungeon [posY][posX+1] = player;``

The above is if I'm moving right. Or did I do it wrongly?

Sorry!
Sorry, I meant line 80.

`dungeon [posY][posX+1] = player;`
This line does not update posX and posY.
Hi Peter87, corrected the 6 to a 5 in line 80, and also added this in to update the position of posX and posY:

`posX = posX + 1;`

It works now, thanks for the help! Really appreciate it :)
 `` `` ``posX = posX + 1;``

If you want, you can write it a bit shorter.

 `` `` ``posX += 1;``

This will do the same thing.

There is another way to make it even shorter that works only because you change by 1.

 `` `` ``++posX;``

Use whatever way you prefer.
Thanks for the tip! Will certainly try it out since it shortens my code by a bit, for convenience sake too.
Topic archived. No new replies allowed.