讲解CSCE 121、辅导C/C++编程、讲解C/C++语言、辅导Linux machine
- 首页 >> C/C++编程CSCE 121 Final Project
due December 2
Objectives: This assignment gives you some experience with designing and writing C++ programs
using object-oriented programming features.
For this assignment you need to turn in only the source code (not object code or executable
code). Your code will be tested using a g++ compiler. Therefore, you are welcome to develop
your program in Visual Studio or Xcode, but make sure your code also compiles using g++ and
runs on a Linux machine.
(10 points) Create a text file, called README, in which you provide:
– Your First Name, Last Name, UIN, Section Number, User Name, E-mail address
– State the Aggie Honor statement:
I certify that I have listed all the sources that I used to develop the solutions
and code to the submitted work.
On my honor as an Aggie, I have neither given nor received any
unauthorized help on this academic work.
Your Name Date
– List any resources used such as webpages (provide URL). Do not mention the textbook
and discussions with the Instructor, TA, or Peer Teachers.
– List any known problems with the assignment you are turning in. For example, if you
know your code does not run correctly, state that. This should be a short explanation.
– Provide list of problems
– Provide a short description or pseudocode. Create a chart to represent the class hierarchy
that satisfies the relation “is-a” and the code structure in general.
– Write how you tested your program(s) for correctness and how you used exceptions for
the error handling.
– Submit to eCampus an electronic version of the file README along with your C++
code for the problems listed below and a hard copy to your TA.
Description
1. The purpose of the assignment is to make a maze game with an arbitrary number of players
that will be competing to reach the end of the maze in the shortest number of moves.
The maze will be made up of Starting tiles, Ending tiles, Empty tiles, and Wall tiles. The
player can walk through starting, ending, and empty tiles, but not through wall tiles. In addition,
the maze will be read in from a file containing these characters: S=starting, E=ending,
X=empty, W=wall as a representation of their tile type. This is an example of a maze:
1
S X X X
W W X E
S X X X
You can see two starting positions (S) on the left, a line of wall tiles (W), and an ending tile
(E) on the right. The rest are empty tiles (X).
Once you have loaded the maze, you need to set up your players. There should be one player
for each starting position, so after spawning them the maze should look like this
0 1 2 3
0 P X X X
1 W W X E
2 P X X X
You can see that P’s have replaced the S’s, because P’s represent the players. Next, we need
to take input for each player in order until one of them has reached an end tile. The valid
inputs are: up, down, left, right (or their abbreviations: u, d, l, r) and should move the
current player in the specified direction. Also, we should print the position of the current
player, so we know who we are controlling. All bold text is entered by the user.
0 1 2 3
0 P X X X
1 W W X E
2 P X X X
player 1 position : (0, 0)
right
0 1 2 3
0 S P X X
1 W W X E
2 P X X X
player 2 position : (2, 0)
right
Finally, when one player reaches the end, we need to stop taking input and print that someone
won. Here’s the continuation of the example.
0 1 2 3
0 S P X X
1 W W X E
2 S P X X
player 1 position : (0, 1)
right
0 1 2 3
0 S X P X
1 W W X E
2 S P X X
2
player 2 position : (2, 1)
right
0 1 2 3
0 S X P X
1 W W X E
2 S X P X
player 1 position : (0, 2)
right
0 1 2 3
0 S X X P
1 W W X E
2 S X P X
player 2 position : (2, 2)
up
0 1 2 3
0 S X X P
1 W W P E
2 S X X X
player 1 position : (0, 3)
down
Player 1 Won!
2. The input maze file name is given by the user. The default name is Maze.txt. Its format
should provide information about type of each tile in each row and column. The rows must
be of the same length so they create a rectangular matrix.
3. The output file should document the history of the game including the number of the moves
and the situation at each move. The name of the output file is output.txt. Here is an output
corresponding to the above input example.
0123
0 PXXX
1 WWXE
2 PXXX
player 1 position : (0 , 0)
move #1: - - - - - - - - - -
right
0123
0 SPXX
1 WWXE
2 PXXX
player 2 position : (2 , 0)
move #1: - - - - - - - - - -
right
3
0123
0 SPXX
1 WWXE
2 SPXX
player 1 position : (0 , 1)
move #2: - - - - - - - - - -
right
0123
0 SXPX
1 WWXE
2 SPXX
player 2 position : (2 , 1)
move #2: - - - - - - - - - -
right
0123
0 SXPX
1 WWXE
2 SXPX
player 1 position : (0 , 2)
move #3: - - - - - - - - - -
right
0123
0 SXXP
1 WWXE
2 SXPX
player 2 position : (2 , 2)
move #3: - - - - - - - - - -
right
0123
0 SXXP
1 WWXE
2 SXXP
player 1 position : (0 , 3)
move #4: - - - - - - - - - -
down
Player 1 Won !
? The project consists of an implementation of the maze. This part includes reading data from
a user file that contains characters representing the tiles types (X, S, E, W) describing the maze.
You should implement the maze using a vector of vectors of pointers to Tiles and a user
should output the maze to a file (output.txt) and display it on the screen.
Use object-oriented programming techniques to implement this assignment.
1. inheritance – the base Tile class has a few subclasses. In the class Tile we have 3
virtual functions: try_walk, is_start, and is_end. Define these functions based on
specific type of the tile. So for example, for an empty tile try_walk would return true
whereas is_start and is_end would return false.
2. polymorphism – a more advanced type could be created using pointers. The pointers
are declared on stack part of computer memory and allocate memory on heap. We
have a vector of Tiles, but we want to specify each tile as empty, starting, ending,
or wall. Assume that Tile t = EndingTile(...), and we were to call a virtual
function t.is_end(). This would return false, when it should return true. This
happens because when we store the subclass EndingTile as a Tile we stripped it of the
4
information that told us that t was actually an EndingTile, so to fix this we need to
use pointers: Tile* t = new EndingTile(...).
(10 points) Code Structure – Create a chart to represent the class hierarchy that satisfies the
relation “is-a” and the code structure in general.
This project looks a little complex so let’s go through it starting at main.cpp. Here we see
that we construct a maze game. If we look at the constructor for MazeGame we can see that
it is reading in a maze from a file. Now if we go back to the main function we can see that we
call start_game after constructing the game object. If we go and look at this, you will see
that there is quite a bit missing here and you need to implement some stuff (its described in
the next part). We can also see that it calls print_maze which just prints out the maze with
players layered on top. We can also see that it calls update_loop which updates the board
game state in a loop. So we take in user input, move the player and then return when a player
reaches an EndingTile. In this function we should call the can_move_to_tile function on
each player when we check for their input.
(130 points) Functions and Classes You Need to Implement
1. (30 points) void MazeGame::start_game()
2. (20 points) class WallTile, class EmptyTile, class StartingTile, class EndingTile
3. (15 points) istream& operator>>(istream& in, Tile*& t)
4. (15 points) bool Maze::can_move_to_tile(Position pos)
5. (50 points) void MazeGame::update_loop()
To implement them, you can use the order in which the functions and classes are listed, but
it is not required, so you can choose your own order. Nevertheless, you need to implement
all of them to get a running code. Please read the comments in the code to get some hints or
explanations. Note that the code compiles without compiler errors or warnings, but it does
not run correctly (it is waiting for some input).
Here are more explanations. In the code we gave you, there are only 4 unimplemented
functions. The start_game function sets up the players in the maze and starts the update
loop. The update_loop function takes input from the user and moves the player accordingly.
The function can_move_to_tile takes in a position and returns whether a player can move
to that position, so this will return true if the player can moves further, but false if a player
tries to move out of bounds of the maze or to a wall tile. Finally, the input operator “>>”
works with a tile pointer (we use a reference to a tile pointer). The reason why this needs to
be a tile pointer and not just a tile is described above. But altogether this needs to read a
character from cin and return a constructed tile.
In addition to these functions you will need to implement each of the Tile subclasses. You
have the main Tile (base) class and 4 other Tile subclasses. The Tile class has 3 ‘virtual’
functions. If the behavior on a subclass is different from the base class you need to ‘override’
the virtual function in the subclass by writing another function with the same name.
Moreover, read the section 14.3.1 “Object layout,” p. 506 in the textbook to better understand
virtual functions, and then analyze the illustration below for the class Tile and its subclasses.
5
Important: A file name should use this name LastName_UIN_maze_game.cpp where
LastName is your last name and UIN is your UIN number.
Submit all the files together as a zip/tar file to eCampus for grading.