-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
106 lines (93 loc) · 3.2 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
import pygame
import sys
# Parameters
b = 1.65
play_color = (251, 234, 235) # Pastel Pink
pause_color = (0, 0, 0) # Black
background_color = (47, 60, 126) # Blue
grid_color = (0, 0, 0) # Black
cell_size = 10
width, height = 610, 610
tick_rate = 50
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Prisoners Dilemma Cellular Automaton')
COOPERATE = 0
DEFECT = 1
# Grid dimensions
grid_width = width // cell_size
grid_height = height // cell_size
# Cell states array
cell_states = [[0] * grid_height for _ in range(grid_width)]
cell_states[grid_width // 2][grid_height // 2] = 1
paused = True
states = 2
def draw_grid():
for x in range(grid_width):
for y in range(grid_height):
rect = pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size)
if cell_states[x][y] == 1:
color = play_color
if paused:
color = pause_color
pygame.draw.rect(screen, color, rect)
pygame.draw.rect(screen, grid_color, rect, 1) # Grid line color
def handle_click(pos):
x, y = pos
grid_x = x // cell_size
grid_y = y // cell_size
cell_states[grid_x][grid_y] = (cell_states[grid_x][grid_y] + 1) % states
def get_score(x, y):
score = 0
cell_state = cell_states[x][y]
for i in range(-1, 2):
for j in range(-1, 2):
if 0 <= x + i < grid_width and 0 <= y + j < grid_height:
if i == 0 and j == 0:
continue
if cell_states[x + i][y + j] == COOPERATE:
if cell_state == COOPERATE:
score += 1
else:
score += b
return score
def update_cell(x, y, scores):
max_score = 0
max_state = COOPERATE
for i in range(-1, 2):
for j in range(-1, 2):
if 0 <= x + i < grid_width and 0 <= y + j < grid_height:
if scores[x + i][y + j] > max_score:
max_score = scores[x + i][y + j]
max_state = cell_states[x + i][y + j]
return max_state
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
handle_click(pygame.mouse.get_pos())
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
paused = not paused
if event.key == pygame.K_c:
cell_states = [[0] * grid_height for _ in range(grid_width)]
cell_states[grid_width // 2][grid_height // 2] = 1
screen.fill(background_color)
draw_grid()
if not paused:
next_states = [[0] * grid_height for _ in range(grid_width)]
scores = [[0] * grid_height for _ in range(grid_width)]
for x in range(grid_width):
for y in range(grid_height):
scores[x][y] = get_score(x, y)
for x in range(grid_width):
for y in range(grid_height):
next_states[x][y] = update_cell(x, y, scores)
cell_states = next_states
pygame.display.flip()
pygame.time.delay(tick_rate)
pygame.quit()
sys.exit()