DungeonCrawl movement issue

Hello,

I have the following code for the DungeonCrawl game. For some reason, the movement function is not correct for the right and the down direction. Can someone, please, point me the mistake?

game.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

const int MAPWIDTH = 7;
const int MAPHEIGHT = 10;

void explainGame(char y);
void clearMap(char Map[MAPHEIGHT][MAPWIDTH]);
void initMap(char Map[MAPHEIGHT][MAPWIDTH]);
void drawMap(char Map[MAPHEIGHT][MAPWIDTH]);
bool gameState(char Map[MAPHEIGHT][MAPWIDTH]);
void movePlayer(char Map[MAPHEIGHT][MAPWIDTH], char keyPress);
int checkWin(char Map[MAPHEIGHT][MAPWIDTH], int X);

#endif // GAME_H_INCLUDED 


game.cpp:

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
#include "game.h"
#include <iostream>
#include <conio.h>

void clearMap(char Map[MAPHEIGHT][MAPWIDTH])
{
    for(int i = 0; i<=MAPHEIGHT; i++)
    {
        for(int j = 0; j<=MAPWIDTH; j++)
        {
            Map[i][j] = '.';
        }
    }
}

void initMap(char Map[MAPHEIGHT][MAPWIDTH]){

    Map[1][1] = 'G';
    Map[6][2] = 'T';
    Map[4][4] = 'T';
    Map[5][6] = 'T';
    Map[10][7] = 'X';
}

void drawMap(char Map[MAPHEIGHT][MAPWIDTH])
{
    for(int i = 0; i<=MAPHEIGHT; i++)
    {
        for(int j = 0; j<=MAPWIDTH; j++)
        {
                std::cout<<Map[i][j];
        }
        std::cout<<"\n";
    }
}

void movePlayer(char Map[MAPHEIGHT][MAPWIDTH], char keyPress)
{
    keyPress = _getch();
    int w,h;
    for(int i = 0; i<MAPHEIGHT; i++)
    {
        for(int j = 0; j<MAPWIDTH; j++)
        {
            if(Map[i][j] == 'G')
            {
                h = i;
                w = j;
                break;
            }
        }
    }
    switch(keyPress)
    {
    case('w'):
        if(h>0)
        {
            Map[h-1][w] = 'G';
            Map[h][w] = '.';
        } else{
            Map[h][w] = 'G';
        }
        break;
    case('s'):
        if(h<MAPHEIGHT)
        {
            Map[h+1][w] = 'G';
            Map[h][w] = '.';
        } else{
            Map[h][w] = 'G';
        }
        break;
    case('a'):
        if(w>0)
        {
            Map[h][w-1] = 'G';
            Map[h][w] = '.';
        } else {
            Map[h][w] = 'G';
        }
        break;
    case('d'):
        if(w<MAPWIDTH)
        {
            Map[h][w+1] = 'G';
            Map[h][w] = '.';
        } else {
            Map[h][w] = 'G';
        }
        break;
    }
}

bool gameState(char Map[MAPHEIGHT][MAPWIDTH])
{
    if(Map[6][2] == 'G' || Map[4][4] == 'G' || Map[5][6] == 'G' || Map[10][7] == 'G')
    {
        return false;
    } else{
        return true;
    }
}

int checkWin(char Map[MAPHEIGHT][MAPWIDTH], int X)
{
    if(Map[6][2] == 'G' || Map[4][4] == 'G' || Map[5][6] == 'G')
    {
        X = 0;
        return X;
    } else if(Map[10][7] == 'G'){
        X = 1;
        return X;
    }
}


main.cpp:

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
// DungeonCrawler.cpp : Defines the entry point for the console application.


#include <iostream>
#include <stdlib.h>
#include "game.h"

using namespace std;

int main()
{
    char dungeon[MAPHEIGHT][MAPWIDTH];
    char keyPress;
    int X;

    clearMap(dungeon);
    initMap(dungeon);
    drawMap(dungeon);
    while(gameState(dungeon) == true)
    {
        movePlayer(dungeon, keyPress);
        drawMap(dungeon);
        if(checkWin(dungeon, X)== 1)
        {
            cout<<"You won!\n";
        } else if(checkWin(dungeon, X)== 0){
            cout<<"Game lost!\n";
        }
    }
    system("pause");
    return 0;
}
@mihaijulien

Try getting rid of all those right parenthesis just before the command 'break;' in your switch statements.
That didn't fix the bug.
Define "not correct". What is the observed and expected behaviors?
When moving the character down, it won't reach the last line. Also, when moving the character to the right, when it reaches the end of the line, it automatically goes on the next row.

I'm not sure how is my movement function wrong, cause it work as expected when moving up and to the left.
@mihaijulien

