I'm making the class or whatever that will let the computer tell where the player is at according to an array. How could I have the program test for the characters within the array and do things according to what character is in the position the player is in. Here's the code:
I'm probably doing this the wrong way totally, any help would be much appreciated. Thanks in advance.Code:#include <iostream> using namespace std; #define NUM_ROWS 3 #define Church_ID 'c' #define Item_ID 'i' #define Weapon_ID 'w' #define Armor_ID 'a' #define Road_ID 'r' #define Wall_ID '*' /*********************/ //c = church //i = item shop //* = not accessible //w = weapon shop //a = armor shop //r = road /*********************/ char *FunkyTown[NUM_ROWS] = {"cri", "wra", "*r*"}; class move { private: int xpos; int ypos; int ymax; int ymin; int xmax; int xmin; int previousYpos; int previousXpos; public: move() { xpos = 1; ypos = 3; ymin = 1; ymax = NUM_ROWS; xmin = 0; xmax = 2; } void position(int value) { previousYpos = ypos; previousXpos = xpos; if(value == 1) { ypos--; if(ypos <= 0) {ypos = ymin;} else if(ypos > 3) {ypos = ymax;} } else if(value == 2) { ypos++; if(ypos <= 0) {ypos = ymin;} else if(ypos > 3) {ypos = ymax;} } else if(value == 3) { xpos--; if(xpos <= 0) {xpos = xmin;} else if(xpos > 2) {xpos = xmax;} } else { xpos++; if(xpos <= 0) {xpos = xmin;} else if(xpos > 2) {xpos = xmax;} } if(xpos == 0 && ypos == 3 || xpos == 2 && ypos == 3) //this is the easy way I test { //to see if the player hits a wall xpos = previousXpos; //without using the array at all. ypos = previousYpos; cout << "You can't go that way, its a wall." << endl; } } void displayPos() { cout << "Current Position: " << (xpos+1) << "," << ypos << endl; } }; void main() { move character; int choice = 0; cout << "Movement Test v1.0" << endl; while(choice!=5) { cout << "1 Move North" << endl; cout << "2 Move South" << endl; cout << "3 Move West" << endl; cout << "4 Move East" << endl; cout << "5 Quit" << endl; cout << "Choose: "; cin >> choice; system("cls"); switch(choice) { case 1: character.position(choice); break; case 2: character.position(choice); break; case 3: character.position(choice); break; case 4: character.position(choice); break; case 5: break; } character.displayPos(); } }
-Psycho