-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameOfLife.py
160 lines (121 loc) · 4.41 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
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
'''
A simple implementation of Conway's Game of Life.
Input an integer as command line agrument to serve as the gameboard size.
Written by Aidan Kelly.
June 8, 2019
'''
import sys
import os
import random
import time
#ensuring that we have an integer inputted.
if(len(sys.argv) != 2):
print("Please enter the size of board you want to use as a command line argument.")
sys.exit()
try:
size = int(sys.argv[1])
except:
print("Please enter an integer for the size of your board.")
sys.exit()
#setting up our variables
maxRow = size-1
maxCol = size-1
liveToken = '0'
deadToken = ' '
#loop to build us a randomly generated gamestate
gameState = []
for i in range(size):
gameState.append([])
for j in range(size):
randInt = random.randint(0,1)
if(randInt == 0):
gameState[i].append(deadToken)
else:
gameState[i].append(liveToken)
#function to print the gamestate.
def printGameboard(gameState):
os.system('cls' if os.name=='nt' else 'clear')
#loop through the gamestate and print the token.
for index in gameState:
for item in index:
sys.stdout.write(str(item))
print("")
#Function to get the indices of a index's neighbors.
def getNeighbors(row, col):
#returns all of the neighbors based off of the caller's positioning on the board.
if row == 0 and col == 0:
return [[0,1],
[1,0], [1,1]]
elif row ==0 and col == maxCol:
return [[0, maxCol-1],
[1, maxCol-1], [1, maxCol]]
elif row == 0:
return [[0, col-1], [0, col+1],
[1, col-1], [1, col], [1, col+1]]
elif row == maxRow and col == 0:
return [[maxRow-1, 0],
[maxRow-1, 1], [maxRow, 1]]
elif row == maxRow and col == maxCol:
return [[maxRow, col-1],
[maxRow-1, col-1], [maxRow-1, col]]
elif row == maxRow:
return [[maxRow, col-1], [maxRow, col+1],
[maxRow-1, col-1], [maxRow-1, col], [maxRow-1, col+1]]
elif col == 0:
return [[row-1, col], [row-1, col+1],
[row, col+1],
[row+1, col], [row+1, col+1]]
elif col == maxCol:
return [[row-1, col], [row-1, col-1],
[row, col-1],
[row+1, col], [row+1, col-1]]
else:
return [[row-1, col-1], [row-1 , col], [row-1 , col+1],
[row, col-1], [row, col+1],
[row+1, col-1], [row+1, col], [row+1, col+1]]
#determine if a cell should live or die.
def liveOrDie(neighbors, entry):
#first we count the cell's live neighbors.
liveNeighborCount = 0
for neighbor in neighbors:
if gameState[neighbor[0]][neighbor[1]] == liveToken:
liveNeighborCount += 1
#we then make the cell live or dead based off of the rules of the game.
if(liveNeighborCount < 2 and entry == liveToken):
return False
elif(liveNeighborCount >3 and entry ==liveToken):
return False
elif(entry == deadToken and liveNeighborCount == 3):
return True
elif(entry == liveToken and (liveNeighborCount == 2 or liveNeighborCount ==3)):
return True
else:
return False
#main function to start the gameloop
def main():
#infinitly loop the game.
while(True):
global gameState
tempGameState = []
#loop through all indicies.
for i in range(0,size):
#add a row to our temp gamestate.
tempGameState.append([])
#loop through the columns.
for j in range(0,size):
#find our neighbors
ourNeighbors = getNeighbors(i, j)
#based off of our neighbors, make our cell live or dead
result = liveOrDie(ourNeighbors, gameState[i][j])
#update our cell
if(result == True):
tempGameState[i].append(liveToken)
else:
tempGameState[i].append(deadToken)
#print out the gameboard
printGameboard(tempGameState)
#make our temp gamestate the new gamestate
gameState = tempGameState
#sleep half a second so that the user can see what is happening.
time.sleep(0.5)
main()