//------------------------------------------------- // // A version of the antworld that makes a simple // game in which you have to guide the ant home in // a certain number of moves. // // The "ant" lives on a grid, and the user gets to // move it around by telling it to go N(orth), S(outh) // E(ast) or W(est). The initial location of the ant // is picked randomly, and so is a home location. The // user has 8 turns to get the ant to its home. // // Simon Parsons // February 20th 2007 // //------------------------------------------------- // Include C++ library definitions #include // For cin cout #include // For rand #include // For sqrt #include // For time using namespace std; //------------------------------------------------- // // Declare variables int x; // ant's x position int y; // ant's y position int homeX; // home x position int homeY; // home y position char c; // user's input int seed; // random seed int turns; // how many turns we have left //double distance; // distance home //------------------------------------------------- // // Declare methods // A method that prints a grid with the ant and its // home position. void displayPosition(int x, int y) { int dx; int dy; for(dy=10; dy>=0; dy--) { cout << "\n-----------------------\n"; for (dx=0; dx<=11; dx++) { if((x == dx) && (y == dy)) { cout << "|x"; } else if((homeX ==dx) && (homeY == dy)) { cout << "|H"; } else { cout << "| "; } } } cout << "\n-----------------------\n\n"; } // Methods to move the ant around. Each method // moves the ant one square in the given direction. // The world "wraps around", so that if the ant // moves off the "top" of the grid it reappears at // the bottom, and so on. void moveNorth() { cout << "moving North...\n"; if (y <= 9) { y = y + 1; } else { y = 0; } } void moveSouth() { cout << "moving South...\n"; if (y >= 1) { y = y - 1; } else { y = 10; } } void moveWest() { cout << "moving West...\n"; if (x >= 1) { x = x - 1; } else { x = 10; } } void moveEast() { cout << "moving East...\n"; if (x <= 9) { x = x + 1; } else { x = 0; } } //------------------------------------------------ // // The main body of the program int main() { // Initialisation srand(time(NULL)); x = rand() % 11; y = rand() % 11; homeX = rand() % 11; homeY = rand() % 11; //distance = 0; displayPosition(x, y); for(turns = 8; turns >= 0 ; turns--) { // Get input from user cout << "Distance home is "; cout << sqrt(((x - homeX) * (x - homeX)) + ((y - homeY) * (y - homeY))) << endl; cout << "Which way should the ant move (enter N,S,E, or W)? "; cin >> c; // Depending on what the user entered, move the right way if ( c=='N' ) { moveNorth(); displayPosition(x, y); } if ( c=='S' ) { moveSouth(); displayPosition(x, y); } if ( c=='E' ) { moveEast(); displayPosition(x, y); } if ( c=='W' ) { moveWest(); displayPosition(x, y); } // Are we home? if ((x == homeX) && (y == homeY)) { cout << "Yay! The ant got home\n"; turns = -1; } } if ((x != homeX) || (y != homeY)) { cout << "You failed to get the ant home\n"; } return 0; }