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
|
// Include statements
#include <iostream>
#include <cmath>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* 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;
}
|