Trying to finish this game

Hi everyone, I've been working on this game where you are placed into a location in a matrix, and you need to go up, down, left, or right to get to the solution. I'm calling it 'Forest Maze'. So pretty much I start at 7,4 --- and I need to make it to 3,2. I have it working so far, but when I finally land on 3,2 my 'winner()' function does not kick in. Here's my code...

/*Forest Adventure code. The user enters directions one after another until they escape the forest*/

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int forest[7][7] = {{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,1,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}};
string dir;
int userX = 4;
int userY = 7;
int userLoc = forest[userY][userX];

void winner()
{
cout << "Congratulations, you found the exit!" << endl;
cout << "Play again sometime!" << endl;
cout << "Press any key to exit...";
cin.get();
}

int movement()
{
if (dir == "U")
{
cout << "You went up one area. Is this the exit...?" << endl;
userY--;
if (userLoc == 1)
{
userLoc = 1;
return 0;
}
else
{
cout << "No exit here... (Currently: " << userY << ", " << userX << ")" << endl << endl;
cin.get();
return 1;
}
}
else if (dir == "D")
{
cout << "You went down one area. Is this the exit...?" << endl;
userY++;
if (userLoc == 1)
{
userLoc = 1;
return 0;
}
else
{
cout << "No exit here... (Currently: " << userY << ", " << userX << ")" << endl << endl;
cin.get();
return 1;
}

}
else if (dir == "L")
{
cout << "You went left one area. Is this the exit...?" << endl;
userX--;
if (userLoc == 1)
{
userLoc = 1;
return 0;
}
else
{
cout << "No exit here... (Currently: " << userY << ", " << userX << ")" << endl << endl;
cin.get();
return 1;
}

}
else if (dir == "R")
{
cout << "You went down one area. Is this the exit...?" << endl;
userX++;
if (userLoc == 1)
{
userLoc = 1;
return 0;
}
else
{
cout << "No exit here... (Currently: " << userY << ", " << userX << ")" << endl << endl;
cin.get();
return 1;
}

}
};

int direction()
{
cout << "Which direction would you like to go?: ";
cin >> dir;
movement();
return userLoc;
}


int main()
{
cout << "Welcome to Forest Adventure!" << endl;
cout << "You are lost in a forest, find your way out!" << endl;
cout << "Type U, D, L, R when prompted to select which direction"<< endl;
cout << "you would like to go! (U=Up,D=Down,L=Left,R=Right)" << endl;
cout << "Have fun!" << endl << endl;

do
{
direction();
}
while (userLoc != 1);

winner();
cin.get();
return 0;
}

Any criticism is completely welcome, lots of redundant stuff going on here I think. Thanks in advance!!

You're using variable globals, you have no bounds checking, and an array starts at 0 not 1.

That being said, I assume this is probably one of your early games as you're just learning C++. Glad to see you're taking an initiative and aren't afraid to try things.

Things to research: using functions with variables, the cases of going out of bounds (when x or y is 7 or larger), and you're not actually using your array that you created.

Edit: I'm surprised it even compiles. Just looking at the variable userLoc, that should cause a runtime error. Also, changing the values of d and y will not change the values of userLoc.

Also, please be sure to use code tags. Highlight your code and click the <> button to the right.
Last edited on
Okie doke, I'll look into that. Thanks
It now compiles okay, but the winner() only triggers when I go to 3,2 instead of 3,3. Any ideas?

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
/*Forest Adventure code. The user enters directions one after another until they escape the forest*/

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

const int HEIGHT = 5;
const int LENGTH = 5;

int forest[HEIGHT][LENGTH] = {{0,0,0,0,0},
					{0,0,0,0,0},
					{0,0,1,0,0},
					{0,0,0,0,0},
					{0,0,0,0,0}};
string dir;
int userY, userX = 1;

void winner()
{	
	cout << "Congratulations, you found the exit!" << endl << endl;
	cout << "Play again sometime!" << endl << endl;
	cout << "Press any key to exit...";
	cin.get();
}


int movement()
{
	int userLoc = forest[userY][userX];
	if (dir == "U")
	{
		cout << "You went up one area. Is this the exit...?" << endl << endl;
		userY--;
		if (userY < 0)
		{
			cout << "Out of bounds! Try again!" << endl << endl;
			userY++;
			return 0;
		}
		if (userLoc == 1)
		{
			return 1;
		}
		else
		{
			cout << "No exit here... (Currently: " << userY << ", " << userX << ")" << endl << endl;
			cin.get();
			return userLoc;
		}
	}
	else if (dir == "D")
	{
		cout << "You went down one area. Is this the exit...?" << endl << endl;
		userY++;
		if (userY > 6)
		{
			cout << "Out of bounds! Try again!" << endl << endl;
			userY--;
			return 0;
		}
		if (userLoc == 1)
		{
			return userLoc;
		}
		else
		{
			cout << "No exit here... (Currently: " << userY << ", " << userX << ")" << endl << endl;
			cin.get();
			return userLoc;
		}

	}
	else if (dir == "L")
	{
		cout << "You went left one area. Is this the exit...?" << endl << endl;
		userX--;
		if (userX < 0)
		{
			cout << "Out of bounds! Try again!" << endl << endl;
			userX++;
			return 0;
		}
		if (userLoc == 1)
		{
			return userLoc;
		}
		else
		{
			cout << "No exit here... (Currently: " << userY << ", " << userX << ")" << endl << endl;
			cin.get();
			return userLoc;
		}

	}
	else if (dir == "R")
	{
		cout << "You went right one area. Is this the exit...?" << endl << endl;
		userX++;
		if (userX > 5)
		{
			cout << "Out of bounds! Try again!" << endl << endl;
			userX--;
			return 0;
		}
		if (userLoc == 1)
		{
			return userLoc;
		}
		else
		{
			cout << "No exit here... (Currently: " << userY << ", " << userX << ")" << endl << endl;
			cin.get();
			return userLoc;
		}

	}
};

int direction()
{
	cout << "Which direction would you like to go?: ";
	cin >> dir;
	int dirAns = movement();
	return dirAns;
}

int main()
{	
	int ans;
	string name;

	cout << "Welcome to Forest Adventure!" << endl;
	cout << "You are lost in a forest, find your way out!" << endl;
	cout << "Type U, D, L, R when prompted to select which direction"<< endl;
	cout << "you would like to go! (U=Up,D=Down,L=Left,R=Right)" << endl;
	cout << "First off, what's your last name, adventurer?: ";
	cin >> name;
	cout << endl << endl << name << ", you are in a 5 x 5 forest maze, one of these coordinates" << endl;
	cout << "leads to the exit, can you find out which one it is?" << endl << endl;

	do
	{
		ans = direction();
	}
	while (ans != 1);

	winner();
	cin.get();
	return 0;
}






Topic archived. No new replies allowed.