0% found this document useful (0 votes)
6 views

Assignment 1

Uploaded by

hadiyaharif10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment 1

Uploaded by

hadiyaharif10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

COMP-1410 Fall 2024

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

Part 1: Recursion & 2D arrays (70 marks)

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 user has the following actions available to them:


(1) Move up/down/left/right. If on the edge of the 2D board, they can jump to the
opposite side. Moving to a zone that is unsafe will cost 1 life point.
(2) Perform a safety check. This action will check neighbouring zones and the number of
safe zones are displayed in the current zone. No information is provided about the
individual zones, just a total sum. The zone maintains this number and updates if
neighbouring zones become safe from a bomb in future actions and positions.
(3) Neutralize zones (bomb). The user can choose to neutralize all neighbouring zones
(make them safe). This action can only be taken 3 times in the game. The neighbouring
zones are all marked as (E) for explored (or X for the exit if found).

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.

© 2024 Ryan Bluteau. All rights reserved. Page 1 of 4


The gameloop should be performed recursively. The inputs are the following: Gameboard 2D
array, lifepoints (starting at 3) passed by reference, number of bombs available (starting at 3)
passed by reference, current position x, current position y (both passed by value), energy
(starting at 450=number of zones) passed by reference.

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

© 2024 Ryan Bluteau. All rights reserved. Page 2 of 4


Part 2: Pointers (30 marks)
Create a program and set up the variables as shown in the diagram. Complete the following steps
for the program:

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]

© 2024 Ryan Bluteau. All rights reserved. Page 3 of 4


Grading:
- Programs should be named part1.c and part2.c
- Programs should be compiled using "gcc -Wall part1.c" and tested using the appropriate
compiler (while not the only option, you can use the one available on the university's server
accessed through NoMachine or SSH).
- Any programs that do not compile (produce errors) automatically receive a -50% penalty.
This means you must ensure you compile and test your program using the appropriate
software. Any compile errors generated from programs tested through 'online compilers' will
also receive a -50% penalty. Fatal and non-fatal run-time errors will receive mark
deductions based on assignment grading scheme (depending on the part that breaks).
- All programs must use the C extension. A C program submitted using a Word document, PDF
formats, or Image formats (like PNG and JPG) which cannot be converted to a C extension
will receive 0 for the associated parts/questions.
- Any diagrams that are required can be submitted in any viewable format: PDF, PNG, JPG,
Word, etc. Text files can be submitted as .txt, Word, or PDF.
- Late assignments receive -25% for each day that it is late. In other words:
• if(submitted_date > submission_deadline - 3 hours) {
int days_late = submitted_date - submission_deadline + 1;
grade *= 1 - 0.25*days_late;
}
• If the assignment is a few hours late due to submission issues, we generally don't apply
the late penalty.

© 2024 Ryan Bluteau. All rights reserved. Page 4 of 4

You might also like