//------------------------------------------- // // roombaWorld.cpp // // A simple game in which the player moves roomba // around in search of dirt for it to vacuum up. // // Simon Parsons // September 10th 2008 // // Include C++ library definitions #include // for cout #include // for rand #include // for time using namespace std; // // Declare variables int x; // robot's x position int y; // robot's y position int dirtX; // home x position int dirtY; // home y position char c; // user's input bool q; // does user want to quit? // // Declare methods void displayPosition() { 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 << "|O"; } else if((dirtX ==dx) && (dirtY == dy)) { cout << "|x"; } else { cout << "| "; } } } cout << "\n-----------------------\n\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; } bool didWeWin() { if((x == dirtX)&&(y == dirtY)){ return true; } else{ return false; } } // // Define main method int main() { srand(time(NULL)); // Initialise random number generator x = 0; // Set the location of the roomba y = 0; q = false; // Make sure we don't quit right away dirtX = rand() % 11; // Pick a random location for the dirt dirtY = rand() % 11; displayPosition(); // We keep doing this until the user tells us to stop, or the // user wins. while ( q==false ){ // Get input from user cout << "Which way should the robot 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 or the user won, set up so that we finish. if (didWeWin()) { q = true; cout << "You won!" << endl; } if ( c=='Q' ) { q = true; cout << "You lost!" << endl; } } cout << "Time to go!" << endl; return 0; }