-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforestSerial.c
More file actions
129 lines (125 loc) · 2.33 KB
/
forestSerial.c
File metadata and controls
129 lines (125 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <stdlib.h>
//
#include <time.h>
//
void display(char forest[30][30], int gridSize)
{
printf("\033[2J\033[1;1H");
int i, j;
for (i = 0; i < gridSize; i++)
{
for (j = 0; j < gridSize; j++)
{
if (forest[i][j] == 'T')
printf("\x1b[32m%c\x1b[0m ", forest[i][j]);
else if (forest[i][j] == '-')
printf("\x1b[33m%c\x1b[0m ", forest[i][j]);
else
printf("\x1b[31m%c\x1b[0m ", forest[i][j]);
}
printf("\n");
}
}
int hasFire(char forest[30][30], int gridSize)
{
int i, j;
for (i = 0; i < gridSize; i++)
{
for (j = 0; j < gridSize; j++)
{
if (forest[i][j] == 'X')
return 1;
}
}
return 0;
}
//
int main( int argc , char* argv[] )
{
srand(time(NULL));
double probability, ret;
int gridSize = 30;
char forest[gridSize][gridSize];
int i, j, count, k, trials = 10;
FILE* out = fopen("forestFireData.txt", "w");
for (probability = 0.0; probability < 1.0; probability += .1)
{
count = 0;
for (k = 0; k < trials; k++)
{
for (i = 0; i < gridSize; i++)
{
for (j = 0; j < gridSize; j++)
{
if (rand()/(float)(RAND_MAX) < probability)
{
forest[i][j] = 'T';
}
else
{
forest[i][j] = '-';
}
}
}
for (i = 0; i < gridSize; i++)
{
forest[i][0] = 'X';
}
//display(forest, gridSize);
while (hasFire(forest, gridSize))
{
for (i = 0; i < gridSize; i++)
{
for (j = 0; j < gridSize; j++)
{
if (forest[i][j] == 'X')
{
forest[i][j] = '-';
if (j > 0 && forest[i][j-1] == 'T')
{
forest[i][j-1] = 'x';
}
if (j < gridSize-1 && forest[i][j+1] == 'T')
{
forest[i][j+1] = 'x';
}
if (i > 0 && forest[i-1][j] == 'T')
{
forest[i-1][j] = 'x';
}
if (i < gridSize-1 && forest[i+1][j] == 'T')
{
forest[i+1][j] = 'x';
}
}
}
}
for (i = 0; i < gridSize; i++)
{
for (j = 0; j < gridSize; j++)
{
if (forest[i][j] == 'x')
forest[i][j] = 'X';
}
}
//display(forest, gridSize);
//usleep(500000);
count = count + 1;
}
}
fprintf(out, "%f\n", count/(trials*(float)gridSize));
}
//
fclose(out);
return 0;
}
//
// end of file
//
// time ./a.out
// Time: 0.3139879703521729 seconds
//
// real 0m0.315s
// user 0m0.313s
// sys 0m0.001s