Dungeon Crawl Help

Hey, i am creating a dungeon crawl game(2D Array)... It works, if i input 'l' it moves left, restart the program then i input 'r', i move right... great... Now implementing a while loop or a goto if loop i still get what i want...
However, after it re-iterates thru the loop, it no longer goes exatly where i tell it too. Now, the 2nd++ times around the loop it places the player in random places...

Trap: [6][2]
. . . . . . .
. P . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . T . . . X
Decision: u
Your move: 'u'
Player now at Index: [1][1]
Check Win: false
. P . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . T . . . X
Check Win: false
Decision: d
Your move: 'd' Now I try to go Down, but it doesnt go straight down... It goes to a random index

Player now at Index: [1][0]
Check Win: false
. . . . . . .
. . . . . . .
P . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . T . . . X
Check Win: false
Decision:

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MAX_SIZE = 7;
const char SPACE = '.';
const char player = 'P', trap = 'T', goal = 'X';

char createDungeon(char myArray[][MAX_SIZE], int rows, int cols, char space)
{
    int trapCount = 0;
    int NUM_TRAP = 3;

    int RandRow = rand() % 7; //generates a random number between 0 and 7
    int RandCol = rand() % 7; //generates a random number between 0 and 7
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            myArray[i][j] = space; //Here is the dungeon
        }
    }
    //while (trapCount < NUM_TRAP)
   // {
      // if (myArray[RandRow][RandCol] == '.')
       // {
           myArray[RandRow][RandCol] = trap; //Spawns three random traps
           cout << "Trap: " << "[" << RandRow << "]" << "[" << RandCol << "]" << endl;
         //  trapCount++;
       // }
    //}
            myArray[1][1] = player;// Spawns a player start
            myArray[6][6] = goal; //Spawns a treasure

   //return myArray;
}
/////Dungeon has been created, leave function alone, now updateDungeon() will keep you updated///
////////////////////////////////////////////////////////////////////////////////////////

void displayDungeon(char myArray[][MAX_SIZE], int rows, int cols, char space) //Output visual Dungeon
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << myArray[i][j] << " ";
        }
        cout << endl;
    }
}

int getPositionX(char myArray[][MAX_SIZE], int rows, int cols) //Reads from createDungeon() to get Players X position(index)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            if (myArray[i][j] == player)
            {
                return i; //Return X index number
            }
        }
    }
}
int getPositionY(char myArray[][MAX_SIZE], int rows, int cols) //Reads from createDungeon() to get Players Y position(index)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            if (myArray[i][j] == player)
            {
                return j; //return Y index number
            }
        }
    }
}

void getMove(char myArray[][MAX_SIZE], int posY, int posX, char space)
{
    //Reads from getPositionY() and getPositionX() and adds or subtracts from those and uses those numbers
    // as new index's for Player.
    char MOVE;
    cout << "Decision: ";
    cin >> MOVE;

    while (MOVE != 'u' && MOVE != 'd' && MOVE != 'l' && MOVE != 'r')
    {
        cout << "Error, try again: " << endl;
        cin >> MOVE;
    }
    if (MOVE == 'u')
    {
        myArray[posX][posY] = space;
        myArray[posY - 1][posX] = player; //Ex: index[1][1] Now equals index[0][1] So player has moved Upwards on the Dungeon
        cout << "Your move: " << "'" << MOVE << "'" << endl;

    }
    else if (MOVE == 'd')
    {
        myArray[posX][posY] = space;
        myArray[posY + 1][posX] = player;
        cout << "Your move: " << "'" << MOVE << "'" << endl;

    }
    else if (MOVE == 'l')
    {
        myArray[posX][posY] = space;
        myArray[posY][posX - 1] = player;
        cout << "Your move: " << "'" << MOVE << "'" << endl;

    }
    else if (MOVE == 'r')
    {
        myArray[posX][posY] = space;
        myArray[posY][posX + 1] = player;
        cout << "Your move: " << "'" << MOVE << "'" << endl;
    }
        cout << "Player now at Index: " << "[" << posY << "]" << "[" << posX << "]" << endl;
}
void updateDungeon(char myArray[][MAX_SIZE], int rows, int cols, int space, int posY, int posX) //Updates Player position onto a new dungeon
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < cols; j++)
        {
            myArray[i][j];
        }

            myArray[posY][posX] = player; //New player position from getX() and getY()

    }
}


