//------------------------------------------------- // // A very simple predator/prey game // // We have a grid world on which there lives a rabbit and // a fox. The rabbit moves randomly, and the user moves // the fox in an attempt to catch the rabbit. // // This is a more advanced version of fox.cpp, using some // techniques that we haven't covered yet, but which you // can look up in the textbook. // // Simon Parsons // October 15th 2008 // //------------------------------------------------- // Include C++ library definitions #include // For cin cout #include // For rand #include // For sqrt #include // For time using namespace std; //------------------------------------------------- // // Declare methods // Make a random movement. Either go forwards, go backwards // or do not move. So that the rabbit doesn't move 2 squares // too often, make a no-move happen half the time. int makeRandomMove(void) { int movement; movement = rand() % 4; if((movement == 0) || (movement == 3)) { return 0; } else if(movement == 1) { return 1; } else if(movement == 2) { return -1; } else // Won't get here but the compiler creates a warning otherwise { return 0; } } // Keep the fox and the rabbit in a 10 by 10 grid int wrapAround(int coordinate) { if(coordinate > 10) { return 0; } else if(coordinate < 0) { return 10; } else { return coordinate; } } // Give the position of the fox and the rabbit. void displayPosition(int xF, int yF, int xR, int yR) { int dx; int dy; for(dy=10; dy>=0; dy--) { cout << "\n-----------------------\n"; for (dx=0; dx<=11; dx++) { if((xF == dx) && (yF == dy)) { cout << "|F"; } else if((xR ==dx) && (yR == dy)) { cout << "|R"; } else { cout << "| "; } } } cout << "\n-----------------------\n\n"; } // Move the fox, using a switch statement and reference parameters void moveFox(char move, int &xF, int &yF) { switch(move) { case 'n': yF = wrapAround(yF + 1); break; case 's': yF = wrapAround(yF - 1); break; case 'e': xF = wrapAround(xF + 1); break; case 'w': xF = wrapAround(xF - 1); break; default: cout << "The fox is confused" << endl << endl; } } //------------------------------------------------ // // The main body of the program int main() { // Declare variables int xFox; // Fox's x position int yFox; // Fox's y position int xRabbit; // Rabbit's x position int yRabbit; // Rabbit's y position char c; // User's input bool quit = false; // Do we want to quit? // Initialisation. Pick locations of fox and rabbit randomly srand(time(NULL)); // Set the random seed xFox = rand() % 11; yFox = rand() % 11; xRabbit = rand() % 11; yRabbit = rand() % 11; cout << "Your job in this game is to have the fox catch the rabbit." << endl; cout << "The rabbit won't stay still!" << endl << endl; displayPosition(xFox, yFox, xRabbit, yRabbit); while(!quit) { // Get input from user cout << endl << "Enter q to quit, or n, s, e or w to have the fox move that way: "; cin >> c; if (c=='q'){ quit = true; } else{ moveFox(c, xFox, yFox); } xRabbit = wrapAround(xRabbit + makeRandomMove()); yRabbit = wrapAround(yRabbit + makeRandomMove()); displayPosition(xFox, yFox, xRabbit, yRabbit); if((xFox == xRabbit) && (yFox == yRabbit)){ cout << "Yay!, you caught the rabbit" << endl; quit = true; } // Move the rabbit randomly, using wrapAround to make // sure that it stays within the grid. } cout << "Bye" << endl; return 0; }