You are to program a maze navigation game. The assignment is broken into two parts. The below game and header file description applies to both parts.
Maze navigation. You are to help a mouse to find cheese in a maze. The maze is a square mazeSize*mazeSize grid of rooms. Each room has two coordinates. The horizontal coordinate is an integer number ranging from 1 to mazeSize. The vertical coordinate is a letter ranging from 'a' to 'a'+mazeSize-1 (note that in C++ you could add an integer to a character as done in this example.)
The mouse is placed in room a1 (top-left room). The cheese is placed in room a+mazeSize-1,mazeSize (bottom-right room). Room r1 is adjacent to room r2, if r1 is to the left, right, up or down from room r2. Diagonal rooms are not considered adjacent. A wall prevents the mouse from moving to an adjacent room.
Your program should randomly place numWalls in the maze. A wall must separate a pair of adjacent rooms. Note that if there is a wall r1|r2, where r1 and r2 are adjacent room coordinates, then there should not be another wall r1|r2 or r2|r1. No other checks, such as a check whether the maze is navigable, need to be performed. On user request, the program should print wall locations.
The game proceeds as follows. The program prints the current mouse room and asks the user for the mouse's next move: (u)p, (d)own, (l)eft, (r)ight, or (q)uit. The program should accept a single character signifying the move. If the move takes the mouse outside the maze or there is a wall between the current mouse room and the next, then the program prints wall and keeps the mouse in the same room. Otherwise, it moves the mouse to the new location and the game continues. The check whether the mouse already visited the room is not needed. The game proceeds until the mouse reaches the cheese room or the user quits the game.
Maze data structures and function description. The data structures and functions needed to implement the game are declared in this header file. The header file defines two structures:
The functions are separated into three groups:
Function pickAdjacent() selects a random room adjacent to the room passed to this function as a parameter. The pseudocode for the function is as follows:
while an adjacent room is not selected select an random direction for the adjacent room: (u)p, (d)own, (l)eft or (r)ight invoke goodDirection() to see if the adjacent room is inside the maze if yes invoke createAdjacent() to create a room in the selected direction mark adjacent room as selected return the selected adjacent room
Function build() accepts an array of RoomPair by reference and initializes wall locations. It uses the other two initialization functions: pickAdjacent() and checkMaze() The pseudocode for build() is as follows:
declare a variable that stores the number of already built walls, initially zero, this variable is to be used as an index in the array of walls loop until all required walls are built select a random wall using pickPair() invoke checkMaze() to verify whether this wall is already built if this wall not built then build this wall by copying the wall into the array increment the number of built walls
Hint 1: To randomly assign a character coordinate (from 'a' to mazeSize) for the first room location in pickPair(), randomly select a number from 1 to mazeSize and then use a switch to select the appropriate letter.
A random direction letter (u, d, l, r) for the second room location may be selected similarly: pick a random number from 0 to 3 and then use a switch to select an appropriate direction.
Hint 2: The logic of clearWalls(), checkMaze() and build() is very similar to the logic of lottery number selection functions in one of the previous labs.
Note that, as shown, portions of the file are commented out. This is to encourage incremental program development. You need to implement the functions that are not commented out first. Then, uncomment the second portion of code and implement those functions. Once your project works correctly with all code of testMaze.cpp uncommented, submit your project.
populate the maze by invoking build() on it declare the room to keep the current mouse location, place the mouse in the top-left corner prompt the user if she wants to see the maze wall positions printed loop while the current mouse location is not the finish room and the user wants to continue the game (the selected room is not the -1*) inside the loop print the current (mouse) room on the basis of the current room, invoke nextMove() get the next room from the user, form a roomPair out of current room and next room to indicate the next move invoke checkMaze() to see if this move is possible if yes move the mouse by updating the current room if not report a wall report ending of the gameHere is an example game dialogue (user input is shown in bold):
Show walls? [y/n]: y d2|d3, c3|d3, b4|a4, a2|a1, c3|c2, c1|c2, b1|c1, b4|b3 Current room: a1 Next move [u/d/l/r/q]: u wall Current Room: a1 Next move [u/d/l/r/q]: r wall Current room: a1 Next move [u/d/l/r/q]: d Current room: a2 Next move [u/d/l/r/q]: l ...
Milestone: implement room and wall functions (first and second part of the testMaze.cpp file).
Make sure your programs adhere to proper programming style. Submit your projects to the subversion repository. Do not forget to verify your submission on the web.