bool checkMove(char myArray[][MAX_SIZE], int posY, int posX)    //Checks if getPositionY() and getX() are same as trap's index
{

    if (myArray[posY][posX] == trap)
    {
        cout << "You've landed on a trap! Game OVER..." << endl;
        return true;

    }
    if (myArray[posY][posX] == goal)
    {
        cout << "You've captured the treasure! Good game!" << endl;
        return true;

    }
        cout << "Check Win: false" << endl;
        return false;

}
int main()
{

    srand ( time(NULL) ); //initialize the random seed
    char DUNGEON[MAX_SIZE][MAX_SIZE] = {};
//Initializing////////////////////Dungeon/////////////////////////////
    createDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);////
    displayDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);//

//
Start:

    //Ask for move
    getMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE), SPACE);
    //Update Dungeon
    updateDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE, getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE));
    checkMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE));
    displayDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);
    if (checkMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE)) == false)
        {
            goto Start;
        }

}
Last edited on
Please edit your post to put [code][/code] tags around your code (the <> format).
Added the
Last edited on
Before someone else says it : avoid as much as you can using goto, it's generally sign of bad design. Instead for your main function you can use a do while loop with a flag:

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
int main()
{
    srand ( time(NULL) ); //initialize the random seed
    char DUNGEON[MAX_SIZE][MAX_SIZE] = {};
    bool restart = false;    //initialise flag
//Initializing////////////////////Dungeon/////////////////////////////
    createDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);////
    displayDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);//

//
    do
    {
        //Ask for move
        getMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE), SPACE);
        //Update Dungeon
        updateDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE, getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE));
        checkMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE));
        displayDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);
        if (checkMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE)) == false)
        {
            restart = true;
        }
    }
    while (restart);
}


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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MAX_SIZE = 7;
const char SPACE = '.';
const char player = 'P', trap = 'T', goal = 'X';

char createDungeon(char myArray[][MAX_SIZE], int rows, int cols, char space)
{
    int trapCount = 0;
    int NUM_TRAP = 3;

    int RandRow = rand() % 7; //generates a random number between 0 and 7
    int RandCol = rand() % 7; //generates a random number between 0 and 7
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            myArray[i][j] = space; //Here is the dungeon
        }
    }
    //while (trapCount < NUM_TRAP)
   // {
      // if (myArray[RandRow][RandCol] == '.')
       // {
           myArray[RandRow][RandCol] = trap; //Spawns three random traps
           cout << "Trap: " << "[" << RandRow << "]" << "[" << RandCol << "]" << endl;
         //  trapCount++;
       // }
    //}
            myArray[1][1] = player;// Spawns a player start
            myArray[6][6] = goal; //Spawns a treasure

   //return myArray;
}
/////Dungeon has been created, leave function alone, now updateDungeon() will keep you updated///
////////////////////////////////////////////////////////////////////////////////////////

void displayDungeon(char myArray[][MAX_SIZE], int rows, int cols, char space) //Output visual Dungeon
{
	for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << myArray[i][j] << " ";
        }
        cout << endl;
    }
}

int getPositionX(char myArray[][MAX_SIZE], int rows, int cols) //Reads from createDungeon() to get Players X position(index)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            if (myArray[i][j] == player)
            {
                return i; //Return X index number
            }
        }
    }
}
int getPositionY(char myArray[][MAX_SIZE], int rows, int cols) //Reads from createDungeon() to get Players Y position(index)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            if (myArray[i][j] == player)
            {
                return j; //return Y index number
            }
        }
    }
}

