Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A C program that tracks a simulation through a 2D array of characters (char\'s).

ID: 669483 • Letter: A

Question

A C program that tracks a simulation through a 2D array of characters (char's). This array must be local to some function, and passed to other functions. Since C arrays do not track their own dimensions, you will also need to pass the array dimensions.
The array represents a 2D environment, and tracks the location of puppies, zombies, and objects. The following characters are used to represent different objects: o ‘p’ – a tired puppy. ‘P’ – a rested puppy. ‘z’ – a tired zombie. ‘Z’ – a rested zombie. ‘ ‘ (space) – an unoccupied cell. All other characters – an obstacle that cannot move or be occupied.
The program will read input from a file called “room”. This file indicates the initial layout of a simulation. For example:

DIMENSIONS: 5 10
ITERATIONS: 1000
+--------+
| p|
| |
|z p|
+--------+
In this example: We have a 5x10 (rows by columns) 2D environment. We must run 1000 iterations of the simulation. Following is the initial layout of the environment, which you must read into your array. In this case, there is a single tired zombie in the lower left of the room, and tired puppies in the upper right and lower right corners. The exact format of this file is described in “Program Input”. The program will then run the specified number of iterations of the zombie simulation (in the example above, 1000 iterations):
In each iteration: Puppies move. Zombies move. Puppies may become infected. Zombies may be destroyed.
The exact rules of the simulation are described in “Rules”.

After the specified number of iterations, the program saves the final room state to a file called “results”. For example, if your simulation determines that one puppy was infected, but the other was not, and evaluated the zombie/puppy movements, it might output:

+--------+
| p z |
| z|
| |
+--------+

after the simulation. This shows several tired zombies approaching a tired puppy in the corner of a room.

Rules

Puppies and zombies can only move: North (up) South (down) East (right) West (left), Or not at all
A zombie and a puppy are adjacent if one can reach the other in a single move; that is, a zombie is adjacent to a puppy if the puppy is immediately north, south, east, or west from the zombie, and vice versa.

In each iteration of the simulation: The puppies, being more mobile, will move first. First, any tired puppy (‘p’) becomes a rested puppy (‘P’). Second, moving across the room, from left to right, then top to bottom, each rested

puppy (‘P’) calls get_puppy_direction. If the cell in the suggested direction is unoccupied (‘ ‘), the puppy moves in that direction, and becomes tired (‘p’). If the cell in the suggested direction is occupied (non-‘ ‘), the puppy does not move, and remains rested (‘P’).

Next, the zombies move. First, any zombie adjacent to 2 or more puppies is destroyed by the puppies. The puppies do not move, and their rested state does not change. The zombie is removed from the board (becomes an unoccupied space, ‘ ‘). Second, any tired zombie (‘z’) becomes a rested zombie (‘Z’). Third, moving across the room, from left to right, then top to bottom, any rested zombie (‘Z’) adjacent to a single puppy: Does not move from its current position. Infects the puppy, converting it to a tired zombie (‘z’). Becomes a tired zombie (‘z’) itself. Fourth, moving across the room, from left to right, then top to bottom, each rested zombie (‘Z’) calls get_zombie_direction. If the cell in the suggested direction is unoccupied (‘ ‘), the zombie moves in that direction, and becomes tired (‘z’). If the cell in the suggested direction is occupied (non-‘ ‘), the zombie does not move, and remains rested (‘Z’).

Each step must be completed for the entire room before the next step is run.

get_puppy_direction:
Receives, as parameters, at least the room and a puppy location. First determines the direction away from the zombie that is nearest to the puppy. If there is more than one nearest zombie (that is, the closest zombies are all the same distance from the puppy), the puppy is unable to make a decision, and does not move. If the nearest zombie’s vertical distance from the puppy is the same as its horizontal distance from the puppy, the puppy is unable to make a decision, and does not move. Due to puppies’ inherent unpredictability, there is a 1 in 5 chance the puppy chooses a different direction (even if it was previously unable to decide on a direction):

Use “if (rand()%5==0)” to determine if a different direction should be chosen. If the check passes, return get_random_direction(). Otherwise, return the direction away from the nearest zombie.

get_zombie_direction:
Receives, as parameters, at least the room and a zombie location. First determines the direction towards puppy that is nearest to the zombie. If there is more than one nearest puppy (that is, the closest puppies are all the same distance from the zombie), the zombie is unable to make a decision, and does not move. If the nearest puppy’s vertical distance from the zombie is the same as its horizontal distance from the zombie, the zombie is unable to make a decision, and does not move. Due to zombies’ inherent clumsiness, there is a 4 in 5 chance the zombie stumbles in a different direction (even if it was previously unable to decide on a direction):
Use “if (rand()%5)” to determine if a different direction should be chosen. If the check passes, return get_random_direction(). Otherwise, return the direction towards the nearest puppy.

get_random_direction:
Chooses a random number, between 0 and 3 (rand()%4). o If 0 is picked, returns north.
If 1 is picked, returns east.
If 2 is picked, returns south.
If 3 is picked, returns west.

Design

Your program should have no global variables.
Your program must implement and use the following functions:
save_room – saves the results of the simulation to a file called “results”
get_room_width – returns the room width from the input file
get_room_height – returns the room height from the input file
get_iterations – returns the simulation iterations from the input file
load_room – receives and initializes the room matrix, using the input file
iterate_simulation – receives the room matrix, performs one iteration of the simulation
get_zombie_direction – returns the proper direction for a given zombie to move
get_puppy_direction – returns the proper direction for a given puppy to move
get_random_direction – returns a random direction

#define the possible directions; for example:
#define NORTH 0
#define SOUTH 1
...

int rand(void)
You must use the following implementation of rand(); do not use the version provided in the standard C libraries (stdlib.h). This will ensure that we are all using the same random number generator, so our simulations will remain the same, even across multiple computers and compilers.

}

Program Input
The program will read input from a file called “room”.
The first line of the input file will contain, in order:
The word “DIMENSIONS:”, followed by a space
An integer n indicating the height (number of rows) of the simulation, followed by a space
An integer m indicating the width (number of columns) of the simulation
A new line character

The second line of the input file will contain, in order:
The word “ITERATIONS:”, followed by a space
An integer indicating the number of iterations to run o A new line character
This will be followed by the environment, which is represented with n lines of m characters each, with each line followed by a new line.

Program Output
The program will output to a file called “results”.
The output will contain the final calculated 2D environment, printed as each row of the array in order, followed by a new line character.

Explanation / Answer

#include #include #include #define NORTH 0 #define EAST 1 #define SOUTH 2 #define WEST 3 #define NONMOVE 4 #define DIMENSION 100 /* NAME: Summer Xue */ /* DATE: Sep 17 */ /* Class: CSE 2421, T/TH 17:20 */ void save_room(FILE *result,char OutArray [DIMENSION][DIMENSION],int height,int width) { int i; for(i=0;i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote