Dana Vrajitoru
C201/I211 Computer Programming II

Homework 10

Due Date: Wednesday, November 15, 2006.

Ex. 1 Download the following files in a new folder.
maze.h
maze.cpp
main.cpp
maze.txt

Create a project just like for the previous homework and include these three files. If you compile this on Linux, the compilation command could be:

g++ maze.cpp main.cpp -o maze
where the executable would be "maze".

The files maze.h and maze.cpp implement a class storing a table (2D array) representing a maze. It also implements a player, displayed as "@", that can move up for the moment. The goal of the game is to reach a target in the games marked by "?".

a. Add three functions to this class, Move_down, Move_left, and Move_right. Call all of these functions from the class method Move the following way: the letters 'j' and 'a' should move left, 'k' and 's' should move down, and 'l' and 'd' should move right.

b. Add a function Check_target returning true or false. The function should return true if the table cell where the player is situated contains the target. Add a call to this function in the main after the move is executed. In the main, if the function returns true, display a message telling the player that they won and quit the program.

c. Create a copy of the text file containing the maze (or a whole new one) and add some food in it, which is marked in the text file by "2". Add a class method to the class Maze with the following prototype:

void Check_food(int &score);
This function should check if the cell at the current position of the player contains food. If so, then convert that cell to a space, and increase the score by 1.

In the function main_loop in the main, add a variable score and initialize it to 0. After making the move and before checking if the target was reached, call this method that checks for food and updates the score. Also, in the main_loop, add a display of the score every time after outputting the maze itself.

Note. Do not modify the name of the text file containing the maze in the main. To read the maze from the file you created, under Windows open a Command Prompt (from the Accessories), use the command "cd" to go to the folder debug inside the project folder, and locate maze.exe (or whatever name you gave the project). Then if the executable is called maze.exe and the file you created is called maze1.txt, run the program with the following command:

maze.exe test1.txt
If you run it from Linux, then the command will be the name of the executable followed by a space and by the name of the text file.

Ex. 2 (optional, 5 extra credit points) You can get up to 5 points for adding a ghost and making it walk around the maze either randomly (but not jumping) or following the player).

Turn in: the source files and the maze file you created.