| Hoggmeiser (3) | |
|
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!! | |
|
|
|
| Volatile Pulse (1329) | |
|
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
|
|
| Hoggmeiser (3) | |
| Okie doke, I'll look into that. Thanks | |
|
|
|
| Hoggmeiser (3) | |||
It now compiles okay, but the winner() only triggers when I go to 3,2 instead of 3,3. Any ideas?
| |||
|
|
|||