-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
152 lines (120 loc) · 4.54 KB
/
grid.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
import pygame
import numpy as np
import random
class Grid:
def __init__(self, width, height, scale, offset):
self.scale = scale
self.columns = int(height/scale)
self.rows = int(width/scale)
self.size = (self.rows, self.columns)
self.grid_array = np.ndarray(shape=(self.size))
self.offset = offset
def random2d_array(self):
print("Executing Random Patterns!")
for x in range(self.rows):
for y in range(self.columns):
self.grid_array[x][y] = random.randint(0,1)
def full_array(self):
self.grid_array.fill(1)
self.grid_array.dump('export',allow_pickle=True)
def fixed2d_array(self):
self.grid_array[50][50] = 1
self.grid_array[51][50] = 1
self.grid_array[52][50] = 1
self.grid_array[53][50] = 1
self.grid_array[54][50] = 1
self.grid_array[50][51] = 1
self.grid_array[51][51] = 1
self.grid_array[52][51] = 1
self.grid_array[53][51] = 1
self.grid_array[54][51] = 1
self.grid_array[50][52] = 1
self.grid_array[51][52] = 1
self.grid_array[52][52] = 1
self.grid_array[53][52] = 1
self.grid_array[54][52] = 1
self.grid_array[50][53] = 1
self.grid_array[51][53] = 1
self.grid_array[52][53] = 1
self.grid_array[53][53] = 1
self.grid_array[54][53] = 1
self.grid_array[50][54] = 1
self.grid_array[51][54] = 1
self.grid_array[52][54] = 1
self.grid_array[53][54] = 1
self.grid_array[54][54] = 1
def gun(self):
self.grid_array[10][15] = 1
self.grid_array[10][16] = 1
self.grid_array[11][15] = 1
self.grid_array[11][16] = 1
self.grid_array[20][15] = 1
self.grid_array[20][16] = 1
self.grid_array[20][17] = 1
self.grid_array[21][14] = 1
self.grid_array[21][18] = 1
self.grid_array[22][13] = 1
self.grid_array[22][19] = 1
self.grid_array[23][13] = 1
self.grid_array[23][19] = 1
self.grid_array[24][16] = 1
self.grid_array[25][14] = 1
self.grid_array[25][18] = 1
self.grid_array[26][15] = 1
self.grid_array[26][16] = 1
self.grid_array[26][17] = 1
self.grid_array[27][16] = 1
self.grid_array[30][13] = 1
self.grid_array[30][14] = 1
self.grid_array[30][15] = 1
self.grid_array[31][13] = 1
self.grid_array[31][14] = 1
self.grid_array[31][15] = 1
self.grid_array[32][12] = 1
self.grid_array[32][16] = 1
self.grid_array[34][11] = 1
self.grid_array[34][12] = 1
self.grid_array[34][16] = 1
self.grid_array[34][17] = 1
self.grid_array[44][13] = 1
self.grid_array[44][14] = 1
self.grid_array[45][13] = 1
self.grid_array[45][14] = 1
def ask_for_the_file(self):
pattern_file = input("Enter the name of the file: ")
if pattern_file:
#TODO
pass
else:
print("No Pattern File Given..")
def Conway(self, off_color, on_color, surface):
for x in range(self.rows):
for y in range(self.columns):
y_pos = y * self.scale
x_pos = x * self.scale
random_color = (random.randint(10, 255), random.randint(10, 255), random.randint(10, 255))
if self.grid_array[x][y] == 1:
pygame.draw.rect(surface, on_color, [x_pos, y_pos, self.scale-self.offset, self.scale-self.offset])
else:
pygame.draw.rect(surface, off_color, [x_pos, y_pos, self.scale-self.offset, self.scale-self.offset])
next = np.ndarray(shape=(self.size))
for x in range(self.rows):
for y in range(self.columns):
state = self.grid_array[x][y]
neighbours = self.get_neighbours( x, y)
if state == 0 and neighbours == 3:
next[x][y] = 1
elif state == 1 and (neighbours < 2 or neighbours > 3):
next[x][y] = 0
else:
next[x][y] = state
self.grid_array = next
def get_neighbours(self, x, y):
total = 0
for n in range(-1, 2):
for m in range(-1, 2):
x_edge = (x+n+self.rows) % self.rows
y_edge = (y+m+self.columns) % self.columns
total += self.grid_array[x_edge][y_edge]
total -= self.grid_array[x][y]
return total