-
Notifications
You must be signed in to change notification settings - Fork 0
/
forestfire.py
74 lines (65 loc) · 2.23 KB
/
forestfire.py
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
import sys, argparse
import random, time, bext
WIDTH = 79
HEIGHT = 22
TREE = '^'
FIRE = '*'
EMPTY = ' '
INITIAL_TREE_DENSITY = 0.2
GROW_CHANCE = 0.01
FIRE_CHANCE = 0.01
PAUSE_LENGTH= 0.5
def displayForest(forest):
bext.goto(0,0)
for y in range(forest['height']):
for x in range(forest['width']):
if forest[(x,y)] == TREE:
bext.fg('green')
print(TREE,end='')
elif forest[(x,y)] == FIRE:
bext.fg('red')
print(FIRE,end='')
elif forest[(x,y)] == EMPTY:
print(EMPTY,end='')
print()
bext.fg('reset')
print('GROW CHANCE: {}% '.format(GROW_CHANCE *100), end='')
print('Lighting CHANCE: {}% '.format(FIRE_CHANCE *100), end='')
def createNewforest():
forest = { 'width' : WIDTH, 'height' : HEIGHT}
for x in range(WIDTH):
for y in range(HEIGHT):
if(random.random() * 100 ) <= INITIAL_TREE_DENSITY:
forest[(x, y)] = TREE
else:
forest[(x, y)] = EMPTY
return forest
def main():
forest = createNewforest()
bext.clear()
while True:
displayForest(forest)
nextforest = { 'width' : forest['width'], 'height' : forest['height']}
for x in range(forest['width']):
for y in range(forest['height']):
if (x,y) in nextforest:
continue
if((forest[(x,y)] == EMPTY) and (random.random() <= GROW_CHANCE)):
nextforest[(x, y)] = TREE
elif((forest[(x,y)] == TREE) and (random.random() <= FIRE_CHANCE)):
nextforest[(x, y)] = FIRE
elif(forest[(x,y)] == FIRE):
for ix in range(-1, 2):
for iy in range(-1, 2):
if forest.get((x + ix, y + iy)) == TREE:
nextforest[(x + ix, y + iy)] = FIRE
nextforest[(x,y)] = EMPTY
else:
nextforest[(x,y)] = forest[(x,y)]
forest = nextforest
time.sleep(PAUSE_LENGTH)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
sys.exit()