//------------------------------------------------- // // A small predator/prey example. // // We have a(nother) grid world on which there lives // a rabbit. // // The user plays the fox which tries to catch the rabbit. // Since the moves are, effectively, simultaneous, and the // rabbit moves randomly, the game can go on for a surprisingly // long time. // // Simon Parsons // March 14th 2007 // //------------------------------------------------- // Include C++ library definitions #include // For cin cout #include // For rand #include // For sqrt #include // For time using namespace std; //------------------------------------------------- // // Declare methods // A method that prints a grid with the fox and // the rabbit void displayPosition(int xFox, int yFox, int xRabbit, int yRabbit) { int dx; int dy; for(dy=10; dy>=0; dy--) { cout << "\n-----------------------\n"; for (dx=0; dx<=11; dx++) { if((xFox == dx) && (yFox == dy)) { cout << "|F"; } else if((xRabbit ==dx) && (yRabbit == dy)) { cout << "|R"; } else { cout << "| "; } } } cout << "\n-----------------------\n\n"; } // Methods to move the animals around. Each method // moves the animal one square in the given direction. // The world "wraps around", so that if the animal // moves off the "top" of the grid it reappears at // the bottom, and so on. int moveNorth(int y) { cout << "moving North...\n"; if (y <= 9) { y = y + 1; } else { y = 0; } return y; } int moveSouth(int y) { cout << "moving South...\n"; if (y >= 1) { y = y - 1; } else { y = 10; } return y; } int moveWest(int x) { cout << "moving West...\n"; if (x >= 1) { x = x - 1; } else { x = 10; } return x; } int moveEast(int x) { cout << "moving East...\n"; if (x <= 9) { x = x + 1; } else { x = 0; } return x; } //------------------------------------------------ // // 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 int moveRabbit; // How we move the rabbit char c; // User's input bool quit = false; // Do we want to quit? // Initialisation srand(time(NULL)); // Set the random seed xFox = rand() % 11; yFox = rand() % 11; xRabbit = rand() % 11; yRabbit = rand() % 11; //distance = 0; displayPosition(xFox, yFox, xRabbit, yRabbit); while(!quit) { // Get input from user cout << "Which way should the fox move (enter N,S,E,W or Q)? "; cin >> c; // Depending on what the user entered, move the fox the right way if ( c=='N' ) { yFox = moveNorth(yFox); } if ( c=='S' ) { yFox = moveSouth(yFox); } if ( c=='E' ) { xFox = moveEast(xFox); } if ( c=='W' ) { xFox = moveWest(xFox); } // At the same time, the rabbit moves // // Move the rabbit randomly. Generate a random number // and use that to choose a direction to move in. // One time in 5 the rabbit doesn't move. moveRabbit = rand() % 5; if(moveRabbit == 0) { yRabbit = moveNorth(yRabbit); } if(moveRabbit == 1) { yRabbit = moveSouth(yRabbit); } if(moveRabbit == 2) { xRabbit = moveEast(xRabbit); } if(moveRabbit == 3) { xRabbit = moveWest(xRabbit); } // See where we ended up. displayPosition(xFox, yFox, xRabbit, yRabbit); // Did the fox catch the rabbit? if ((xFox == xRabbit) && (yFox == yRabbit)) { cout << "Yay! The fox caught the rabbit\n"; quit = true; } // If we entered Q, then we want to quit. if (c=='Q') { quit = true; } } cout << "Bye" << endl; return 0; }