Assignment 1
Assignment 1
Assignment 1
Due Oct 21 2024 11:59pm
Objective:
- Develop recursive functions in C
- Use two-dimensional arrays
- Use Pointers
Learning outcomes:
- The acquisition, application and integration of knowledge
- Critical thinking and problem-solving skills
- Literacy and numeracy skills
- Creativity and aesthetic appreciation
We are going to create a game where the user can control a character with specific actions that
can be taken. The gameboard will be a 2D array where each entry represents a location the
character can exist. The character can move around the gameboard with the intention to escape.
That's right, we placed the character randomly on the board and the user must free them by
reaching the exit, which is also randomly placed on the board (not the same location).
The gameboard should be a 2D array. Note that when displaying the board to the user, you can
only show zones that have been explored and neighbour options for movement. Zones that are
unsafe are randomly generated at a rate of 20% of the board. The board is 15 by 30 wide.
The recursive function should display zones that have been explored (marked as E), current
position, and neighbouring unexplored options (marked as ?) in a grid, safety checks (digit), and
unsafe locations reached (! and #). Any zone that is not one of those types can simply be printed
as a whitespace.
Then the function should present the user with possible actions. Given a selected action, the
function should perform the action and call itself recursively with the new parameters (make sure
to handle lifepoints, bombs, energy and gameboard changes before making the recursive call).
Note that energy is reduced every time an action is taken. If no energy left, they lost a lifepoint
instead for any new actions. Consider printing energy to the user off the board. There are an extra
3 lifepoints randomly placed on the board for the user to collect shown as + when found (and
remains as a + on the board).
When a bomb is used, the neighbouring zones all turn into E for explored while reducing the
bomb count. It does not affect zones that were reached and unsafe, and they always cause a lost
lifepoint if travelled to again. Note that if the exit is part of the neighbouring zones, then an X is
displayed instead (so that the user can move there and win).
If the user chooses to make a safety check instead, then the T symbol in the current position is
changed to a digit for the number of safe neighbouring zones. This digit remains on the board
from that point on and updates if a bomb causes one of the unsafe zones to become clear. When
travelling to the zone again, the number remains present (T disappears).
When the user finds the exit, the recursive function will simply return and the entire stack should
pop. A message should print to say they won before returning. The user loses when they lost all
their lifepoints. In this case the function prints a message saying they lost and returns.
Legend:
- T: Character/current position
- E: Explored Region
- : (white space) unexplored region (not accessible yet)
- ? : Possible move that can be made (neighbouring unexplored zones to T)
- X : The exit
- 1/2/3/4 : Value for number of safe neighbouring zones (replaces T when action is taken).
- ! : Just moved to an unsafe location (lost a lifepoint) (replaces T at current position)
- # : Previous unsafe zone that was hit
- + : Lifepoints increase by 1
Part 2A:
1. Create a function 'setter' with parameters P and a new value k (integer). The function
should follow the pointer and change the integer at the end of the chain to the new value.
[8 marks]
2. Call 'setter' in main with k=2. When it returns, demonstrate that the new value of i was
changed (hint: print i). [2 marks]
3. Create a swap function with parameters A2 and B2. The function should swap them
such that A2 now points at B and B2 now points to A. Call this function with A2/B2 in
main. [8 marks]
4. Call 'setter' with k=8.
5. Now print all variables (dereferenced to i or j) in the following format: [2 marks]
i :2
A : 2 (hint: you must print using the pointer)
B2 :2
j :8
B :8
A2 :8
P :8
Part 2 B: Finally, draw the new diagram for the new variable setup. [10 marks]