//------------------------------------------------- // // The basis for a simple pursuer/evader game // // We have a grid world on which there lives an evader // which responds to commands from the user. // // Simon Parsons // November 22nd // //------------------------------------------------- // Include C++ library definitions #include // For cin cout #include // For rand #include // For sqrt #include // For time using namespace std; //------------------------------------------------- // // Function headers int wrapAround(int coordinate); void displayPosition(int xE, int yE); void moveEvader(char move, int &xE, int &yE); //------------------------------------------------ // // The main body of the program int main(){ // Declare variables int xEvader; // Evader's x position int yEvader; // Evader's y position char c; // User's input bool quit = false; // Do we want to quit? // Initialisation. Pick locations of the evader randomly srand(time(NULL)); // Set the random seed xEvader = rand() % 10; yEvader = rand() % 10; cout << endl << "Your job in this game is to make the evader"; cout << " stay away from the pursuers" << endl; cout << "The pursuers will chase you down!" << endl << endl; displayPosition(xEvader, yEvader); while(!quit) { // Get input from user cout << endl << "Enter q to quit, or n, s, e or w to have the evader move that way: "; cin >> c; if (c=='q'){ quit = true; } else{ moveEvader(c, xEvader, yEvader); } displayPosition(xEvader, yEvader); } cout << "Bye" << endl; return 0; } //------------------------------------------------- // // Declare methods // Keep the robots in a 10 by 10 grid int wrapAround(int coordinate) { if(coordinate > 9) { return 0; } else if(coordinate < 0) { return 9; } else { return coordinate; } } // Display the positions of the robots void displayPosition(int xE, int yE) { int dx; int dy; for(dy=10; dy>=0; dy--) { cout << "\n----------------------\n"; for (dx=0; dx<=10; dx++) { if((xE == dx) && (yE == dy)) { cout << "|E"; } else { cout << "| "; } } } cout << "\n----------------------\n\n"; } // Move the evader, using a switch statement and reference parameters void moveEvader(char move, int &xE, int &yE) { switch(move) { case 'n': yE = wrapAround(yE + 1); break; case 's': yE = wrapAround(yE - 1); break; case 'e': xE = wrapAround(xE + 1); break; case 'w': xE = wrapAround(xE - 1); break; default: cout << "The evader is confused" << endl << endl; } }