-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
99 lines (74 loc) · 1.89 KB
/
game.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
import random
import copy
class World():
def __init__(self, width, height):
self.width = width
self.height = height
self.world = []
def generate(self):
self.world = []
for i in range(self.height):
self.world.append([random.choice([Cell(0), Cell(1)]) for j in range(self.width)])
def set(self, world):
self.world = world
def show(self):
for x in range(self.height):
print ''.join([str(y.status) for y in self.world[x]])
def evolve(self):
evolvers = [
Killer(),
Killer(),
Keeper(),
LifeGiver(),
Killer(),
Killer(),
Killer(),
Killer(),
Killer(),
]
evolution = copy.deepcopy(self.world)
neighbours = self.get_neighbourhoods()
for x in range(self.width):
for y in range(self.height):
evolver = evolvers[neighbours[y][x]]
evolver.act(evolution[y][x])
self.world = evolution
def get_neighbourhoods(self):
bordered_world = copy.deepcopy(self.world)
#add borders
bordered_world.insert(0, [Cell(0) for i in range(self.width)])
bordered_world.append([Cell(0) for i in range(self.width)])
for i in range(self.height+2):
bordered_world[i].insert(0, Cell(0))
bordered_world[i].append(Cell(0))
neighbours = []
for y in range(self.height):
neighbours.append([])
for x in range(self.width):
neighbours[y].append(0)
#count neighbours
for x in range(self.width):
for y in range(self.height):
for i in range(-1, 2):
for j in range(-1, 2):
neighbours[y][x] += bordered_world[y+1+i][x+1+j].status
neighbours[y][x] -= bordered_world[y+1][x+1].status
return neighbours
class Cell():
def __init__(self, status):
self.status = status
def show(self):
print self.status
def die(self):
self.status = 0
def live(self):
self.status = 1
class Killer():
def act(self, cell):
cell.die()
class Keeper():
def act(self, cell):
pass
class LifeGiver():
def act(self, cell):
cell.live()