Lab 3 Part 2: Fire! Fire in the Forest!
Overview
In Part 1 of this lab, we "flooded" an unfortunate city. For the part 2, further disaster unfolds.... This time, we will have the smoky, haze-inducing forest fire. In a forest fire, the fuel (usually trees) and wind blows play important parts in the scale of the fire. Fuel determines how long a fire can last, and wind direction dictate the direction of the spreading fire. In this lab, we are going to simulate this scenario....
The lab would start off by populating an empty forest (i.e. planting trees of varying height). Afterwards, fire will start spreading through the forest....
Getting Started
As in part 1, the bird-eye-view of the forest is simulated. An integer array is used, where each number indicates the height of the tree at a particular spot (0 simply means no tree). So, below is an example of a 8 x 8 empty forest:
0 |
1 |
2 |
3 | 4 | 5 | 6 | 7 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
7 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Planting Trees
The only piece of information from the user for planting tree is the total number of the trees desired. Tree planting then proceed by a pretty straight forward process:
For Each Tree:
1. Pick a
random coordinate. Make sure that the spot is empty (i.e. 0)
2. Pick a
random height ranging from [1..4] inclusive.
Lets see an example:
13 Trees are planted in the Forest
0 |
1 |
2 |
3 | 4 | 5 | 6 | 7 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 1 | 0 | 0 | 0 | 3 | 0 |
2 | 0 | 0 | 0 | 1 | 0 | 2 | 0 | 4 |
3 | 0 | 0 | 0 | 4 | 0 | 0 | 0 | 0 |
4 | 0 | 2 | 0 | 0 | 2 | 0 | 0 | 0 |
5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
6 | 0 | 0 | 0 | 4 | 0 | 4 | 2 | 0 |
7 | 0 | 4 | 0 | 0 | 0 | 0 | 4 | 0 |
The trees are bolded for your reference.
Forest Fire
As mentioned above, forest fire is affected by two factors: Fuel and Wind Direction. We will discuss them one by one:
Fuel:
1. Burning one square consumes 1 unit of fuel.
2. Each extra unit of fuel can spread the fire 1
square in the direction top, down, left and right.
3. When a fire hit a tree, each meter of the tree serves as 1
unit of fuel. e.g. a 4 meters tree can spread the fire 4 squares in the 4
directions.
4. Fuel is culmulative, i.e. when we are burning a square
with 3 units of fuel and hit a tree of 2 meters, we will have 3+2-1 = 4 units of
fuel to spread the fire.
The -1 is for burning the current
square.
Wind Direction:
1. In our scenario, the wind can only blows in
four directions: up, down, left and right.
2. A fire cannot spread in the opposite
direction of the wind. So, when wind is blowing upwards, the fire can only
spread to the left, right and up, but NOT
down. Likewise for the
other directions.
Finally, a few rules for the "behavior" of the fire:
1. The forest fire will keep spreading regardless
of whether the ground is burnt as long as there is fuel.
2. Fire is represented by -1.
A user will initiate a fire by indicating the following:
1. The coordinate of the fire.
2. The direction of wind blow. An integer is used to
represent the direction: 1 for UP, 2 for DOWN, 3 for LEFT, 4 for RIGHT.
3. A user fire is started with 1 unit of fuel.
Lets see some examples (using the forest above):
Fire at (0,0) Wind blowing
toward Left
0 |
1 |
2 |
3 | 4 | 5 | 6 | 7 | |
0 | -1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 1 | 0 | 0 | 0 | 3 | 0 |
2 | 0 | 0 | 0 | 1 | 0 | 2 | 0 | 4 |
3 | 0 | 0 | 0 | 4 | 0 | 0 | 0 | 0 |
4 | 0 | 2 | 0 | 0 | 2 | 0 | 0 | 0 |
5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
6 | 0 | 0 | 0 | 4 | 0 | 4 | 2 | 0 |
7 | 0 | 4 | 0 | 0 | 0 | 0 | 4 | 0 |
Since (0,0) is empty, we run out of fuel after burning that square.
Fire at (4,1) Wind blowing toward Right (continue from above)
0 |
1 |
2 |
3 | 4 | 5 | 6 | 7 | |
0 | -1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 1 | 0 | 0 | 0 | 3 | 0 |
2 | 0 | -1 | 0 | 1 | 0 | 2 | 0 | 4 |
3 | 0 | -1 | -1 | 4 | 0 | 0 | 0 | 0 |
4 | 0 | -1 | -1 | -1 | 2 | 0 | 0 | 0 |
5 | 0 | -1 | -1 | 0 | 0 | 0 | 0 | 0 |
6 | 0 | -1 | 0 | 4 | 0 | 4 | 2 | 0 |
7 | 0 | 4 | 0 | 0 | 0 | 0 | 4 | 0 |
With the 2 meter tree at (4,1), we have:
1 (initial fuel) + 2 (from tree) - 1 (spent on burning (4,1))
= 2 units of fuel
So, the fire will spread 2 squares from (4,1) reaching as far as (2,1), (6,1)
and (4,3). Since there are no tree in the range, the fire dies down after that.
Note that the fire never spread to the left of (4,1) because of the prevailing
wind blowing to the right.
Fire at (6,6) Wind blowing toward Right (continue from above)
0 |
1 |
2 |
3 | 4 | 5 | 6 | 7 | |
0 | -1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 1 | 0 | 0 | 0 | 3 | 0 |
2 | 0 | -1 | 0 | 1 | 0 | 2 | -1 | 4 |
3 | 0 | -1 | -1 | 4 | 0 | 0 | -1 | -1 |
4 | 0 | -1 | -1 | -1 | 2 | 0 | -1 | -1 |
5 | 0 | -1 | -1 | 0 | 0 | 0 | -1 | -1 |
6 | 0 | -1 | 0 | 4 | 0 | 4 | -1 | -1 |
7 | 0 | 4 | 0 | 0 | 0 | 0 | -1 | -1 |
This time we have a much larger scale of forest fire. The square (6,6) is burnt with 1 fuel, adding to the 2 meters tree, we have: 1 (initial fuel) + 2 (from tree) - 1 (spent on burning (6,6)) = 2 units of fuel Hence, the square (6,6) will spread the fire to (5,6), (7,6) and (6,7) with 2 units of fuel. The square (7,6) happens to have a tree of 4 meters. we have: 2 (initial fuel) + 4 (from tree) - 1 (spent on burning (7,6)) = 5 units of fuel So, you can see that the fire spreads 5 squares from (7,6) reaching as far as (2,6) and (3,7). Further examples and output format are shown in the Sample Output section.
Bits and Pieces
Since this is already the third lab, you will be given lesser help. Dont forget that you are allowed to define other functions to aid your coding, for example, the tree planting process can be entirely coded in the main(), or you can opt to use a function to handle it. A few simple/tedious functions are given to let you concentrate on the main problem.
int IsValid(int row, int column);
Purpose:
Checks whether row and column are in the range 0 to SIZE-1
Input: Row (an integer
representing the row number)
Column (an integer representing the column number)
Output: 1 (True. The row and
column are well defined)
0 (False. Either the row or column is out of range)
Remark: You can choose NOT to
implement or use this function.
void FireAt(int Map[][SIZE], int row, int
column, int Fuel, int Direction);
Purpose:
A recursive function to form a meteor crater on map.
Input: Map (the 2d array
that represents the Forest)
Row (the row number)
Column (the column number)
Fuel (the current amount of fuel)
Direction (the wind direction, 1:Up, 2:Down, 3:Left, 4:Right )
Output: None.
Putting it together
With the help of the functions above, you can now design the main function. The basic steps is given as follows:
Basic Steps:
1. Initialize the Map.
2. Print Map.
3. Ask for number of trees to be
planted,
nTree.
4. For each tree:
a. Pick a
random coordinate (repeat for coordinate already with tree)
b. Pick a
random number from [1,4] inclusive for the height
d. Plant the
Tree
e. Print out
information for the tree, coordinate and height.
5. Print the Map
6. Ask for number of fire,
nFire.
7. For each Fire:
a. Ask for
coordinate (repeat for invalid coordinate)
b. Ask for
the wind direction (1 to 4, repeat for invalid input)
b. Start the
fire at coordinate with 1 unit of fuel
c. Print Map.
Refer to the sample output section for output format.....
Skeleton Code
#include#define SIZE 8 //The two functions below are given. int Reading(char[]); void PrintArray(char[],int [][SIZE]); //These are the functions that you need to code. You may //define extra functions if necessary. int IsValid(int,int); void FireAt(int[][SIZE],int,int,int,int); int main() { int Map[SIZE][SIZE]; //Starts your code here. return 0; } int Reading(char msg[]) { int input; printf(msg); scanf("%d",&input); return input; } void PrintArray(char str[],int a[][SIZE]) { int i,j; printf("%s\n",str); printf("===========================\n\n"); printf(" "); for (i = 0; i < SIZE; i++){ printf("%3d ",i); } printf("\n"); for (i = 0; i < SIZE; i++){ printf("%2d ",i); for (j = 0; j < SIZE;j++){ printf("%3d ",a[i][j]); } printf("\n"); } printf("\n"); } int IsValid(int X, int Y) { } void FireAt(int Map[][SIZE],int X, int Y, int Fuel, int Direction) { }
Sample Output
Note: User Input is in bold.
Initial Map =========================== 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 Number of Tree(s) to Plant: 13 Planted Tree At (6,6) of 2 meters Planted Tree At (3,3) of 4 meters Planted Tree At (2,3) of 1 meters Planted Tree At (6,5) of 4 meters Planted Tree At (4,4) of 2 meters Planted Tree At (7,1) of 4 meters Planted Tree At (1,6) of 3 meters Planted Tree At (2,7) of 4 meters Planted Tree At (7,6) of 4 meters Planted Tree At (1,2) of 1 meters Planted Tree At (4,1) of 3 meters Planted Tree At (6,3) of 4 meters Planted Tree At (2,5) of 2 meters Map after Tree Planting Phase =========================== 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 3 0 2 0 0 0 1 0 2 0 4 3 0 0 0 4 0 0 0 0 4 0 3 0 0 2 0 0 0 5 0 0 0 0 0 0 0 0 6 0 0 0 4 0 4 2 0 7 0 4 0 0 0 0 4 0
Number of Forest Fire(s): 5 Row = -1 Column = 3 Invalid Coordinate. Try Again Row = 0 Column = 0 Wind blows toward (1:Up 2:Down 3:Left 4:Right): 6 Invalid Direction. Try Again Wind blows toward (1:Up 2:Down 3:Left 4:Right): 3 Map after Latest Forest Fire =========================== 0 1 2 3 4 5 6 7 0 -1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 3 0 2 0 0 0 1 0 2 0 4 3 0 0 0 4 0 0 0 0 4 0 3 0 0 2 0 0 0 5 0 0 0 0 0 0 0 0 6 0 0 0 4 0 4 2 0 7 0 4 0 0 0 0 4 0 Fire At (0,0) done. Row = 1 Column = 2 Wind blows toward (1:Up 2:Down 3:Left 4:Right): 3 Map after Latest Forest Fire =========================== 0 1 2 3 4 5 6 7 0 -1 0 -1 0 0 0 0 0 1 0 -1 -1 0 0 0 3 0 2 0 0 -1 1 0 2 0 4 3 0 0 0 4 0 0 0 0 4 0 3 0 0 2 0 0 0 5 0 0 0 0 0 0 0 0 6 0 0 0 4 0 4 2 0 7 0 4 0 0 0 0 4 0 Fire At (1,2) done. Row = 6 Column = 6 Wind blows toward (1:Up 2:Down 3:Left 4:Right): 4 Map after Latest Forest Fire =========================== 0 1 2 3 4 5 6 7 0 -1 0 -1 0 0 0 0 0 1 0 -1 -1 0 0 0 3 0 2 0 0 -1 1 0 2 -1 4 3 0 0 0 4 0 0 -1 -1 4 0 3 0 0 2 0 -1 -1 5 0 0 0 0 0 0 -1 -1 6 0 0 0 4 0 4 -1 -1 7 0 4 0 0 0 0 -1 -1 Fire At (6,6) done. Row = 4 Column = 4 Wind blows toward (1:Up 2:Down 3:Left 4:Right): 2 Map after Latest Forest Fire =========================== 0 1 2 3 4 5 6 7 0 -1 0 -1 0 0 0 0 0 1 0 -1 -1 0 0 0 3 0 2 0 0 -1 1 0 2 -1 4 3 0 0 0 4 0 0 -1 -1 4 0 3 -1 -1 -1 -1 -1 -1 5 0 0 0 -1 -1 -1 -1 -1 6 0 0 0 4 -1 4 -1 -1 7 0 4 0 0 0 0 -1 -1 Fire At (4,4) done. Row = 2 Column = 5 Wind blows toward (1:Up 2:Down 3:Left 4:Right): 3 Map after Latest Forest Fire =========================== 0 1 2 3 4 5 6 7 0 -1 -1 -1 -1 0 -1 0 0 1 -1 -1 -1 -1 -1 -1 3 0 2 -1 -1 -1 -1 -1 -1 0 4 3 -1 -1 -1 -1 -1 -1 0 -1 4 -1 -1 -1 -1 -1 -1 -1 -1 5 -1 -1 -1 -1 -1 -1 -1 -1 6 -1 -1 -1 -1 -1 4 -1 -1 7 -1 -1 -1 -1 0 0 -1 -1 Fire At (2,5) done. Program Terminated. Bye Bye!