/
life2.py
207 lines (151 loc) · 4.82 KB
/
life2.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import csplot
import time
import random
from copy import deepcopy #for copying Boards
from copy import copy #for copying states
class Board():
class_attribute = None
def __init__(self, width, height=None):
#creates a 2d array
self.width = width
if height is None:
self.height = width
else:
self.height = height
self.board = [[0]*self.width for x in range(self.height)]
def update1a(self):
#adds a diagonal strip of "on" cells from SW corner
new = deepcopy(self)
for row in range(self.height):
for col in range(self.width):
if row == col:
new.board[row][col] = 1
return new
def update1b(self):
#adds a diagonal strip of "on" cells from NW corner
new = deepcopy(self)
for row in range(self.height):
for col in range(self.width):
if (self.height-1 - row) == col:
new.board[row][col] = 1
return new
def update1c(self):
#adds a diagonal strip of "on" cells from SE corner
new = deepcopy(self)
for row in range(self.height):
for col in range(self.width):
if row == (width-1 - col):
new.board[row][col] = 1
return new
def update1d(self):
#adds a diagonal strip of "on" cells from NE corner
new = deepcopy(self)
for row in range(self.height):
for col in range(self.width):
if (self.height - row) == (self.width - col):
new.board[row][col] = 1
return new
def diagnals(self):
# applies diagnal lines from all corners
new = deepcopy(self)
new = update1a(new)
new = update1b(new)
new = update1c(new)
return update1d(new)
def random(self):
# returns a board generated randomly of the same size as self
new = deepcopy(self)
new.board = [[random.choice([0,1])for x in ([0]*self.width)]
for z in [0]*self.height]
return new
def fill(self):
# returns a board that is all 1s, same dimentions as self
new = deepcopy(self)
new.board = [[1]*self.width for R in [1]*self.height]
return new
def blank(self):
# returns a board that is all 1s, same dimentions as self
new = deepcopy(self)
new.board = [[0]*self.width for R in [1]*self.height]
return new
def border(self, wide=1, bordervalue=0):
# returns a board that has a border added on top of board B to it
# of width: wide and of value: bordervalue
exception = False
if self.width < wide: exception = True
if self.height < wide: exception = True
if exception:
if bordervalue:
return self.fill()
else:
return self.blank()
new = deepcopy(self)
for row in range(wide) + range(self.height - wide, self.height):
for col in range(self.width):
new.board[row][col] = bordervalue
for col in range(wide) + range(self.width - wide, self.width):
for row in range(self.height):
new.board[row][col] = bordervalue
return new
def invert(self):
# returns a board that is the inverse of the input
new = deepcopy(self)
for row in range(self.height):
for col in range(self.width):
if self.board[row][col]:
new.board[row][col] = 0
else :
new.board[row][col] = 1
return new
def update2(self):
return self.fill().boarder()
def updateR(self):
return self.random().border()
def neighbors(self , row , col , wide = 1):
# returns the number of neighbors that are within a radius of wide
# of cell self.board[row][col]
# error catching
rowL = max(0, row-wide)
rowU = min(row+wide+1, self.height)
colL = max(0, col-wide)
colU = min(col+wide+1, self.width)
sum = -self.board[row][col]
for r in range(rowL, rowU):
for c in range (colL, colU):
sum += self.board[r][c]
return sum
def life(width, height=None):
# will become John Conway's Game of Life...
if height is None: height = width
B = Board(width, height).random()
while True: # run forever
csplot.show(B.board) # show current B
time.sleep(0.25) # pause a bit
B = B.random() # sets the new board correctly
def updateNextLife(B):
newB = B.blank()
for row in range(B.height):
for col in range(B.width):
n = B.neighbors(row, col)
#if ( n > 3 ) or ( n < 2 ):
#newB[row][col] = 0
if n == 3 :
newB.board[row][col] = 1
elif n == 2:
newB.board[row][col] = copy(B.board[row][col])
else :
newB.board[row][col] = 0
return newB
def life2(B):
# is John Conway's Game of Life...
csplot.showAndClickInIdle(B.board)
# hold s and click on the grid to bring a cell to life
# then close the grid and life will play
while True:
csplot.show(B.board) # show B
time.sleep(0.25) # pause a bit
B = updateNextLife(B) # sets the new board correctly
if B.board == B.blank().board:
break
csplot.show(B.board)
csplot.done()