void getMove(char myArray[][MAX_SIZE], int posY, int posX, char space)
{
    //Reads from getPositionY() and getPositionX() and adds or subtracts from those and uses those numbers
    // as new index's for Player.
    char MOVE;
    cout << "Decision: ";
    cin >> MOVE;

    while (MOVE != 'u' && MOVE != 'd' && MOVE != 'l' && MOVE != 'r')
    {
        cout << "Error, try again: " << endl;
        cin >> MOVE;
    }
    if (MOVE == 'u')
    {
        myArray[posX][posY] = space;
        myArray[posY - 1][posX] = player; //Ex: index[1][1] Now equals index[0][1] So player has moved Upwards on the Dungeon
        cout << "Your move: " << "'" << MOVE << "'" << endl;

    }
    else if (MOVE == 'd')
    {
        myArray[posX][posY] = space;
        myArray[posY + 1][posX] = player;
        cout << "Your move: " << "'" << MOVE << "'" << endl;

    }
    else if (MOVE == 'l')
    {
        myArray[posX][posY] = space;
        myArray[posY][posX - 1] = player;
        cout << "Your move: " << "'" << MOVE << "'" << endl;

    }
    else if (MOVE == 'r')
    {
        myArray[posX][posY] = space;
        myArray[posY][posX + 1] = player;
        cout << "Your move: " << "'" << MOVE << "'" << endl;
    }
        //cout << "Player now at Index: " << "[" << posY << "]" << "[" << posX << "]" << endl;
        //THIS DOESN'T WORK SINCE YOU DON'T ACTUALLY CHANGE X AND Y
}
void updateDungeon(char myArray[][MAX_SIZE], int rows, int cols, int space, int posY, int posX) //Updates Player position onto a new dungeon
{
    for(int i = 0; i < rows; i++)
    {
        for(int j = 0; j < cols; j++)
        {
            myArray[i][j];
        }

            myArray[posY][posX] = player; //New player position from getX() and getY()
    }
    
    cout << "\n\nPlayer now at Index: " << "y[" << posY << "] " << "x[" << posX << "]" << endl;
}


bool checkMove(char myArray[][MAX_SIZE], int posY, int posX)    //Checks if getPositionY() and getX() are same as trap's index
{

    if (myArray[posY][posX] == trap)
    {
        cout << "You've landed on a trap! Game OVER..." << endl;
        return true;

    }
    if (myArray[posY][posX] == goal)
    {
        cout << "You've captured the treasure! Good game!" << endl;
        return true;

    }
        cout << "Check Win: false" << endl;
        return false;

}
int main()
{

    srand ( time(NULL) ); //initialize the random seed
    char DUNGEON[MAX_SIZE][MAX_SIZE] = {};
    
    bool flag = true;
//Initializing////////////////////Dungeon/////////////////////////////
    createDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);////
    displayDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);//

//
	do
	{
    	//Ask for move
    	getMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE), SPACE);
    	//Update Dungeon
    	updateDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE, getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE));
    	
    	if (checkMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE)))
    	{
    	    flag = false;
    	}
    	else
    	{
    		cout << "\nY " << getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE)
    			<< "\nX " << getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE)
    			<< "\n\n";
    	}
    	
    	displayDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);
    }
	while (flag);

}


I tried to fiddle a bit with your code and found something which might lead you to solve your problem.
When you display the player index you show Y first and X second, however if you display x and y from their respective functions getPositionX(), getPositionY()
you see that you switched them around.
You don't need to pass MAX_SIZE and SPACE around.

Use a struct to hold row and col so you don't need to pass them around separately.

If you keep track of the player's position you don't need to search for it over and over. It's probably best not to store the player symbol in the grid. Just keep track of the player's position and pass it to the displayDungeon function along with the dungeon.

Not only are you passing the Y, X in the wrong order to updateDungeon, the function isn't really doing anything anyway (just saying myArray[i][j] doesn't do anything, and you've already placed the player).

