//------------------------------------------- // // A more interactive version of the antworld. // // Simon Parsons // February 8th 2007 // // Include C++ library definitions #include using namespace std; // // Declare variables int x; // robot's x position int y; // robot's y position char c; // user's input bool q; // does user want to quit? // // Declare methods void displayPosition() { cout << "the ant is at location ("; cout << x; cout << ","; cout << y; cout << ")\n"; } void moveNorth() { cout << "moving North...\n"; y = y + 1; } void moveSouth() { cout << "moving South...\n"; y = y - 1; } void moveWest() { cout << "moving West...\n"; x = x - 1; } void moveEast() { cout << "moving East...\n"; x = x + 1; } // // Define main method int main() { x = 0; // Set variables y = 0; q = false; while ( q==false ) // We keep doing this bit { // Get input from user cout << "Which way should the ant move (enter N,S,E,W or Q)? "; cin >> c; cout << "You entered: "; cout << c; cout << "\n"; // Depending on what the user entered, move the right way if ( c=='N' ) { moveNorth(); displayPosition(); } if ( c=='S' ) { moveSouth(); displayPosition(); } if ( c=='E' ) { moveEast(); displayPosition(); } if ( c=='W' ) { moveWest(); displayPosition(); } // If the user entered Q, set up so that we finish. if ( c=='Q' ) { q = true; } } cout << "Time to go!"; return 0; }