Saturday, 24 October 2009

Source code give: Treasure Hunt

I am giving the following code for free and you may use it wherever but I do ask you give credit where credit is due.




#include <iostream>
using namespace std;

#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()

int random (int Range)
{
int num;
num = rand() % (Range + 1);
return num;
}
int main ()
{
srand(time(0)); // Initialize random number generator.
cout << "Welcome to treasure Hunt! A game by Alfie275!\n You have to guess the position of the treasure on the following grid:\n0 1 2 3 4 5 6 7 8 9\n1\n2\n3\n4\n5\n6\n7\n8\n9";
bool Guessed = false;
int Guesses = 0;
int pX;
int pY;
int X = random(9);
int Y = random(9);
while (!Guessed){
cout << "\n\n Enter your guess:\nX:";
cin >> pX;
cout <<"\nY:";
cin >> pY;
Guesses ++;
if (pX != X pY != Y){
cout << "\n Incorrect! The treasure was to the ";
if (pY < Y){
cout << "south";
}
if (pY > Y){
cout << "north";
}
if (pX < X){
cout << "east";
}
if (pX > X){
cout << "west";
}
cout << ".";
}
if (pX == X && pY == Y){
cout << "\n\nYou got it right ";
if (Guesses == 1){
cout << "first time!";
}
else {
cout << "in " << Guesses << " guesses.";
}
Guessed = true;
}
}
while (1);
}

No comments:

Post a Comment