Sorry about my first response. Actually I missed seeing the left parenthesis after the else keywords, so thought you had misplaced right parenthesis'. I corrected the coding for you. Also put in explanations as to what I did. A big problem you were having was that you were going out of bounds in the dungeon array. Arrays are 0 to the maximum, minus one. So MAPWIDTH of 7 is 0 to 6, and MAPHEIGHT of 10 is 0 to 9. I also am showing your code as a single source code. It's just easier for me to program that way.

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
#include <iostream>
#include <conio.h>
#include <stdlib.h>

const int MAPWIDTH = 7;
const int MAPHEIGHT = 10;

void explainGame(char y);
void clearMap(char Map[MAPHEIGHT][MAPWIDTH]);
void initMap(char Map[MAPHEIGHT][MAPWIDTH]);
void drawMap(char Map[MAPHEIGHT][MAPWIDTH]);
bool gameState(char Map[MAPHEIGHT][MAPWIDTH]);
void movePlayer(char Map[MAPHEIGHT][MAPWIDTH], char keyPress);
int checkWin(char Map[MAPHEIGHT][MAPWIDTH], int X);

using namespace std;

int main()
{
	char dungeon[MAPHEIGHT][MAPWIDTH];
	char keyPress = ' ';
	int X=0;

	clearMap(dungeon);
	initMap(dungeon);
	drawMap(dungeon);
	while (gameState(dungeon) == true)
	{
		movePlayer(dungeon, keyPress);
		drawMap(dungeon);
		if (checkWin(dungeon, X) == 1)
		{
			cout << "You won!\n";
		}
		else if (checkWin(dungeon, X) == 0)
		{
			cout << "Game lost!\n";
		}
	}
	system("pause");
	return 0;
}

void clearMap(char Map[MAPHEIGHT][MAPWIDTH])
{
	for (int i = 0; i < MAPHEIGHT; i++)
	{
		for (int j = 0; j < MAPWIDTH; j++)
		{
			Map[i][j] = '.';
		}
	}
}

void initMap(char Map[MAPHEIGHT][MAPWIDTH]){

	Map[0][0] = 'G'; // Top left corner 
	Map[6][2] = 'T';
	Map[4][4] = 'T';
	Map[5][5] = 'T';
	Map[9][6] = 'X'; // Bottom right corner
	// Array is 0 to 6 or 7 wide
	// and 0 to 9 or 10 high

}

void drawMap(char Map[MAPHEIGHT][MAPWIDTH])
{
	for (int i = 0; i < MAPHEIGHT; i++) // Removed the'=' signs to keep inside array
	{
		for (int j = 0; j < MAPWIDTH; j++)
		{
			std::cout << Map[i][j];
		}
		std::cout << "\n";
	}
	std::cout << "\n"; // Extra linefeed, to separate each Dungeon map being printed
}

void movePlayer(char Map[MAPHEIGHT][MAPWIDTH], char keyPress)
{
	keyPress = _getch();
	int w, h;
	for (int i = 0; i<MAPHEIGHT; i++)
	{
		for (int j = 0; j<MAPWIDTH; j++)
		{
			if (Map[i][j] == 'G')
			{
				h = i;
				w = j;
				break;
			}
		}
	}
	switch (keyPress)
	{
	case 'w':
		if (h > 0) 
		{
			Map[h - 1][w] = 'G';
			Map[h][w] = '.';
		}
		else
		{
			Map[h][w] = 'G';
		}
		break;
	case 's' :
		if (h < MAPHEIGHT-1)
		{
			Map[h + 1][w] = 'G';
			Map[h][w] = '.';
		}
		else
		{
			Map[h][w] = 'G';
		}
		break;
	case 'a' :
		if (w>0)
		{
			Map[h][w - 1] = 'G';
			Map[h][w] = '.';
		}
		else
		{
			Map[h][w] = 'G';
		}
		break;
	case 'd':
		if (w < MAPWIDTH-1)
		{
			Map[h][w + 1] = 'G';
			Map[h][w] = '.';
		}
		else
		{
			Map[h][w] = 'G';
		}
		break;
	}
}

bool gameState(char Map[MAPHEIGHT][MAPWIDTH])
{
	if (Map[6][2] == 'G' || Map[4][4] == 'G' || Map[5][5] == 'G' || Map[9][6] == 'G')
	{
		return false;
	}
	else
	{
		return true;
	}
}

int checkWin(char Map[MAPHEIGHT][MAPWIDTH], int X)
{
	X = -1; // Set X 
	if (Map[6][2] == 'G' || Map[4][4] == 'G' || Map[5][5] == 'G') // Location of traps
	{
		X = 0; // Change IF you hit a trap
	}
	else if (Map[9][6] == 'G')
	{
		X = 1; // Show you reached EXIT
	}
	return X;
}
Last edited on
Thank you, that really fixed the problem. :)
Topic archived. No new replies allowed.