Assignment Questions C
Assignment Questions C
Island map
The island map is represented as a 9x9 array:
B W W B B W B W W
B L L V L L L L B
W L L L L D L L B
B L L D L L L L W
B L D L L L L L W
W L L L L L V L B
W V L L L L L L W
W L L L D L L L W
B B W B W B B W B
where each cell is labelled as follows:
• L: Land, the explorer can safely step on this cell.
• B: Bridge, the explorer can safely step on this cell and use it to
get off the island.
• W: Water, the explorer will die if this cell is stepped on.
• D: Dinosaur, the explorer will die if this cell is stepped on.
• V: Volcano, the explorer will die if this cell is stepped on.
Note that there is a space between each letter in the island map and no
space at the end of the line. Also, this is not the same map as that used
in the task itself.
The only way to escape from the island is for the explorer to step onto a
Bridge (B) cell.
Stepping onto a Land (L) cell is safe, however if the explorer steps on a
Dinosaur (D), Volcano (V), or Water (W) cell, the walk is terminated and
the next random walk should be attempted.
Task
Each cell in the map is to be used as a starting point for walks,
beginning, for example, with the B cell at the top-left corner (i.e. [0][0] in
a 2D array representing the island map).
For each cell on the map, the program should perform 1000 random
walks where the explorer attempts to get off the island.
On each attempt, the explorer can take up to, and including, 10 steps
before they run out of energy and the walk terminates unsuccessfully,
moving on to the next attempt.
Each step on a walk is randomly chosen from one of eight directions, i.e.
North (N), Northeast (NE), East (E), Southeast (SE), South (S),
Southwest (SW), West (W), or Northwest (NW), with equal probability.
After each step, the program should check to see if the walk:
• Is successful by stepping onto a Bridge cell.
• Has failed by stepping onto a Dinosaur, Volcano or Water cell.
• Continues by stepping onto a Land cell.
• Has failed due to the maximum number of steps being taken.
For example, assuming that the explorer is near the top-left, at location
[1][1] (ignoring the spaces) which is a Land (L) cell. If the next step is to
the NW or E they will have stepped on a Bridge cell (B) and so will have
escaped from the island successfully. If the next step is to E, SE, or S,
then they have moved to another Land (L) cell and the walk should
continue for another step. However, if the next step is to N, NE, or SW
then the explorer lands on a Water (W) cell and the walk terminates
unsuccessfully.
REQUIREMENTS
Inputs
Your program should read a 9x9 island map from a text file, stored in the
same directory as your program, called "island_map.txt".
Input validation
We will compile and execute your code, using unseen maps to check the
behaviour. This will include passing invalid inputs to your code. You
should add input validation checks to your code, anticipating the different
ways in which a user may provide an incorrect input.
Starting conditions
You should analyse walk paths starting from each of the 81 possible
starting cells in a 9x9 island map. If the starting cell is a Bridge (B), no
steps are needed and so the path length should be recorded as 0. If the
starting cell is Water (W), Dinosaur (D), or Volcano (V), the explore will
die immediately and so the path length should be recorded as 0.
Coding scheme
To perform the random movement you must use the rand() function (see
below). You should map each move to an integer value as follows:
• 0 represents move N.
• 1 represents move NE.
• 2 represents move E.
• 3 represents move SE.
• 4 represents move S.
• 5 represents move SW.
• 6 represents move W.
• 7 represents move NW.
This coding scheme is shown on the diagram below.
Generating random movements
You are required to use the rand() function to randomly move in one of
eight possible directions.
Note that rand() must be seeded with a value before it is first called. For
this purpose you must use srand() as:
srand(123456);
You should only call srand() once, near the start of your program in int
main(). (Aside: Random numbers.)
If on an edge cell, and a random move would take the explorer outside
the edge of the map, then the explorer should stay put in that direction.
For example:
• If they are on the bottom row, and try and move South, they will
stay on the same cell. (As a move South would take them out of
the map.)
• If they are on the bottom row and try and move South East they
will move East, but stay on the same row.
• If they are on the bottom row, in the bottom right corner, and
move South East they will stay on the same cell. (As a move
South would take them out of the map, and a move to the East
would take them out of map.)
All of these cases still count as a step, and so the step counter should
be incremented.
Note that the explorer can take up to 10 steps on each try. 0 and 10 are
thus valid numbers of steps to take.
.
You are not allowed to include your own custom headers. These will not
be used by the marking system.
Outputs
For every cell on the island map your program should display (to two
decimal places when suitable):
• A copy of the input map.
• The probability of successfully escaping from the island,
displayed as a percentage. That is, how many of the random
walks made it to a Bridge (B) cell.
• The mean path length (i.e. number of steps)
of successful walks.
• The standard deviation of path lengths of successful walks.
Invalid starting cells have 0.00 for the above numerical items. The
values should be displayed to the terminal.
Use of functions
Although you can complete a suitable program without them, we suggest
that you use your own custom functions to keep your code compact and
avoid repetitive code for common actions. You might like to use the
following (incomplete) function prototypes:
• void random_step(…);
• int calculate_status(…);
where:
• random_step calculates the next random step.
• status returns the status of the next step:
o 0 Failure
o 1 Successfully made it to a bridge cell
o 2 Walk continuing
You should also think about your test strategy. How will you check that
your code does what you want it to do? How will you check that any
functions you write in your code do what you want them to do? Feel free
to make your own maps and use these to test your code.
EXAMPLE OUTPUT
The below shows the formatting of the output required from your
program. Note that this is not the output for the island_map.txt included
in the GitHub site. The actual coursework uses a different map to that
below. In particular, note that:
• The numbers below are separated by spaces, not by tabs.
• There is no space on the end of a row.
Map:
B W W B B W B W W
B L L V L L L L B
W L L L L D L L B
B L L D L L L L W
B L D L L L L L W
W L L L L L V L B
W V L L L L L L W
W L L L D L L L W
B B W B W B B W B
Probability of escape:
100.00 0.00 0.00 100.00 100.00 0.00
100.00 0.00 0.00
100.00 35.60 28.10 0.00 32.10 40.30 34.10 49.40
100.00 0.00 40.80 20.50 13.60 13.00 0.00 31.10 43.
60 100.00
100.00 41.40 19.70 0.00 9.00 14.60 20.70 27.60
0.00
100.00 39.70 0.00 8.50 9.20 11.50 17.00 25.80
0.00
0.00 20.90 13.20 11.40 14.50 12.70 0.00 23.80
100.00
0.00 0.00 19.00 19.30 16.70 16.60 24.40 28.50
0.00
0.00 31.20 37.20 23.80 0.00 39.20 41.90 35.60
0.00
100.00 100.00 0.00 100.00 0.00 100.00 100.00 0.00
100.00