Assignment Two
Your task for this assignment is to design, code (in C89 version of C) and test a program. In summary, your program will:
- Retrieve map information from a text file provided in command line argument.
- Adjusttheforegroundandbackgroundcolorofspecificcharacteronthegameinterface.
- Utilize a generic linkedlist and struct to keep track of player movement and collapsed floor locations.
1 Code Design
You must comment your code sufficiently in a way that we understand the overall design of your program. Remember that you cannot use “//” to comment since it is C99-specific syntax. You can only use “/*.........*/” syntax.
Your code should be structured in a way that each file has a clear scope and goal. For example, “main.c” should only contain a main function, “game.c” should contain functions that handle and control the game features, and so on. Basically, functions should be located on reasonably appropriate files and you will link them when you write the makefile. DO NOT put everything together into one single source code file. Make sure you use the header files and header guard correctly. Never include .c files directly.
Make sure you free all the memories allocated by the malloc() function. Use valgrind to detect any memory leak and fix them. Memory leaks might not break your program, but penalty will still be applied if there is any. If you do not use malloc, you will lose marks.
Please be aware of our coding standard (can be found on Blackboard) Violation of the coding standard will result in penalty on the assignment. Keep in mind that it is possible to lose significant marks with a fully-working program that is written messily, has no clear structure, full of memory leaks, and violates many of the coding standards. The purpose of this unit is to teach you a good habit of programming.
3 Task Details 3.1 Quick Preview
First of all, please watch the supplementary videos on the Assignment link on Blackboard. These videos demonstrates what we expect your program should do. You will expand the basic functionalities of the escape program you did for assignment 1 (The ”BORDERLESS” feature is NOT necessary to complete assignment 2. You can leave the feature as it is, or remove it if you prefer). Further details on the specification will be explained on the oncoming sections.
3.2 Command Line Arguments
Please ensure that the executable is called “escape”. Your executable should accept one command-
line parameters/arguments:
./escape <map_file_name>
<map_file_name> is the textfile name that contains the map size and location of all the neces- sary characters of the game. You have to use the proper File I/O functionalities to retrieve the information. For more detail, please refer to Section 3.3.
If the amount of the arguments are too many or too few, your program should print a message on the terminal and end the program accordingly (do NOT use exit() function). If the file does not exist, then you need to handle it with a proper error message.
Note: Remember, the name of the executable is stored in variable argv[0].
3.3 File I/O
Please watch the supplementary video about the File I/O. The text file will look like this:
Figure 1: Example of a text file containing the map size and location information.
The two integers on the first line are the playable row and column map size respectively. After- wards, each line represents the location (starting from index zero) for the player, goal, and the collapsed floor grids. Each line will have the row position, column position, and the character. You can assume there is one and only one player and one goal in the text file. Collapsed floor grids locations are optional on the text file. Those characters can appear in any order.
Note: Keep in mind that we will use our own map text file when we mark your assign- ment. So, please make sure your file I/O implementation works with various files.
Similar to assignment one, your program should clear the terminal screen and print the 2D map:
Then the user can enter the command via key ’w’, ’a’, ’s’, and ’d’ to control the player. Every time the user inputs the command, the program should update the map accordingly and refresh the screen (clear the screen and reprint the map). In addition, the player can enter the key ’u’ to undo the movement of the player. For more detail about UNDO feature, please refer to section 3.6.
3.5 Adding Colors
Please watch the supplementary video regarding the pre-written methods to add foreground color and background color on the game interface. You need to adopt those functions to your program to make:
• Player ’P’ has the blue font color.
• Goal ’G’ has the green font color.
• The most recent collapsed floor grid ’X’ will have the red background color.
Note: Keep in mind that when you use the undo feature from section 3.6, your program should still be able to keep track the most recent collapsed floor grid and add the back- ground color accordingly.
3.6 Struct and Generic Linked List to implement UNDO feature
In this section, you will need to write a generic linked list with appropriate struct to implement UNDO feature. This feature is triggered when the user enters the key ’u’. Everytime the key is pressed, the player will move back to the previous movement, and the most recent collapsed floor grid is restored. The player can use the undo feature at any point of the game as long as the game is not over. It should be possible to keep undo-ing until the initial state of the game when you just run the program.
Please refer to supplementary video for some advices regarding this implementation. The sum- mary is that you can use the linkedlist node to store the previous player ’P’ location, the current and previous collapsed floor grid ’X’ location (This is only one example, you can store more information if you need it). You will need to create a new linkedlist node every time the player moves and store it in the linkedlist. When the player presses ’u’, your program should be able to retrieve the most recent node and update the map to the previous state. If the linkedlist is empty, pressing ’u’ should not do anything.
Note: Please do not just copy the whole linkedlist code from someone else because it might lead to collusion academic misconduct.
3.7 Makefile
You should manually write the makefile according to the format explained in the lecture and practical. It should include the Make variables, appropriate flags, appropriate targets, correct prerequisites, conditional compilation, and clean rule. We will compile your program through the makefile. If the makefile is missing, we will assume the program cannot be compiled, and you will lose marks. DO NOT use the makefile that is generated by the VScode. Write it your- self.
3.8 Assumptions
For simplification purpose, these are assumptions you can use for this assignments:
- The coordinates of all characters provided in the text file are all valid. (NOT out of bound)
- There will be exactly one player and one goal in the text file. However, they can appear in any order in the text file.
- Thesizeofthemapwillbereasonabletobedisplayedonterminal.Forexample,wewill not test the map with gigantic size such as 300 x 500. It is the the same for the opposite scenario, we will not test your program with the map size less than 5 x 5.
- You only need to handle lowercase inputs (’w’, ’s’, ’a’, ’d’, ’u’).