-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgol_gen.py
63 lines (51 loc) · 2.07 KB
/
gol_gen.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
import pickle
from gol import GOL
def test_iterations(structuresize, gridsize, rounds, failisblank = True,failnochange = False):
'''
Generates a list of all possible structures based on structuresize (a sqare grid)
tests them for rounds, and returns the ones that pass
'''
teststructures = generate_binary_grid(structuresize)
results = []
for structure in teststructures:
game = GOL(gridsize,gridsize)
game.add_structure(structure, xoffset=int(gridsize/2),yoffset=int(gridsize/2))
result = game.test(rounds, failisblank = failisblank,failnochange = failnochange)
if result == True:
results.append(structure)
return results
def test_list(struclist, gridsize, rounds,failisblank = True,failnochange = False):
'''
Takes a list of structures, places each of them on the gameboard in the lower right
quadrent, and tests it for x number of rounds. Returns a list of strucures that have passed
'''
results=[]
for structure in struclist:
game = GOL(gridsize,gridsize)
game.add_structure(structure,xoffset=int(gridsize/2),yoffset=int(gridsize/2))
result = game.test(rounds, failisblank = failisblank,failnochange = failnochange)
if result == True:
results.append(structure)
return results
def rotate_structure(structure):
'''
Rotates a structre clockwise once.
'''
def generate_binary_grid(gridsize):
'''
Generates all possible iterations of a square binary matrix. Gridsize is
the size of the side of the square - eg. 3 generates all 3x3 binary matrixs
'''
results = []
grid = [0 for x in range(gridsize**2)] # Generates gird of all zeros
for x in range((2**(gridsize)**2)-1): # Iterates though
grid[0] += 1
for index, entry in enumerate(grid):
if entry > 1:
grid[index] = 0
grid[index+1] += 1
else:
break
new_grid = [grid[i:i+gridsize] for i in range(0,len(grid),gridsize)]
results.append(new_grid)
return(results)