Is this code able to be fixed? It is meant to be a search program that has the same function as lifesrc. I am doing this so that people have an easy way to search for spaceships without having too much knowledge in programming.
There are 3 main problems I have that need to be solved.
1: Searching doesn't work perfectly. It does look like it is doing something in the code, but it never calls the backup function and the consistency function may be incorrect.
2: If you set the period above 1, it stops the script immediately after starting.
3: There is no implication function or anything because I don't know how to calculate the rules for it.
The reason why everything is calculated in numpy arrays is because there is no unknown state in Golly.
Code: Select all
import math
import random
import golly as g
import numpy as np
from collections import deque
velx = 0
vely = 0
period = 1
width = 8
height = 8
neighbor = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]
def tocells(n, x, y, z=0, w=0):
c = []
for q in np.argwhere(n>0.5):
c.extend([q[0]+z,q[1]+w])
return c
def toarray(c, x, y, z=0, w=0):
n = np.zeros((x, y), dtype=np.int32)
for i in range(0, len(c), 2):
if 0 <= c[i]+z < x and 0 <= c[i+1]+w < y:
n[c[i]+z, c[i+1]+w] = 1
return n
def evolve(x, p, width=width, height=height):
return toarray(g.evolve(tocells(x, width, height, 0, 0), p), width, height, 0, 0)
def display(r, x=0, y=0):
if len(g.getrect()) != 0:
g.select(g.getrect())
g.clear(0)
g.select([])
g.putcells(g.transform(tocells(r, 0, 0), x, y))
g.update()
cells = np.full((width, height, period), -1)
free = np.ones((width, height, period))
q = deque()
nextset = 0
def setcell(x, y, t, s, f):
if cells[x, y, t] == s:
return 1
if cells[x, y, t] != -1:
return 0
cells[x, y, t] = s
free[x, y, t] = f
q.append([x, y, t, s, f])
return 1
def consistify(x, y, t):
t %= period
prevcell = cells[x, y, t-1]
nextprevcell = evolve(cells[:, :, t-1], 1)[x, y]
if nextprevcell != cells[x, y, t]:
if prevcell == 1:
if cells[x, y, t] == 0:
return 0
cells[x, y, t] = 0
free[x, y, t] = 0
q.append([x, y, t, 1, 0])
if prevcell == 0:
if cells[x, y, t] == 1:
return 0
cells[x, y, t] = 1
free[x, y, t] = 0
q.append([x, y, t, 0, 0])
if nextprevcell == 0 and cells[x, y, t] == 0 and setcell(x, y, t-1, 0, 0) != 1:
return 0
if nextprevcell == 1 and cells[x, y, t] == 1 and setcell(x, y, t-1, 1, 0) != 1:
return 0
s = -1
if nextprevcell == 1 and cells[x, y, t] == -1:
s = 0
if nextprevcell == 0 and cells[x, y, t] == -1:
s = 1
if s == -1:
return 1
for i, j in neighbor:
if 0 < x+i < width and 0 < y+j < height:
if cells[x+i, y+j, t-1] == -1 and setcell(x+i, y+j, t-1, s, 0) != 1:
return 0
return 1
def consistify10(x, y, t):
g.show(str(q))
display(cells[:, :, 0])
if consistify(x, y, t) != 1:
return 0
for i, j in neighbor:
if 0 < x+i < width and 0 < y+j < height:
if consistify(x+i, y+j, t+1) != 1:
return 0
return 1
def examinenext():
if nextset == len(q):
return 2
cell = q.popleft()
return consistify10(cell[0], cell[1], cell[2])
def proceed(x, y, t, s, f):
if setcell(x, y, t, s, f) != 1:
return 0
while True:
status = examinenext()
if status == 0:
return 0
if status == 2:
return 1
def backup():
while len(q) != 0:
cell = q.popleft()
if cell[4] == 0:
cells[cell[0], cell[1], cell[2]] = -1
free[cell[0], cell[1], cell[2]] = 1
else:
nextset = len(q)
return cell
nextset = 0
return "NULL"
def go(x, y, t, s, f):
while True:
status = proceed(x, y, t, s, f)
if status == 1:
return 1
cell = backup()
if cell == "NULL":
return 0
f = 0
s = 1 - cells[x, y, t]
cells[x, y, t] = -1
def getunknown():
if len(np.argwhere(cells == -1)) != 0:
return np.argwhere(cells == -1)[-1].tolist()
return "NULL"
def search():
cell = getunknown()
if cell == "NULL":
cell = backup()
if cell == "NULL":
return 0
f = 0
s = 1 - cell[3]
cells[cell[0], cell[1], cell[2]] = -1
else:
f = 1
s = 1
while True:
if go(cell[0], cell[1], cell[2], f, s) != 1:
return 0
cell = getunknown()
if cell == "NULL":
return 1
f = 1
s = 1
search()