-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
168 lines (133 loc) · 4.53 KB
/
main.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
# Pere Muñoz Figuerol - Connway's Game of Life
import sys
import random
from time import sleep
import pygame
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREY = (128, 128, 128)
# Screen
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Cell size
CELL_SIZE = 10
# Game control
global CELLS_ALIVE
CELLS_ALIVE = 0
def main():
# Control entry parameters
if len(sys.argv) < 2:
print('Usage: python main.py <execution mode> [random]')
sys.exit(1)
EXECUTION_MODE = sys.argv[1]
# Init
pygame.init()
# Screen
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Conway's Game of Life by Pere Muñoz")
if len(sys.argv) > 2 and sys.argv[2] == 'random':
print('Random mode')
state = [[random.randint(0, 1) for i in range(0, SCREEN_WIDTH, CELL_SIZE)] for j in
range(0, SCREEN_HEIGHT, CELL_SIZE)]
else:
state = readInitialState()
printStartingGame(SCREEN)
drawGrid(SCREEN, state)
countAliveCells(state)
pygame.display.set_caption(
"Conway's Game of Life by Pere Muñoz - Iteration 0 - Cells alive: {}".format(CELLS_ALIVE))
iterations = 0
while CELLS_ALIVE > 0:
if EXECUTION_MODE == 'manual':
while True:
event = pygame.event.wait()
if event.type == pygame.KEYDOWN:
break
if event.type == pygame.QUIT:
pygame.quit()
quit()
else:
sleep(0.22)
iterations += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
drawGrid(SCREEN, state)
processState(state)
pygame.display.update()
pygame.display.set_caption(
"Conway's Game of Life by Pere Muñoz - Iteration " + str(iterations) + " - Cells alive: " + str(
CELLS_ALIVE))
printGameOver(SCREEN)
def printGameOver(screen):
screen.fill(WHITE)
sysFont = pygame.font.SysFont(pygame.font.get_default_font(), 50)
img = sysFont.render('Game over !!', True, RED)
screen.blit(img, (SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 - 50))
pygame.display.update()
sleep(2)
def printStartingGame(screen):
screen.fill(WHITE)
sysFont = pygame.font.SysFont(pygame.font.get_default_font(), 30)
img = sysFont.render('Starting game', True, RED)
screen.blit(img, (SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 - 50))
pygame.display.update()
sleep(0.5)
for i in range(0, 4):
img = sysFont.render('.' * i, True, RED)
screen.blit(img, (SCREEN_WIDTH / 2 + 40, SCREEN_HEIGHT / 2 - 50))
pygame.display.update()
sleep(0.5)
screen.fill(WHITE)
pygame.display.update()
def processState(state):
for i in range(len(state)):
for j in range(len(state[i])):
state[i][j] = processCell(state, i, j)
def processCell(state, i, j):
global CELLS_ALIVE
neighbours = getNeighbours(state, i, j)
if state[i][j] == 1:
if neighbours < 2 or neighbours > 3:
CELLS_ALIVE -= 1
return 0
else:
return 1
else:
if neighbours == 3:
CELLS_ALIVE += 1
return 1
else:
return 0
def getNeighbours(state, i, j):
neighbours = 0
for k in range(i - 1, i + 2):
for l in range(j - 1, j + 2):
if 0 <= k < len(state) and 0 <= l < len(state[i]) and (k != i or l != j):
neighbours += 1 if state[k][l] == 1 else 0
return neighbours
def readInitialState():
with open('initialState.txt', 'r') as initialFile:
matrix = [[int(num) for num in line.split(',')] for line in initialFile]
return matrix
def countAliveCells(state):
global CELLS_ALIVE
CELLS_ALIVE = 0
for i in range(len(state)):
for j in range(len(state[i])):
CELLS_ALIVE += 1 if state[i][j] == 1 else 0
def drawGrid(screen, state):
global CELLS_ALIVE
for i in range(0, SCREEN_WIDTH, CELL_SIZE):
for j in range(0, SCREEN_HEIGHT, CELL_SIZE):
cellLine = pygame.Rect(i, j, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, GREY, cellLine, 1)
cell = pygame.Rect(i + 2, j + 2, CELL_SIZE - 4, CELL_SIZE - 4)
normalizedState = state[int(j / CELL_SIZE)][int(i / CELL_SIZE)]
pygame.draw.rect(screen, BLACK if normalizedState == 1 else WHITE, cell,
0)
if __name__ == "__main__":
main()