And as hoogo mentioned, using goto is almost always 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
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

struct Position
{
    int row, col;
    Position(int r = 0, int c = 0) : row(r), col(c) { }
};

const int SIZE = 7;
const int NUM_TRAPS = 3;
const char SPACE = '.', PLAYER = 'P', TRAP = 'T', GOAL = 'X';
const Position PlayerStart(1, 1);
const Position GoalPosition(SIZE-1, SIZE-1);

ostream& operator<<(ostream& os, const Position& pos)
{
    return os << '[' << pos.row << ',' << pos.col << ']';
}

void createDungeon(char dungeon[][SIZE])
{
    for (int r = 0; r < SIZE; ++r)
        for (int c = 0; c < SIZE; ++c)
            dungeon[r][c] = SPACE;

    dungeon[GoalPosition.row][GoalPosition.col] = GOAL;
    // temporarily place player symbol in grid while we set the traps
    dungeon[PlayerStart.row][PlayerStart.col] = PLAYER;

    for (int trapCount = 0; trapCount < NUM_TRAPS; )
    {
        Position rnd(rand() % 7, rand() % 7);
        if (dungeon[rnd.row][rnd.col] != SPACE)
            continue;

        dungeon[rnd.row][rnd.col] = TRAP;
        ++trapCount;

        //cout << "Trap: " << rnd << '\n';
    }

    // remove player symbol from grid
    dungeon[PlayerStart.row][PlayerStart.col] = SPACE;
}

void displayDungeon(char dungeon[][SIZE], const Position& pos)
{
    for (int r = 0; r < SIZE; ++r)
    {
        for (int c = 0; c < SIZE; ++c)
            if (r == pos.row && c == pos.col)
                cout << PLAYER << ' ';
            else
                cout << dungeon[r][c] << ' ';
        cout << '\n';
    }
}

void getMove(Position& pos)
{
    //cout << "Player1: " << pos << '\n';
    Position newPos;

    while (true)
    {
        char move;

        while (true)
        {
            cout << "Move: ";
            cin >> move;
            if (move == 'u' || move == 'd' || move == 'l' || move == 'r')
                break;
            cout << "Invalid direction, must be u, d, l, or r.\n";
        }

        newPos = pos;
        if      (move == 'u') --newPos.row;
        else if (move == 'd') ++newPos.row;
        else if (move == 'l') --newPos.col;
        else if (move == 'r') ++newPos.col;

        if (newPos.col >= 0 && newPos.col < SIZE &&
            newPos.row >= 0 && newPos.row < SIZE)
            break;

        cout << "You can't move that way.\n";
    }

    pos = newPos;
    //cout << "Player2: " << pos << '\n';
}

bool checkMove(char dungeon[][SIZE], const Position& pos)
{
    if (dungeon[pos.row][pos.col] == TRAP)
    {
        cout << "You've landed on a TRAP! Game OVER...\n";
        return true;
    }
    if (dungeon[pos.row][pos.col] == GOAL)
    {
        cout << "You've captured the treasure! Good game!\n";
        return true;
    }
    return false;
}

int main()
{
    srand(time(0));

    char dungeon[SIZE][SIZE];
    createDungeon(dungeon);

    Position pos(PlayerStart);
    displayDungeon(dungeon, pos);

    do
    {
        getMove(pos);
        displayDungeon(dungeon, pos);
    }
    while (!checkMove(dungeon, pos));
}

Last edited on
Wow, struct really simplified everything. I'll be sure to be making more data types and groups with this thanks! And yeah, i know goto is never good, i just used it as a place holder until i fixed the random indexing issues. Thanks for your help everyone, time to start the next phase of the project :)!

Also sorry for the X and Y positions being mixed up... I noticed it when i woke up today :D I was definitely throwing too much in at the same time and really went through my code too quickly...
Topic archived. No new replies allowed.