Dungeon Crawls Problem

Hi, I'm new to these forums and new to C++. I started about a week ago and was looking for practice problems online when I ran into a thread from this forum with the following 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.

I wrote up the following code for it:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string input;
    char player = 'G', trap = 'T', goal = 'X';
    char grid[7][10];
    int i, j, posX = 1, posY = 1, skip = 0;
    for (i = 0; i < 7; ++i)
    {
        for (j = 0; j < 10; ++j)
            grid[i][j] = '.';
    }
    
    grid[1][1] = player;
    grid[2][6] = trap;
    grid[4][4] = trap;
    grid[5][6] = trap;
    grid[6][9] = goal;
    
    cout << "Welcome to Dungeon Crawl!\n";
    cout << "\nYou(G) need to get to the exit of the dungeon(X)";
    cout << " without hitting any of the traps(T)!\n";
    cout << "\nBest of luck to you!!\n";
    
    for (i = 0; i < 7; ++i)
    {
        for (j = 0; j < 10; ++j)
            cout << grid[i][j];
        cout << endl;
    }
    
    while (grid[6][9] != player)
    {
        do
        {
            cout << "\nIn which direction do you wanna move?\n";
            cout << "(up, down, left, right or quit):";
            cin >> input;
            
            if (input != "up" || input != "down" || input != "left" || input != "right" || input != "quit")
                cout << "\nThat's not a valid input. Try again.\n";
        } while (input != "up" || input != "down" || input != "left" || input != "right" || input != "quit");
        
        if (input == "up")
        {
            for (j = 0; j < 10; ++j)
            {
                if (grid[0][j] == player)
                {
                    cout << "You cannot move up from that position!\n\n";
                    skip = 1;
                }
            }
            if (skip == 0)
            {
                grid[posY][posX] = '.';
                grid[posY - 1][posX] = player;                
            }
        }
        
        if (input == "down")
        {
            for (j = 0; j < 9; ++j)
            {
                if (grid[6][j] == player)
                {
                    cout << "You cannot move down from that position!\n\n";
                    skip = 1;
                }
            }
            if (skip == 0)
            {
                grid[posY][posX] = '.';
                grid[posY + 1][posX] = player;
            }
        }
        
        if (input == "left")
        {
            for (i = 0; i < 7; ++i)
            {
                if (grid[i][0] == player)
                {
                    cout << "You cannot move left from that position!\n\n";
                    skip = 1;
                }
            }
            if (skip == 0)
            {
                grid[posY][posX] = '.';
                grid[posY][posX - 1] = player;
            }
        }
        
        if (input == "right")
        {
            for (i = 0; i < 6; ++i)
            {
                if (grid[i][9] == player)
                {
                    cout << "You cannot move right from that position!\n\n";
                    skip = 1;
                }
            }
            if (skip == 0)
            {
                grid[posY][posX] = '.';
                grid[posY][posX + 1] = player;
            }
        }
        
        if (input == "quit")
            break;

        if (grid[2][6] == player || grid[4][4] == player || grid[5][6] == player)
            cout << "It's a trap!";
            break;
        
        for (i = 0; i < 7; ++i)
        {
            for (j = 0; j < 10; ++j)
                cout << grid[i][j];
            cout << endl;
        }
        
        skip = 0;
    }
    
    if (grid[6][9] == 'G')
        cout << "You've escaped the dungeon!!";
    
    cout << "\n\nThanks for playing!";
    
    return 0;
}


A lot of this can probably be shortened, but with my limited knowledge, it was the best I can do.

The problem I am having with this is that when I run it, no matter what my input for direction is, it just keeps looping the section with the input and it tells me that it is an invalid input. Any help with this would be really appreciated. Thank you!
1
2
3
if (input != "up" || input != "down" || input != "left" || input != "right" || input != "quit")
                cout << "\nThat's not a valid input. Try again.\n";
        } while (input != "up" || input != "down" || input != "left" || input != "right" || input != "quit");


Switch the OR with AND. So, this:

1
2
3
if (input != "up" && input != "down" && input != "left" && input != "right" && input != "quit")
                cout << "\nThat's not a valid input. Try again.\n";
        } while (input != "up" && input != "down" && input != "left" && input != "right" && input != "quit");


This is what the program was doing with your code:
- input...user inputs up
- is input not up? False
- is it not down? True
- That's not a valid input. Try again

Just a simple mistake in the arguments.
Last edited on
HOT DAMN. Thank you so much for that!

But now I seem to be having a different problem. Regardless of what my input is, it skips all the rest of the code and outputs the last line "Thanks for playing!" then just shuts down. Why is the entirety of the code being skipped?
1
2
3
4
if (grid[2][6] == player || grid[4][4] == player || grid[5][6] == player)
            cout << "It's a trap!";
            break;
        


Should be...

1
2
3
4
5
6
if (grid[2][6] == player || grid[4][4] == player || grid[5][6] == player)
        {
            cout << "It's a trap!";
            break;
        }
        


Note the brackets. Before the break made it skip the loop.
thank you very much!
@MottMan thanks a lot for catching the mistakes.
@Tom Red
I then went and compiled the program but the game was not working as intended.
Then I altered the code trying as much as possible to stick to the exercise parameters and not going too far away from your way of doing it. In the end I managed to get this code.

Anyways , I think the program now works better. I added a small function for the map drawing routine and made it such that the map draws after each move. Check it out.

Also I am a newbie, so my programming practices are probably far from satisfactory. This I know. This code was just my attempt to getting the concept to work

http://codepad.org/dYsQp8QV
Last edited on
If you don't want the map to be drawn every time and wish it to remain hidden, just remove the function call line

gridIt(grid,0,0);

wherever necessary. I only used it so that I could get an idea of what was going with the map with each movement so I could check if everything was happening correctly.

Last edited on
Well it's probably better to let him figure out all the problems himself so he can learn as he goes. If referencing working code would help here is mine: http://codepad.org/RyvduP6E
http://codepad.org/RyvduP6E
Nice Mottman. Probably extensive and probably more in compliance with good programming techniques. Thanks for the link. I didn't know about codepad until now.

I am a newbie, so I made a very crude one here
http://codepad.org/3sJWyq2S

Last edited on
Codepad is great for sharing code and collaborating with others. You can make a sub-domain to share project code. Thanks, I am not the most proficient or well versed programmer either but that's just what I happened to come up with :)
@Mottman Great little project.
I got errors on line 56 and 86 which I commented out to make the prog work.
I was wondering what is the purpose of that code. ie; cout << string(20, '\n');
I dont think I've seen that before.


BTW the errors were;

Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion) c:\documents and settings\user\my documents\visual studio 2010\projects\dungeon crawl \dungeon crawl \main.cpp 86
2 IntelliSense: no operator "<<" matches these operands c:\documents and settings\user\my documents\visual studio 2010\projects\dungeon crawl \dungeon crawl \main.cpp 56
3 IntelliSense: no operator "<<" matches these operands c:\documents and settings\user\my documents\visual studio 2010\projects\dungeon crawl \dungeon crawl \main.cpp 86
Last edited on
It writes out 20 new lines. It's a better version of system calls. I edited some stuff since that code but I can upload my newer code if there is an interest. It should've worked though.
Oh right.
Yeah, my compiler just didnt like lines 56 and 86. After I commented out those cout<<string(20, 'n'); (line 86) and cout<<string(100, 'n'); (line 56),
it worked fine.
Topic archived. No new replies allowed.