-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameoflife.py
executable file
·105 lines (90 loc) · 3.31 KB
/
gameoflife.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
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
'''
This program is free software: you can redistribute it and/or modify
t under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import random
import Bricks
import Config
def update_wall(grid, x, y, geracao):
if (get_total(grid,x,y) < 30 or not(geracao % 300)):
return Bricks.build_grid_wall(x, y)
updated_grid = Bricks.build_grid_wall(x, y)
for i in range(Config.WIDTH * x):
for j in range((Config.HEIGHT * y) + Config.WIDTH):
neighbors = get_neighbors(i, j, Config.WIDTH * x, (Config.HEIGHT * y) + Config.WIDTH, grid)
if(get_cell(i, j, Config.WIDTH * x, (Config.HEIGHT * y) + Config.WIDTH, grid)):
if(neighbors < 2):
updated_grid[i,j] = Config.DEAD
elif(neighbors > 3):
updated_grid[i,j] = Config.DEAD
else:
updated_grid[i,j] = Config.LIVE
else:
if(neighbors == 3):
updated_grid[i,j] = Config.LIVE
else:
updated_grid[i,j] = Config.DEAD
return updated_grid
def update(grid, x, y, geracao):
if (get_total(grid,x,y) < 30 or not(geracao % 300)):
return Bricks.build_grid(x, y)
updated_grid = Bricks.build_grid(x, y)
for i in range(Config.WIDTH * x):
for j in range(Config.HEIGHT * y):
neighbors = get_neighbors(i, j, Config.WIDTH * x, Config.HEIGHT * y, grid)
if(get_cell(i, j, Config.WIDTH * x, Config.HEIGHT * y, grid)):
if(neighbors < 2):
updated_grid[i,j] = Config.DEAD
elif(neighbors > 3):
updated_grid[i,j] = Config.DEAD
else:
updated_grid[i,j] = Config.LIVE
else:
if(neighbors == 3):
updated_grid[i,j] = Config.LIVE
else:
updated_grid[i,j] = Config.DEAD
return updated_grid
def get_total(grid,x,y):
count = 0
for i in range(Config.WIDTH * x):
for j in range(Config.HEIGHT * y):
if (get_cell(i, j, Config.WIDTH * x, Config.HEIGHT * y, grid)):
count = count + 1
print "LIVE ",count
return count
def get_cell(x, y, x_max, y_max, g):
if(x < 0 or x >= x_max or y < 0 or y >= y_max):
return 0
else:
if(g[x,y] < 128):
return 1
else:
return 0
def get_neighbors(x,y,x_max,y_max,g):
out = 0
if(get_cell(x-1,y-1,x_max,y_max,g)):
out = out + 1
if(get_cell(x-1,y,x_max,y_max,g)):
out = out + 1
if(get_cell(x-1,y+1,x_max,y_max,g)):
out = out + 1
if(get_cell(x,y-1,x_max,y_max,g)):
out = out + 1
if(get_cell(x,y+1,x_max,y_max,g)):
out = out + 1
if(get_cell(x+1,y-1,x_max,y_max,g)):
out = out + 1
if(get_cell(x+1,y,x_max,y_max,g)):
out = out + 1
if(get_cell(x+1,y+1,x_max,y_max,g)):
out = out + 1
return out