-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_of_life.py
109 lines (97 loc) · 3.51 KB
/
game_of_life.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
# -*- coding: UTF-8 -*-
# Copyright (c) 2019 Claus Jørgensen
from functools import reduce
from os import system
from time import sleep
import copy
class GameOfLife(object):
"""
Conway's Game of Life
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
"""
def __init__(self, seed):
"""
Constructs a Game of Life
Parameters
----------
seed : list
The initial game seed (two-dimensional array).
"""
self.map = seed
def transition(self):
"""
Transitions the game once.
"""
newMap = copy.deepcopy(self.map)
for x in range(0, len(self.map) - 1):
for y in range(0, len(self.map[x]) - 1):
numAlive = reduce(lambda a, c: a + 1 if c == 1 else a, self.neighbors(x, y))
if numAlive < 2:
newMap[x][y] = 0 # death by under population
elif numAlive > 3:
newMap[x][y] = 0 # death by over population
elif self.map[x][y] == 0 and numAlive == 3:
newMap[x][y] = 1 # alive by reproduction
self.map = newMap
def __str__(self):
"""
Gets a string representation of the game's current state.
"""
output = ""
for x in range(0, len(self.map) - 1):
for y in range(0, len(self.map[x]) - 1):
if self.map[x][y] == 1:
output += "+ "
else:
output += " "
output += "\n"
return output
def neighbors(self, x, y):
"""
Gets the cell neighbors
"""
neighbors = []
if self.map[x - 1][y] >= 0:
neighbors.append(self.map[x - 1][y]) # left
if self.map[x + 1][y]:
neighbors.append(self.map[x + 1][y]) # right
if self.map[x][y - 1] >= 0:
neighbors.append(self.map[x][y - 1]) # top
if self.map[x][y + 1] >= 0:
neighbors.append(self.map[x][y + 1]) # bottom
if self.map[x - 1][y - 1] >= 0:
neighbors.append(self.map[x - 1][y - 1]) # top-left
if self.map[x - 1][y + 1] >= 0:
neighbors.append(self.map[x - 1][y + 1]) # bottom-left
if self.map[x + 1][y + 1] >= 0:
neighbors.append(self.map[x + 1][y + 1]) # top-right
if self.map[x + 1][y - 1] >= 0:
neighbors.append(self.map[x + 1][y - 1]) # bottom-right
return neighbors
pentadecathlon = [
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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