-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconway_game_of_life.py
More file actions
223 lines (174 loc) · 8.17 KB
/
conway_game_of_life.py
File metadata and controls
223 lines (174 loc) · 8.17 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# This code is taken from https://github.com/mwharrisjr/Game-of-Life
import time
import os
import random
import sys
def clear_console():
"""
Clears the console using a system command based on the user's operating system.
"""
if sys.platform.startswith('win'):
os.system("cls")
elif sys.platform.startswith('linux'):
os.system("clear")
else:
print("Unable to clear terminal. Your operating system is not supported.\n\r")
def resize_console(rows, cols):
"""
Re-sizes the console to the size of rows x columns
:param rows: Int - The number of rows for the console to re-size to
:param cols: Int - The number of columns for the console to re-size to
"""
if cols < 32:
cols = 32
if sys.platform.startswith('win'):
command = "mode con: cols={0} lines={1}".format(cols + cols, rows + 5)
os.system(command)
elif sys.platform.startswith('linux'):
command = "\x1b[8;{rows};{cols}t".format(rows=rows + 3, cols=cols + cols)
sys.stdout.write(command)
else:
print("Unable to resize terminal. Your operating system is not supported.\n\r")
def create_initial_grid(rows, cols):
"""
Creates a random list of lists that contains 1s and 0s to represent the cells in Conway's Game of Life.
:param rows: Int - The number of rows that the Game of Life grid will have
:param cols: Int - The number of columns that the Game of Life grid will have
:return: Int[][] - A list of lists containing 1s for live cells and 0s for dead cells
"""
grid = []
for row in range(rows):
grid_rows = []
for col in range(cols):
# Generate a random number and based on that decide whether to add a live or dead cell to the grid
if random.randint(0, 7) == 0:
grid_rows += [1]
else:
grid_rows += [0]
grid += [grid_rows]
return grid
def print_grid(rows, cols, grid, generation):
"""
Prints to console the Game of Life grid
:param rows: Int - The number of rows that the Game of Life grid has
:param cols: Int - The number of columns that the Game of Life grid has
:param grid: Int[][] - The list of lists that will be used to represent the Game of Life grid
:param generation: Int - The current generation of the Game of Life grid
"""
clear_console()
# A single output string is used to help reduce the flickering caused by printing multiple lines
output_str = ""
# Compile the output string together and then print it to console
output_str += "Generation {0} - To exit the program early press <Ctrl-C>\n\r".format(generation)
for row in range(rows):
for col in range(cols):
if grid[row][col] == 0:
output_str += ". "
else:
output_str += "@ "
output_str += "\n\r"
print(output_str, end=" ")
def create_next_grid(rows, cols, grid, next_grid):
"""
Analyzes the current generation of the Game of Life grid and determines what cells live and die in the next
generation of the Game of Life grid.
:param rows: Int - The number of rows that the Game of Life grid has
:param cols: Int - The number of columns that the Game of Life grid has
:param grid: Int[][] - The list of lists that will be used to represent the current generation Game of Life grid
:param next_grid: Int[][] - The list of lists that will be used to represent the next generation of the Game of Life
grid
"""
for row in range(rows):
for col in range(cols):
# Get the number of live cells adjacent to the cell at grid[row][col]
live_neighbors = get_live_neighbors(row, col, rows, cols, grid)
# If the number of surrounding live cells is < 2 or > 3 then we make the cell at grid[row][col] a dead cell
if live_neighbors < 2 or live_neighbors > 3:
next_grid[row][col] = 0
# If the number of surrounding live cells is 3 and the cell at grid[row][col] was previously dead then make
# the cell into a live cell
elif live_neighbors == 3 and grid[row][col] == 0:
next_grid[row][col] = 1
# If the number of surrounding live cells is 3 and the cell at grid[row][col] is alive keep it alive
else:
next_grid[row][col] = grid[row][col]
def get_live_neighbors(row, col, rows, cols, grid):
"""
Counts the number of live cells surrounding a center cell at grid[row][cell].
:param row: Int - The row of the center cell
:param col: Int - The column of the center cell
:param rows: Int - The number of rows that the Game of Life grid has
:param cols: Int - The number of columns that the Game of Life grid has
:param grid: Int[][] - The list of lists that will be used to represent the Game of Life grid
:return: Int - The number of live cells surrounding the cell at grid[row][cell]
"""
life_sum = 0
for i in range(-1, 2):
for j in range(-1, 2):
# Make sure to count the center cell located at grid[row][col]
if not (i == 0 and j == 0):
# Using the modulo operator (%) the grid wraps around
life_sum += grid[((row + i) % rows)][((col + j) % cols)]
return life_sum
def grid_changing(rows, cols, grid, next_grid):
"""
Checks to see if the current generation Game of Life grid is the same as the next generation Game of Life grid.
:param rows: Int - The number of rows that the Game of Life grid has
:param cols: Int - The number of columns that the Game of Life grid has
:param grid: Int[][] - The list of lists that will be used to represent the current generation Game of Life grid
:param next_grid: Int[][] - The list of lists that will be used to represent the next generation of the Game of Life
grid
:return: Boolean - Whether the current generation grid is the same as the next generation grid
"""
for row in range(rows):
for col in range(cols):
# If the cell at grid[row][col] is not equal to next_grid[row][col]
if not grid[row][col] == next_grid[row][col]:
return True
return False
def get_integer_value(prompt, low, high):
"""
Asks the user for integer input and between given bounds low and high.
:param prompt: String - The string to prompt the user for input with
:param low: Int - The low bound that the user must stay within
:param high: Int - The high bound that the user must stay within
:return: The valid input value that the user entered
"""
while True:
try:
value = int(input(prompt))
except ValueError:
print("Input was not a valid integer value.")
continue
if value < low or value > high:
print("Input was not inside the bounds (value <= {0} or value >= {1}).".format(low, high))
else:
break
return value
def run_game():
"""
Asks the user for input to setup the Game of Life to run for a given number of generations.
"""
clear_console()
# Get the number of rows and columns for the Game of Life grid
rows = get_integer_value("Enter the number of rows (10-60): ", 10, 60)
cols = get_integer_value("Enter the number of cols (10-118): ", 10, 118)
# Get the number of generations that the Game of Life should run for
generations = get_integer_value("Enter the number of generations (1-100000): ", 1, 100000)
resize_console(rows, cols)
# Create the initial random Game of Life grids
current_generation = create_initial_grid(rows, cols)
next_generation = create_initial_grid(rows, cols)
# Run Game of Life sequence
gen = 1
for gen in range(1, generations + 1):
if not grid_changing(rows, cols, current_generation, next_generation):
break
print_grid(rows, cols, current_generation, gen)
create_next_grid(rows, cols, current_generation, next_generation)
time.sleep(1 / 5.0)
current_generation, next_generation = next_generation, current_generation
print_grid(rows, cols, current_generation, gen)
input("Press <Enter> to exit.")
# Start the Game of Life
run_game()