-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
132 lines (114 loc) · 4.01 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
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
import random
import time
class Game:
def __init__(self, m=20, n=20, fertility=False):
print 'new game created'
#Start by defining a m x n grid size
self.m = m #m rows
self.n = n #n columns
#If the fertility parameter was not passed at initialization, get it from the user
if not fertility:
fertility = input("Enter, as decimal, the land's fertility: ")
assert fertility>0 and fertility<1 , 'fertility set to ' + str(fertility) + '. Must be between 0 and 1'
#Initializes the grid with randomly generated cells according to fertility probability
def seedCellsToGrid():
#Initialize the cells in the starting grid
self.grid = []
for cell in range(0,self.m*self.n):
self.grid.append(random.random()<fertility)
seedCellsToGrid()
#Get the value of cell at location i,j
def cell(self,i,j):
#If the cell asked for is outside of hte grid, return false
if i<self.m and j<self.n and i>=0 and j>=0:
return self.grid[i*self.n + j]
else:
return False
#Print the grid of cells
def printGrid(self):
#Print the mxn grid of cells
str= '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n'
for column in range(0,self.n):
str = str + '---'
str = str + '\n'
for i in range (0,self.m):
str = str + '|'
for j in range(0,self.n):
if self.cell(i,j):
str = str + '* '
else:
str = str + ' '
str = str + '|\n'
for column in range(0,self.n):
str = str + '---'
print str
#Given the (i,j)th cell, count its live neighbors
def getAdj(self,i,j):
assert i>=0 and j>=0 and i<self.m and j<self.n, 'Attempted to count adjacent cells to invalid location ' + str(i) + ',' + str(j)
#print('Counting neighbors for cell at ' + str(i) + ',' + str(j))
neighbors=sum([self.cell(i-1,j-1),self.cell(i,j-1),self.cell(i+1,j-1),self.cell(i-1,j),
self.cell(i+1,j),self.cell(i-1,j+1),self.cell(i,j+1),self.cell(i+1,j+1)])
#print(str(neighbors) + ' found')
return neighbors
#Check to see whether a given cell will support live next turn
def supportsLife(self,i,j):
if self.getAdj(i,j) == 3:
return True
elif self.getAdj(i,j) == 2 and self.cell(i,j):
return True
else:
return False
#Update self.grid to the next step, given the rules of the game
def nextStep(self):
#initialize a brand new grid
newGrid = []
#Check each location to see if it will be alive or dead, and append to grid array
for i in range(0,self.m):
for j in range(0,self.n):
newGrid.append(self.supportsLife(i,j))
try:
self.past2grid = self.lastGrid
self.lastGrid = self.grid
self.grid = newGrid
except:
self.lastGrid = self.grid
self.grid = newGrid
def historyRepeats(self):
try:
if self.grid == self.lastGrid or self.grid == self.past2grid:
return True
else:
return False
except:
return False
def run(self):
sleep=.07
while(not self.historyRepeats()):
self.printGrid()
self.nextStep()
time.sleep(sleep)
for i in range(0,20):
self.printGrid()
self.nextStep()
time.sleep(sleep)
if __name__ == '__main__':
sleep=.07
width=50
height=37
density=.2
def runSimulation(sleep,width,height,density):
game = Game(height,width,density)
game.printGrid()
#wait=raw_input('Press enter to begin')
while(not game.historyRepeats()):
game.printGrid()
game.nextStep()
time.sleep(sleep)
for i in range(0,20):
game.printGrid()
game.nextStep()
time.sleep(sleep)
print 'The game has ended. Restarting in 5 seconds'
time.sleep(5)
while True:
runSimulation(sleep,width,height,density)