// @topic W010101 Maze (C/C++) // @brief C++ program demonstarting global data and procedures // maze_main.cpp #include <iostream> // This program implements a tiny maze made up of two rooms. // Two "rooms" are hardcoded in two operations: // enter_kitchen() enter_dining() // C naming convention // enterKitchen() enterDining() // Java naming convention // The topology of the maze looks like this: // (North) // (West) kitchen - door - dining (East) // (South) // The visitor (the user of the program) can move from // room to room using the keyboard input as follows: // G = go forward // L = turn left // R = turn right // For simplicity, there is no input error checking of any kind, // so the game will break as soon as any bad input is encountered. // The program demondstrates procedural approach and uses the // following global data: // useful variables to indicate possible direction: int West = 0, North = 1, East = 2, South = 3; // a variable specifying the visitor int visitor_direction = West; // a variable to store last user action, // may contain 'L', 'R', or 'G' (see above) char visitor_action = 0; //-------------------------------------------------- // function declarations //-------------------------------------------------- int turn_left(); int turn_right(); int get_visitor_action(); int enter_kitchen(); int enter_dining(); //-------------------------------------------------- // visitor wants to turn left //-------------------------------------------------- int turn_left() { if ( visitor_direction == West ) { visitor_direction = South; } else if ( visitor_direction == North ) { visitor_direction = West; } else if ( visitor_direction == East ) { visitor_direction = North; } else if ( visitor_direction == South ) { visitor_direction = East; } return 0; } //-------------------------------------------------- // visitor wants to turn right //-------------------------------------------------- int turn_right() { if ( visitor_direction == West ) { visitor_direction = North; } else if ( visitor_direction == North ) { visitor_direction = East; } else if ( visitor_direction == East ) { visitor_direction = South; } else if ( visitor_direction == South ) { visitor_direction = West; } return 0; }//turn_right() //-------------------------------------------------- // ask the visitor what they want to do //-------------------------------------------------- int get_visitor_action() { std::cout << "What would you like to do? (L R G)\n"; // get user input from the keyboard: std::cin >> visitor_action; if ( visitor_action == 'L' ) { turn_left(); } if ( visitor_action == 'R' ) { turn_right(); } return 0; }//get_visitor_action() //-------------------------------------------------- // the kitchen function describes what the visitor // can do in this room. It traps the visitor in the room // forever, in the endless loop //-------------------------------------------------- int enter_kitchen() { std::cout << "You are in the kitchen!\n"; for (;;) { // endless loop begins... // Tell the user what they see if ( visitor_direction == West ) { std::cout << "You are facing West wall\n"; } if ( visitor_direction == North ) { std::cout << "You are facing North wall\n"; } if ( visitor_direction == East ) { std::cout << "You are facing East door\n"; } if ( visitor_direction == South ) { std::cout << "You are facing South wall\n"; } // Ask for an action get_visitor_action(); // if action is "Go" and direction is East, then let them into the dining room // && is a logical AND operator if ( visitor_action == 'G' && visitor_direction == East ) { enter_dining(); } if ( visitor_action == 'G' ) { std::cout << "Sorry, bad command!\n"; } }//for return 0; }//enter_kitchen() //-------------------------------------------------- // the kitchen function describes what the visitor // can do in this room. It traps the visitor in the room // forever, in the endless loop //-------------------------------------------------- int enter_dining() { std::cout << "You are in the dining room!\n"; for (;;) { // endless loop begins... // Tell the user what they see if ( visitor_direction == West ) { std::cout << "You are facing West door\n"; } if ( visitor_direction == North ) { std::cout << "You are facing North wall\n"; } if ( visitor_direction == East ) { std::cout << "You are facing East wall\n"; } if ( visitor_direction == South ) { std::cout << "You are facing South wall\n"; } // Ask for an action get_visitor_action(); // if action is "Go" and direction is East, then let them into the dining room // && is a logical AND operator if ( visitor_action == 'G' && visitor_direction == West ) { enter_kitchen(); } if ( visitor_action == 'G' ) { std::cout << "Sorry, bad command!\n"; } }//for return 0; }//enter_dining() int main() { std::cout << "Welcome to the Maze!\n"; enter_kitchen(); // System.out.println( "Hello" ); // Java std::cout << "Press x and Enter to exit: "; std::cin.get(); }