-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathLifeGameSparse.py
More file actions
60 lines (57 loc) · 2.21 KB
/
LifeGameSparse.py
File metadata and controls
60 lines (57 loc) · 2.21 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
from __future__ import print_function
import ast
from ProjectSparseLifeGrid import SparseLifeGrid
def main():
grid = SparseLifeGrid()
string_config = raw_input("Give tuples of r and c like: (1,2),(2,1),(2,2),(2,3) -> ")
INIT_CONFIG = ast.literal_eval(string_config)
#INIT_CONFIG = [(1,2), (2,1), (2,2), (2,3)]
grid.configure(INIT_CONFIG) #play the game
draw(grid)
play = True
while play:
evolve(grid)
user_desire = raw_input("Do you still go ahead? Yes for going othewise ends! -> ")
if(user_desire.lower() == 'yes'):
play = True
else:
play = False
draw(grid)
def evolve(grid):
gridList = list()
row_min, col_min = grid.minRange()
row_max, col_max = grid.maxRange()
for r in range(row_min - 1, row_max + 2):#row_max is increemented by 2 due to range function
for c in range(col_min - 1, col_max + 2):
neighbors = grid.numLiveNeighbors(r,c)
if (r == row_min - 1) or (c == col_min -1) \
or (r == row_max + 1) or (c == col_max +1):
if neighbors == 3:
#grid.setCell(r,c)
gridList.append((r,c))
# neighbors 3 and outside the grid
#inside the grid
else:
if (neighbors == 2 and grid.isLiveCell(r,c)) or \
(neighbors == 3 and grid.isLiveCell(r,c)):
#grid.setCell(r,c) #set the cell
gridList.append((r,c))
if (neighbors == 3 and not grid.isLiveCell(r,c)):
gridList.append((r,c))
#else:
#grid.clearCell(r,c)
#Grid clearCell and setCell is not used here because of loop
#I have to think how to make it yet
grid.configure(gridList)
def draw(grid):
row_min, col_min = grid.minRange()
row_max, col_max = grid.maxRange()
for r in range(row_min, row_max+1):
for c in range(col_min, col_max+1):
if grid.isLiveCell(r,c):
print('x', sep = ' ', end= ' ')
else:
print('o', sep= ' ', end= ' ')
print ('\n')
print("----next gen-----")
main()