/** roomba3.cpp 21-feb-2008/azhar this program expands on roomba2.cpp by starting the robot in a random location in the room. */ // section 1: include C++ library definitions #include /*for random and math function*/ #include #include /*for time function*/ #include using namespace std; // section 2: declare variables int x; // robot's x position int y; // robot's y position char c; // user's input bool answer; // does user want to quit? // section 3: declare methods void display() { cout << "the roomba is at location ("; cout << x; cout << ","; cout << y; cout << ")\n"; } // end of display() void moveForward() { cout << "moving forward...\n"; y = y + 1; } // end of moveForward() void moveBackward() { cout << "moving backward...\n"; y = y - 1; } // end of moveBackward() void moveLeft() { cout << "moving left...\n"; x = x - 1; } // end of moveLeft() void moveRight() { cout << "moving right...\n"; x = x + 1; } // end of moveRight() // section 4: define main method int main() { // initialize random seed srand( time( NULL )); // find random initial location x = rand() % 10; y = rand() % 10; display(); // loop until user is done answer = false; while ( answer==false ) { cout << "which way should roomba move (enter F,B,L,R or Q)? "; cin >> c; cout << "you entered: "; cout << c; cout << "\n"; if ( c=='F' ) { moveForward(); display(); } else if ( c=='B' ) { moveBackward(); display(); } else if ( c=='L' ) { moveLeft(); display(); } else if ( c=='R' ) { moveRight(); display(); } else if ( c=='Q' ) { answer = true; } else { cout << "oops! you entered something invalid. please try again :-)\n"; } } // end while answer==false } // end of main()