This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_universe.py
138 lines (116 loc) · 4.05 KB
/
generate_universe.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
from dict_seed import gun
import numpy as np
import matplotlib.pyplot as plt
def generate_universe(size):
"""
Generates universe of given size with dead cells
Parameters
----------
size :
Tuple (int, int) dimensions of the universe
Returns
-------
universe
Numpy array of the universe created; All dead cells
"""
return np.zeros(size, dtype=np.int)
def create_seed(type_seed):
'''
Creates the seed by the given dictionnary
Parameters
-------
type_seed
String of the type of seed
Returns
-------
seed
Seed obtained as a numpy array
'''
return np.array(dict_seed[type_seed])
def add_seed_to_universe(seed,universe,y_start=0,x_start=0):
'''
Adds the seed to universe by replacing the corresponding places
Parameters
-------
seed
Numpy array of the seed to be added
universe
Numpy array of the universe to be added
y_start
Start place of the row parameter to be added; By default 0)
x_start
Start place of the column parameter to be added; By default 0)
Returns
-------
universe
Numpy array of the universe with seed inserted
'''
num_rows_seed,num_cols_seed = seed.shape
num_rows_univ,num_cols_univ = universe.shape
# Detect if seed violates constraints of the universe
# If yes, return universe without seed inserted
try:
if num_rows_seed > num_rows_univ or num_cols_seed > num_cols_univ:
raise ValueError("The seed is bigger than the size of the universe!")
except:
return universe
excy = num_rows_seed + y_start - num_rows_univ
excx = num_cols_seed + x_start - num_cols_univ
if num_rows_univ >= y_start+num_rows_seed and num_cols_univ >= x_start+num_cols_seed:
universe[y_start:y_start+num_rows_seed,x_start:x_start+num_cols_seed] = seed
elif num_rows_univ < y_start+num_rows_seed and num_cols_univ > x_start+num_cols_seed:
universe[y_start:num_rows_univ,x_start:x_start+num_rows_seed]=seed[0:num_rows_seed-excy,0:num_cols_seed]
universe[0:excy,0:num_cols_seed]=seed[num_rows_seed-excy:num_rows_seed,0:num_cols_seed]
elif num_rows_univ > y_start+num_rows_seed and num_cols_univ < x_start+num_cols_seed:
universe[y_start:y_start+num_rows_seed,x_start:num_rows_univ]=seed[0:num_rows_seed,0:num_cols_seed-excx]
universe[0:num_rows_seed,0:excx]=seed[0:num_rows_seed,num_cols_seed-excx:num_cols_seed]
elif num_rows_univ < y_start+num_rows_seed and num_cols_univ < x_start+num_cols_seed:
universe[y_start:num_rows_univ,x_start:num_cols_univ]=seed[0:num_rows_seed-excy,0:num_cols_seed-excx]
universe[0:excy,0:num_cols_seed]=seed[num_rows_seed-excy:num_rows_seed,0:num_cols_seed]
universe[0:num_rows_seed,0:excx]=seed[0:num_rows_seed,num_cols_seed-excx:num_cols_seed]
return universe
def display_universe(universe):
'''
Displays the universe
Parameters:
-------
universe
Numpy array of universe to be displayed
'''
plt.imshow(universe, cmap='Greys')
plt.show()
dict_seed = {
"boat": [[1, 1, 0], [1, 0, 1], [0, 1, 0]],
"r_pentomino": [[0, 1, 1], [1, 1, 0], [0, 1, 0]],
"beacon": [[1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1]],
"acorn": [[0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [1, 1, 0, 0, 1, 1, 1]],
"block_switch_engine": [
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 1],
[0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0],
],
"infinite": [
[1, 1, 1, 0, 1],
[1, 0, 0, 0, 0],
[0, 0, 0, 1, 1],
[0, 1, 1, 0, 1],
[1, 0, 1, 0, 1],
],
'planeur':[
[0,1,0],
[1,1,0],
[1,0,1]
],
'line_3':[
[1,1,1]
],
'gun':gun()
}
if __name__ == "__main__":
universe=generate_universe([6,6])
#print(np.array([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]))
#print(create_seed(type_seed = "r_pentomino"))
pass