### How to update while loop until its correct?

Im having trouble trying to get my while loop to update until it satisfies the function. The goal is to match the x1, x2 with y1 and y2. Any help will be great. The following is the code.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107`` ``````// Include statements #include #include #include /* srand, rand */ #include /* time */ using namespace std; //map size; const int X_DIM = 20; const int Y_DIM = 10; const int MAX_X = X_DIM - 1; // valid locations are 0..X_DIM - 1 const int MAX_Y = Y_DIM - 1; // valid locations are 0..Y_DIM - 1 const int FAST = 4; // Number of moves for a fast win const int SLOW = 8; // Number of moves for a slow win int main() { int TreasureX, TreasureY; // The location of the treasure chest int StartX, StartY; // The player's starting location int X,Y; // The player's current location int NumMoves = 0; // The number of player moves char Direction; // The current Direction entered by user int NumSteps; // The nubmer of Steps the user wants to move int Distance; // Distance from player to the treasure in steps // Seed the random number variable srand (time(NULL)); // Set the location of the treasure chest TreasureX = rand() % X_DIM; // set to a value in range 0..XDIM-1 TreasureY = rand() % Y_DIM; // set to a value in range 0..YDIM-1 // Set the starting location of the player StartX = rand() % X_DIM; // set to a value in range 0..XDIM-1 StartY = rand() % Y_DIM; // set to a value in range 0..YDIM-1 X = StartX; Y = StartY; if( TreasureX > StartX && TreasureY > StartY){ Distance = TreasureX - StartX + TreasureY - StartY;} else if( StartX > TreasureX && StartY > TreasureY){ Distance = StartX - TreasureX + StartY - TreasureY;} else if( StartX > TreasureX && TreasureY > StartY){ Distance = StartX - TreasureX + TreasureY - StartY;} else if( TreasureX > StartX && StartY > TreasureY){ Distance = TreasureX - StartX + StartY - TreasureY;} if( Direction == 'e') { X = StartX + NumSteps; } else if( Direction == 'n') { Y = StartY + NumSteps; } else if( Direction == 's') { Y = StartY - NumSteps; } else if( Direction == 'w') { X = StartX - NumSteps; } cout << "This homework was written by YOUR NAME HERE.\n"; cout << "You are stranded on a desert island with no idea how to survive.\n"; cout << "Fortunately, There is a treasure chest full of gold hidden somewhere.\n"; cout << "If you are smart, or lucky, you can find it.\n"; cout << "It is not gonna help you since you will die of hunger anyway.\n"; cout << "But at least it is better to die as a rich person. :-)\n\n"; cout << "The map is " << X_DIM << " wide and " << Y_DIM << " tall.\n\n"; cout << "You are at X=" << StartX << ", Y=" << StartY << ".The treasure is at X:" << TreasureX << ", Y:" << TreasureY << endl; // Output number of steps away from the treasure cout << "You are" << Distance << "away from the treasure" << endl; // Get the desired direction; remember error checking cout << "Enter a valid new direction (N, E, W, S)" << endl; cin >> Direction; // Get the desired number of steps; remember error checking cout << "Enter the number of steps (a positive integer)" << endl; cin >> NumSteps; // while the player is not at the same location as the treasure while(StartX != TreasureX && StartY != TreasureY) { // Output the player's current location,and the treasure location(only for Debugging) cout << "You are at X=" << X << ", Y=" << Y << ". The treasure is at X:" << TreasureX << ", Y:" << TreasureY << endl; // Output number of steps away from the treasure cout << "You are" << Distance << "away from the treasure" << endl; // Get the desired direction; remember error checking cout << "Enter a valid new direction (N, E, W, S)" << endl; cin >> Direction; // Get the desired number of steps; remember error checking cout << "Enter the number of steps (a positive integer)" << endl; cin >> NumSteps; // Move the player to the new location; don't go off the map! } // end of the loop //Print the final statements if( X == TreasureX && Y == TreasureY) { cout << "You finished the game in:" << NumMoves; } // Print the map cout << "\n\nHere is the map.\n"; return 0; } ``````
When the player enters the number of steps to move in a given direction, you do your basic error checking (is player walking off map!?!), then update either the x or y values depending on the direction, then calculate the new distance again.

As for your distance calculation method, if the player is at coordinates (8, 4) and the treasure is at (4, 8), guess what the distance to treasure is?
You should probably be using the distance formula for 2 points rather than the method you are using.
http://www.purplemath.com/modules/distform.htm
Last edited on
The distance formula is the distance from point a to point b. In this project we can only go north, south, west, and east. There is no northest or northwest (this is like taking a shortcut which the directions im using does not permit).
How can I check to see if the player goes off the map and how can I update the x and y values based on the direction? I just need an example please. Thanks
Last edited on
 ``123456789101112131415161718192021222324252627`` ``````// Get the desired number of steps; remember error checking for (bool valid_steps = false; !valid_steps; ) { cout << "Enter the number of steps (a positive integer)" << endl; cin >> NumSteps; switch(Direction & 0x5f) { //convert to uppercase case 'S': NumSteps *= -1; case 'N': if (NumSteps + StartY >= 0 && NumSteps + StartY <= MAX_Y) { /* do something here */ valid_steps = true; } break; case 'W': NumSteps *= -1; case 'E': if (NumSteps + StartX >= 0 && NumSteps + StartX <= MAX_X) { /* do something here */ valid_steps = true; } break; default: cout << "Invalid Direction entered!\n" return 1; } }``````
Topic archived. No new replies allowed.