def matches(glider, x, y): for i in range(0, len(glider.clist), 2): if g.getcell(glider.clist[i]+x, glider.clist[i+1]+y) == 0: return 0 bkg = glider.background for i in range(0, len(bkg), 2): if g.getcell(bkg[i]+x, bkg[i+1]+y) == 1: return 0 return 1
def checkneighbor(x, y): # first check if x,y is outside bounded grid if g.getwidth() > 0 and (x < gridl or x > gridr): return if g.getheight() > 0 and (y < gridt or y > gridb): return if g.getcell(x, y) == 0: return # no need for next test because we kill cell after adding it to ncells # if (x, y) in ncells: return False ncells.append( (x, y, g.getcell(x,y)) ) g.setcell(x, y, 0)
def rules(x, y, u): i = neighborcount(x, y) if i == 3 and g.getcell(x, y) == 0: u[x][y] = 1 if i > 3 and g.getcell(x, y) == 1: u[x][y] = 0 if i < 2 and g.getcell(x, y) == 1: u[x][y] = 0 if i == (2 | 3) and g.getcell(x, y) == 1: u[x][y] = g.getcell(x, y) return u
def scanXY(x, y, islast, lines): res = "" for i in range(10): for j in range(10): if g.getcell(x + i, y + j) != 0: if not islast: add_rule(lines, XY_toline(x + i, y + j)) elif g.getcell(x + i + 10, y + j) != 0: add_rule(lines, XY_toline(x + i, y + j)) return res
def XY_toline(x, y): global variables ids = {} for i in variables: ids[i] = 0 res = "" for i, j, in idxs: res += cell_to_string(g.getcell(x + i, y + j), ids) + "," res += cell_to_string(g.getcell(x + 10, y), ids, g.getcell(x, y)) + "\n" return res
def findlivecell(x, y): if g.getcell(x, y) > 0: return [x, y] # spiral outwards from x,y looking for a nearby live cell; # the smaller the scale the smaller the area searched maxd = 10 mag = g.getmag() if mag > 0: # mag can be 1..5 (ie. scales 1:2 to 1:32) maxd = 2 * (6 - mag) # 10, 8, 6, 4, 2 d = 1 while d <= maxd: x -= 1 y -= 1 for i in range(2*d): x += 1 # move east if getstate(x, y) > 0: return [x, y] for i in range(2*d): y += 1 # move south if getstate(x, y) > 0: return [x, y] for i in range(2*d): x -= 1 # move west if getstate(x, y) > 0: return [x, y] for i in range(2*d): y -= 1 # move north if getstate(x, y) > 0: return [x, y] d += 1 return [] # failed to find a live cell
def bruteForceSearch(searchPatt): cols = len(searchPatt[0]) rows = len(searchPatt) matches = list() wd = golly.getwidth() ht = golly.getheight() [startWd, endWd] = getIndices(wd) [startHt, endHt] = getIndices(ht) #formatStr = 'Width: {0} Height: {1}\nStart Pos: ({2},{3})\nEnd Pos:{4},{5}\np,q = {6} {7} ' #golly.note(formatStr.format(wd,ht,startWd,startHt,endWd,endHt,p,q)) # Only search the candidate matrix cells which can contain # the matrix we're searching for candidates = [(x,y) for x in range(startWd,endWd-cols+2) for y in range(startHt,endHt-rows+2)] for (iCol, iRow) in candidates: jCol = 0 jRow = 0 while(golly.getcell(iCol + jCol,iRow + jRow) == searchPatt[jRow][jCol]): if (jCol == cols-1) and (jRow == rows-1): print ' Match = ({0},{1})'.format(iCol,iRow) matches.append((iCol,iRow)) break jCol = jCol + 1 if (jCol > cols - 1): jCol = 0 jRow = jRow + 1 if (jRow > rows - 1): print ' Break = ({0},{1})'.format(iCol,iRow) break ## End of search space, and no match found return [matches, candidates]
def outputmatch(outputcells): outputmatchflag = 1 for outcell in range(0, len(outputcells), 3): if g.getcell(outputcells[outcell], outputcells[outcell + 1]) != 1: outputmatchflag = 0 break return outputmatchflag
def count_bits(row, col, value): x = col total = 0 while g.getcell(x, row) == value: total += 1 x += 1 return total
def create_smaller_icons(big, small): # scale down the big x big bitmaps into small x small bitmaps # using a simple sampling algorithm global iconinfo15, iconinfo31 if big == 15: numicons = iconinfo15[1] // 15 ybig = 32 else: # big = 31 numicons = iconinfo31[1] // 31 ybig = 0 if small == 7: y = 48 else: # small = 15 y = 32 sample = big // small offset = sample // 2 for i in range(numicons): x = i * 32 for row in range(small): for col in range(small): state = g.getcell(x + offset + col * sample, ybig + offset + row * sample) if state > 0: g.setcell(x + col, y + row, state)
def create31x31icons(): # scale up the 15x15 bitmaps into 31x31 bitmaps using a simple # algorithm that conserves any vertical or horizontal symmetry global iconinfo15 width = 15 middle = 7 # middle row or column in 15x15 icon height = iconinfo15[1] numicons = height // width for i in range(numicons): x = i * 32 y = 32 for row in range(width): for col in range(width): state = g.getcell(x + col, y + row) if state > 0: if row == middle and col == middle: # expand middle cell into 9 cells xx = i * 32 + 15 yy = 15 g.setcell(xx, yy, state) g.setcell(xx, yy + 1, state) g.setcell(xx, yy - 1, state) g.setcell(xx + 1, yy, state) g.setcell(xx - 1, yy, state) g.setcell(xx + 1, yy + 1, state) g.setcell(xx + 1, yy - 1, state) g.setcell(xx - 1, yy + 1, state) g.setcell(xx - 1, yy - 1, state) elif row == middle: # expand cell in middle row into 6 cells xx = i * 32 + col * 2 yy = row * 2 if col > middle: xx += 1 g.setcell(xx, yy, state) g.setcell(xx, yy + 1, state) g.setcell(xx + 1, yy, state) g.setcell(xx + 1, yy + 1, state) g.setcell(xx, yy + 2, state) g.setcell(xx + 1, yy + 2, state) elif col == middle: # expand cell in middle column into 6 cells xx = i * 32 + col * 2 yy = row * 2 if row > middle: yy += 1 g.setcell(xx, yy, state) g.setcell(xx, yy + 1, state) g.setcell(xx + 1, yy, state) g.setcell(xx + 1, yy + 1, state) g.setcell(xx + 2, yy, state) g.setcell(xx + 2, yy + 1, state) else: # expand all other cells into 4 cells xx = i * 32 + col * 2 yy = row * 2 if col > middle: xx += 1 if row > middle: yy += 1 g.setcell(xx, yy, state) g.setcell(xx, yy + 1, state) g.setcell(xx + 1, yy, state) g.setcell(xx + 1, yy + 1, state)
def PrepareList(cells): g.new("") g.putcells(cells) cells = g.getcells(g.getrect()) g.new("") g.putcells(cells, -cells[0], -cells[1]) c = g.getcells(g.getrect()) result = [] for i in xrange(0, len(c), 2): x = c[i] y = c[i + 1] for dx in xrange(-1, 2): for dy in xrange(-1, 2): val = g.getcell(x + dx, y + dy) if (x + dx, y + dy, val) in result: continue result.append((x + dx, y + dy, val)) random.shuffle(result) return result
def recipe(): res = "" i = 0 prev = 0 while True: i += 1 y = int(128 * i) + 1 cells = g.getcells([-1500+y, y, 2 * 1500, 3]) if len(cells) == 0: break x = cells[0] y = cells[1] cur = x - y if g.getcell(x + 2, y) == 0: res += "O" + str(cur) + " " else: res += "E" + str(cur - 1) + " " prev = cur return res
def canonise_orientation(length, breadth, ox, oy, a, b, c, d): representation = "" chars = "0123456789abcdefghijklmnopqrstuvwxyz" for v in xrange(int((breadth-1)/5)+1): zeroes = 0 if (v != 0): representation += "z" for u in xrange(length): baudot = 0 for w in xrange(5): x = ox + a*u + b*(5*v + w) y = oy + c*u + d*(5*v + w) baudot = (baudot >> 1) + 16*g.getcell(x, y) if (baudot == 0): zeroes += 1 else: if (zeroes > 0): if (zeroes == 1): representation += "0" elif (zeroes == 2): representation += "w" elif (zeroes == 3): representation += "x" else: representation += "y" representation += chars[zeroes - 4] zeroes = 0 representation += chars[baudot] return representation
def neighborcount(x, y): c = 0 j = x -1 while j <= x + 1: k = y - 1 while k <= y +1: if k == y and j == x: k +=1 break if g.getcell(j, k) == 1: c +=1 k +=1 j +=1 # if g.getcell(x - 1, y) == 1: # c += 1 # if g.getcell(x - 1, y - 1) == 1: # c += 1 # if g.getcell(x, y - 1) == 1: # c += 1 # if g.getcell(x - 1, y + 1) == 1: # c += 1 # if g.getcell(x, y + 1) == 1: # c += 1 # if g.getcell(x + 1, y + 1) == 1: # c += 1 # if g.getcell(x + 1, y - 1) == 1: # c += 1 # if g.getcell(x + 1, y) == 1: # c += 1 return c
def create31x31icons(): # scale up the 15x15 bitmaps into 31x31 bitmaps using a simple # algorithm that conserves any vertical or horizontal symmetry global iconinfo15 width = 15 middle = 7 # middle row or column in 15x15 icon height = iconinfo15[1] numicons = height/width for i in xrange(numicons): x = i*32 y = 32 for row in xrange(width): for col in xrange(width): state = g.getcell(x+col, y+row) if state > 0: if row == middle and col == middle: # expand middle cell into 9 cells xx = i*32+15 yy = 15 g.setcell(xx, yy, state) g.setcell(xx, yy+1, state) g.setcell(xx, yy-1, state) g.setcell(xx+1, yy, state) g.setcell(xx-1, yy, state) g.setcell(xx+1, yy+1, state) g.setcell(xx+1, yy-1, state) g.setcell(xx-1, yy+1, state) g.setcell(xx-1, yy-1, state) elif row == middle: # expand cell in middle row into 6 cells xx = i*32+col*2 yy = row*2 if col > middle: xx += 1 g.setcell(xx, yy, state) g.setcell(xx, yy+1, state) g.setcell(xx+1, yy, state) g.setcell(xx+1, yy+1, state) g.setcell(xx, yy+2, state) g.setcell(xx+1, yy+2, state) elif col == middle: # expand cell in middle column into 6 cells xx = i*32+col*2 yy = row*2 if row > middle: yy += 1 g.setcell(xx, yy, state) g.setcell(xx, yy+1, state) g.setcell(xx+1, yy, state) g.setcell(xx+1, yy+1, state) g.setcell(xx+2, yy, state) g.setcell(xx+2, yy+1, state) else: # expand all other cells into 4 cells xx = i*32+col*2 yy = row*2 if col > middle: xx += 1 if row > middle: yy += 1 g.setcell(xx, yy, state) g.setcell(xx, yy+1, state) g.setcell(xx+1, yy, state) g.setcell(xx+1, yy+1, state)
def create_smaller_icons(big, small): # scale down the big x big bitmaps into small x small bitmaps # using a simple sampling algorithm global iconinfo15, iconinfo31 if big == 15: numicons = iconinfo15[1] / 15 ybig = 32 else: # big = 31 numicons = iconinfo31[1] / 31 ybig = 0 if small == 7: y = 48 else: # small = 15 y = 32 sample = big / small offset = sample / 2 for i in xrange(numicons): x = i*32 for row in xrange(small): for col in xrange(small): state = g.getcell(x + offset + col*sample, ybig + offset + row*sample) if state > 0: g.setcell(x+col, y+row, state)
def sof_representation(length, breadth, ox, oy, a, b, c, d): sof = '' for v in range(breadth): value = 1 run = 0 blank = True if (v != 0): sof += '-' for u in range(length + 1): x = ox + a * u + b * v y = oy + c * u + d * v if (g.getcell(x, y) == value): # Continue the run run += 1 else: # Encode the run, unless a run of zeros reaches the boundary if (u < length or value == 1): while (run > 78): run -= 78 sof += '~0' sof += chr(run + 48) # ord('0') = 48 run = 1 value = 1 - value # Toggle value if (value == 1): blank = False if (blank == True): # Remove unnecessary '0' sof = sof[:-2] + '-' sof += '.' return sof
def PrepareList(cells): g.new("") g.putcells(cells) cells = g.getcells(g.getrect()) g.new("") g.putcells(cells, -cells[0], -cells[1]) c = g.getcells(g.getrect()) result = [] for i in xrange(0, len(c), 2): x = c[i] y = c[i + 1] for dx in xrange(-1,2): for dy in xrange(-1, 2): val = g.getcell(x + dx, y + dy) if (x + dx, y + dy, val) in result: continue result.append((x + dx, y + dy, val)) random.shuffle(result) return result
def findlivecell(x, y): if g.getcell(x, y) > 0: return [x, y] # spiral outwards from x,y looking for a nearby live cell; # the smaller the scale the smaller the area searched maxd = 10 mag = g.getmag() if mag > 0: # mag can be 1..4 (ie. scales 1:2 to 1:16) maxd = 2 * (5 - mag) # 8, 6, 4, 2 d = 1 while d <= maxd: x -= 1 y -= 1 for i in xrange(2*d): x += 1 # move east if getstate(x, y) > 0: return [x, y] for i in xrange(2*d): y += 1 # move south if getstate(x, y) > 0: return [x, y] for i in xrange(2*d): x -= 1 # move west if getstate(x, y) > 0: return [x, y] for i in xrange(2*d): y -= 1 # move north if getstate(x, y) > 0: return [x, y] d += 1 return [] # failed to find a live cell
def SnakeReader(let): g.new("") g.setrule("LifeHistory") cl = let2cells[let][0] g.putcells(cl) emptyline = True dir = 1 rect = g.getrect() y0 = 0 h = rect[1] + rect[3] + 2 l = rect[0] + rect[2] + 3 x0 = rect[0] - 1 y = y0 x = x0 code = "" while True: y += dir if y < y0: if emptyline == True: code += "Y+;" break dir = 1 y += dir code += "Y+;" x += 1 emptyline = True elif y >= y0 + h: dir = -1 y += dir code += "Y+;" x += 1 emptyline = True else: if dir == -1: code += "X-;" else: code += "X+;" if g.getcell(x, y) != 0: code += "SET;" emptyline = False if x >= x0 + l: break for i in range(h): code = code.replace("X+;Y+;X-;", "Y+;") code = code.replace("X-;Y+;X+;", "Y+;") return code
def randfill_mash(rectcoords, amt, statemap): newstate = g.getoption("drawingstate") # g.note('%s'%statemap) for i in range(rectcoords[0], rectcoords[0] + rectcoords[2]): for j in range(rectcoords[1], rectcoords[1] + rectcoords[3]): if (100 * random.random() < amt): try: g.show('processing %i,%i' % (i, j)) g.setcell(i, j, statemap[g.getcell(i, j)]) except: dict_lc = [1] * g.numstates() dict_lc[0] = 0 dict_lc[1:3] = [2, 2] g.setcell(i, j, dict_lc[g.getcell(i, j)]) else: # g.setcell(i, j, 0) continue
def scanX(y, lines, stage, vars): if stage == 0: if g.getcell(0, y) == 0: if len(g.getcells([0, y, 10, 1])) != 0: stage = 2 else: return 0 else: stage = 1 if stage == 1: if g.getcell(0, y) == 0: stage = 2 else: vars[g.getcell(0, y)] = [g.getcell(10, y)] dx = 20 while g.getcell(dx, y) != 0: vars[g.getcell(0, y)].append(g.getcell(dx, y)) dx += 10 return stage stage = 2 dx = 0 while len(g.getcells([dx, y, 10, 10])) != 0: dx += 10 for i in range(0, dx, 10): scanXY(i, y, i == dx - 10, lines) return stage
def drawline(x1, y1, x2, y2): # draw a line of cells from x1,y1 to x2,y2 using Bresenham's algorithm; # we also return the old cells in the line so we can erase line later oldcells = [] # note that x1,y1 has already been drawn # oldcells.append( (x1, y1, g.getcell(x1, y1)) ) # g.setcell(x1, y1, drawstate) if x1 == x2 and y1 == y2: g.update() return oldcells dx = x2 - x1 ax = abs(dx) * 2 sx = 1 if dx < 0: sx = -1 dy = y2 - y1 ay = abs(dy) * 2 sy = 1 if dy < 0: sy = -1 if ax > ay: d = ay - (ax / 2) while x1 != x2: oldcells.append( (x1, y1, g.getcell(x1, y1)) ) g.setcell(x1, y1, drawstate) if d >= 0: y1 += sy d -= ax x1 += sx d += ay else: d = ax - (ay / 2) while y1 != y2: oldcells.append( (x1, y1, g.getcell(x1, y1)) ) g.setcell(x1, y1, drawstate) if d >= 0: x1 += sx d -= ay y1 += sy d += ax oldcells.append( (x2, y2, g.getcell(x2, y2)) ) g.setcell(x2, y2, drawstate) g.update() return oldcells
def randfill_mash(rectcoords, amt): newstate = g.getoption("drawingstate") for i in range(rectcoords[0], rectcoords[0] + rectcoords[2]): for j in range(rectcoords[1], rectcoords[1] + rectcoords[3]): if (100 * random.random() < amt): g.setcell(i, j, dict_fill[rule][g.getcell(i, j)]) else: # g.setcell(i, j, 0) continue
def censusgen(): neighbor_list = [] bbox = g.getrect() for y in range(bbox[1], bbox[1] + bbox[3]): for x in range(bbox[0], bbox[0] + bbox[2]): numneighs = sum([ g.getcell(x, y - 1), g.getcell(x + 1, y - 1), g.getcell(x + 1, y), g.getcell(x + 1, y + 1), g.getcell(x, y + 1), g.getcell(x - 1, y + 1), g.getcell(x - 1, y), g.getcell(x - 1, y - 1) ]) neighbor_census[9 * g.getcell(x, y) + numneighs][1] += 1
def matches(pat, x, y): for i in range(0, len(pat), 2): if g.getcell(pat[i] + x, pat[i + 1] + y) % 2 == 0: return 0 # bkg = getbackground(pat) # for i in range(0, len(bkg), 2): # if g.getcell(bkg[i]+x, bkg[i+1]+y) % 2 == 1: # return 0 return 1
def add_shooter(cost, inserter_cells, left_g, right_g=[], flip=True): global shoot_defs g.new('') g.putcells(inserter_cells) g.putcells(left_g) g.putcells(right_g) g.run(2048) reaction_cells = g.getcells([-150,-150,300,300]) g.run(512) if not all(g.getcell(x, y) for x, y in gld_pairs): g.fit() g.update() assert(False) stable_cells = set(zip(reaction_cells[::2], reaction_cells[1::2])) for _ in range(4): g.run(1) for i in range(0, len(reaction_cells), 2): tup = reaction_cells[i], reaction_cells[i+1] if tup in stable_cells and not g.getcell(*tup): stable_cells.remove(tup) stable_cells2 = [] for x, y in stable_cells: stable_cells2.append(x) stable_cells2.append(y) stable_cells = stable_cells2 tup = (cost, inserter_cells, left_g, right_g, reaction_cells, stable_cells) shoot_defs.append(tup) if flip: shoot_defs.append((cost, f(inserter_cells), f(right_g), f(left_g), f(reaction_cells), f(stable_cells)))
def test(pat, gen, loc, x, y): global results if not all(g.getcell(x + loc, y) for x, y in on_cells): return if any(g.getcell(x + loc, y) for x, y in off_cells): return begin = start_cells + g.evolve(g.transform(pat, -x, -y), gen) if finalpop >= 0 and len(g.evolve(begin, 100)) != 2 * finalpop: return results += 1 g.setlayer(results_layer) g.putcells(g.evolve(pat, gen % 8), 50 * results - x, -y) g.putcells(begin, 50 * results, 50) g.putcells(g.evolve(begin, delay), 50 * results, 100) g.setlayer(work_layer)
def is_there(x, y, gl): l = len(gl) for i in range(1, l, 2): xg = gl[i - 1] yg = gl[i] if g.getcell(x + xg, y + yg) == 0: return False for i in range(1, l, 2): xg = gl[i - 1] yg = gl[i] g.setcell(x + xg, y + yg, 0) return True
def FindConnected(listXY): result = copy(listXY) for xy in listXY: x = xy[0] y = xy[1] for i in xrange(-1, 2): for j in xrange(-1, 2): if g.getcell(x + i, y + j) > 0 and len([i for t in listXY if (t[0] == x + i and t[1] == y + j)]) == 0: if len([i for t in result if (t[0] == x + i and t[1] == y + j)]) == 0: result.append([x + i, y + j]) return result
def add(d, idx): new_str = "" for i in range(10): for j in range(10): if g.getcell(i + d, j) == 1: dx = i dy = j break for i in range(10): for j in range(10): if g.getcell(i + d, j) == 1: new_str += "locators[%d].locations.push_back(locatorbit(%d,%d,false));\n" % (idx, i - dx, j - dy) for i in range(10): for j in range(10): if g.getcell(i + d, j) == 2: new_str += "locators[%d].locations.push_back(locatorbit(%d,%d,true));\n" % (idx, i - dx, j - dy) return new_str
def FindConnected(listXY): result = copy.copy(listXY) for xy in listXY: x = xy[0] y = xy[1] for i in xrange(-1, 2): for j in xrange(-1, 2): if g.getcell(x + i, y + j) > 0 and len([i for t in listXY if (t[0] == x + i and t[1] == y + j)]) == 0: if len([i for t in result if (t[0] == x + i and t[1] == y + j)]) == 0: result.append([x + i, y + j]) return result
def CanApplyRecipe(self, recipe, expected): g.new("") g.setstep(3) g.putcells(blck) g.putcells(self.init) for r in self.recipe: g.putcells(gld, 80, 80 + r) g.step() g.select([self.block0[0], self.block0[1], 2, 2]) g.clear(0) cells = g.getcells(g.getrect()) g.putcells(blck, self.block0[0], self.block0[1]) delta = self.block0[1] - self.block0[0] for r in recipe: g.putcells(gld, 80, 80 + r + delta) g.step() for i in xrange(0, len(cells), 2): x = cells[i] y = cells[i + 1] if g.getcell(x, y) == 0: return False for i in xrange(0, len(expected), 2): x = expected[i] + self.block0[0] y = expected[i + 1] + self.block0[1] if g.getcell(x, y) == 0: return False return True
def UpdateState(self, newPat, dx, dy): self.under = [] self.curPat = newPat self.dx = dx self.dy = dy for i in xrange(0, len(self.curPat), 2): if g.getcell(self.curPat[i] + self.dx, self.curPat[i + 1] + self.dy) == 1: self.under.append(self.curPat[i] + self.dx) self.under.append(self.curPat[i + 1] + self.dy) g.setcell(self.curPat[i] + self.dx, self.curPat[i + 1] + self.dy, 1) g.update()
def encode_row(row): result = '' # find te rightmost 1 bit in this row far_right = x + width - 1 while g.getcell(far_right, row) == 0: far_right -= 1 if far_right < x: g.exit('Empty rows forbidden: ' + str(row)) col = x while col <= far_right: cell_value = g.getcell(col, row) num_consecutive = count_bits(row, col, cell_value) if cell_value == 1: # next cell 1? col += num_consecutive result += encode_consecutive_ones(num_consecutive) else: col += num_consecutive + 1 result += encode_consecutive_zeros(num_consecutive) result += '0' * (chunk_size // 2 + 3) # end of row return result
def IsObjectExists(objectArray, x, y): for obj, objName, t, p in objectArray: found = True for i in xrange(0, len(obj), 2): dx = obj[i] dy = obj[i + 1] if g.getcell(x + dx, y + dy) == 0: found = False break if found: return [obj, objName, x, y, t, p] return None
def SignalAt(self, x, y): for s in self.signals: found = True for i in xrange(0, len(s), 2): xs = s[i] ys = s[i + 1] if g.getcell(x + xs, y + ys) == 0: found = False break if found: return s return None
def getinp(s): ########################### temp = g.getcell(x, y) g.setcell(x, y, 5) g.show(s + "Ptr:" + str(ptr) + " x:" + str(x) + " y:" + str(y)) g.fit() g.update() g.setcell(x, y, temp) # return k = g.getevent() count = 0 while k == "": sleep(.01) k = g.getevent() count += 1 if k == "key q none": g.exit() return
def drawlines(): global oldline, firstcell started = False oldmouse = "" while True: event = g.getevent() if event.startswith("click"): # event is a string like "click 10 20 left altctrlshift" evt, x, y, butt, mods = event.split() oldmouse = x + ' ' + y if started: # draw permanent line from start pos to end pos endx = int(x) endy = int(y) drawline(startx, starty, endx, endy) # this is also the start of another line startx = endx starty = endy oldline = [] firstcell = [] else: # start first line startx = int(x) starty = int(y) firstcell = [ startx, starty, g.getcell(startx, starty) ] g.setcell(startx, starty, drawstate) g.update() started = True g.show("Click where to end this line (and start another line)...") else: # event might be "" or "key m none" if len(event) > 0: g.doevent(event) mousepos = g.getxy() if started and len(mousepos) == 0: # erase old line if mouse is not over grid if len(oldline) > 0: eraseline(oldline) oldline = [] g.update() elif started and len(mousepos) > 0 and mousepos != oldmouse: # mouse has moved, so erase old line (if any) and draw new line if len(oldline) > 0: eraseline(oldline) x, y = mousepos.split() oldline = drawline(startx, starty, int(x), int(y)) oldmouse = mousepos
def underneath(object): # return list of live cells underneath given object (a multi-state list) cells = [] objlen = len(object) if objlen % 3 == 1: objlen -= 1 # ignore padding int i = 0 while i < objlen: x = object[i] y = object[i+1] s = g.getcell(x, y) if s > 0: cells.append(x) cells.append(y) cells.append(s) i += 3 # append padding int if necessary if len(cells) > 0 and (len(cells) & 1) == 0: cells.append(0) return cells
def main(): # get selection and set to boundary bound = g.getselrect() if len( bound ) == 0: g.note( "No selection." ) g.exit() # get current Golly states left = bound[ 0 ] top = bound[ 1 ] width = bound[ 2 ] height = bound[ 3 ] for y in xrange( 0, height ): for x in xrange( 0, width ): state = g.getcell( left + x, top + y ) g.setcell( height - y - 600, x - 600, state )
def EdgeGlider(): for i in xrange(0, 4): rect = g.getrect() x0 = rect[0] + rect[2] - 3 y0 = rect[1] + rect[3] - 3 gldFound = True for j in xrange(0, len(gld), 2): if g.getcell(x0 + gld[j], y0 + gld[j + 1]) == 0: gldFound = False break if gldFound: return g.getcells([x0, y0, 3, 3]) g.run(1) return -1
def PerformDelete(glider, gunPeriod): initCells = g.getcells(g.getrect()) for j in xrange(0, len(glider), 2): if g.getcell(glider[j], glider[j + 1]) != 1: return False Erase(glider) rect = g.getrect() cells = g.getcells(rect) g.run(gunPeriod) if str(g.getcells(rect)) == str(cells): g.new("") g.putcells(cells) return True else: g.new("") g.putcells(initCells) return False
def FindObj(objData, idx): c = g.getcells(g.getrect()) result = [] for i in xrange(2, len(c), 3): #g.show(str(i) + "/" + str(len(c))) x = c[i - 2] y = c[i - 1] found = True for r in objData: dx, dy, v = r if g.getcell(x + dx, y + dy) != v: found = False break if found: result.append((x, y, idx)) return result
def getobject(x, y): object = [] ncells.append( (x, y, g.getcell(x,y)) ) g.setcell(x, y, 0) while len(ncells) > 0: # remove cell from end of ncells and append to object x, y, s = ncells.pop() object.append(x) object.append(y) object.append(s) # add any live neighbors to ncells checkneighbor(x , y+1) checkneighbor(x , y-1) checkneighbor(x+1, y ) checkneighbor(x-1, y ) checkneighbor(x+1, y+1) checkneighbor(x+1, y-1) checkneighbor(x-1, y+1) checkneighbor(x-1, y-1) # append padding int if necessary if len(object) > 0 and (len(object) & 1) == 0: object.append(0) g.putcells(object) return object
def Main(self): g.show("left click on a pattern to change, 'h' for help") gollyMode = False while True: event = g.getevent() if ("key" in event and "return" in event) or (gollyMode and " a " in event): gollyMode = not gollyMode if gollyMode: g.show("In golly mode") g.update() else: g.show("left click on a pattern, right click to finish") g.setrule("B3/S23") g.setalgo("HashLife") g.reset() g.update() continue if gollyMode: if " delete " in event: g.clear(0) if "click" in event and "ctrl" in event and g.getxy() != "": x, y = g.getxy().split() cell = g.getcell(int(x), int(y)) if cell >= 0 and cell <= 1: g.setcell(int(x), int(y), 1 - cell) g.update() if " c " in event and "ctrl" in event and g.getselrect() != []: g.copy() if " v " in event and "ctrl" in event and g.getxy() != "": x, y = g.getxy().split() g.paste(int(x), int(y), "or") if " space " in event: if "ctrl" in event: g.run(10) else: g.run(1) g.doevent(event) continue if "click" in event: if "left" in event: if self.ExistinCircuitHandler() == None: if self.SignalClickHandler(event) == None: g.show("left click on a pattern, h for help") elif "key" in event: if " space " in event: for i in xrange(0, 30): g.run(60) g.update() g.reset() g.update() if " a " in event: if g.getrule() == "Perrier": g.setrule("B3/S23") g.setalgo("HashLife") g.update() else: g.setrule("Perrier") for key in self.smarCells: x, y = key.split(":") g.setcell(int(x), int(y), self.smarCells[key] + 2) gollyMode = True g.show("In golly mode") g.update() if " s " in event: fname = os.path.join(g.getdir("data"), "MetadataManager.json") #self.Save(fname) if " h " in event: noteMessage = "Viewing and Selecting\n\n" noteMessage += "'left click' to chose gun or glider\n" noteMessage += "'a' to see in colors, a to go back \n" noteMessage += "'space' see ahead 1800 generations \n" noteMessage += "'enter' gollyMode, stays in the script \n" noteMessage += "\n Editing Gun \n\n" noteMessage += "'left click' to place\n" noteMessage += "'right click' to switch gun/orientation \n" noteMessage += "'delete' to delete the gun \n" noteMessage += "'left-right arrow' - one step adjustment\n" noteMessage += "\n In Golly Mode \n\n" noteMessage += "'delete' to clear selection\n" noteMessage += "'ctrl' + 'click' to draw \n" noteMessage += "'ctrl' + 'c' to copy selection \n" noteMessage += "'ctrl' + 'v' to paste in mouse location \n" noteMessage += "'space' + to run 1 generation \n" noteMessage += "'ctrl' +'space' to run 10 generations \n" g.note(noteMessage)
# write the cells that are in state 1 (can ignore the zeros) # (still plenty of room for optimisation here) tape = '11' for row in xrange(r.top, r.top + r.height): # if large selection then give some indication of progress newsecs = time() if newsecs - oldsecs >= 1.0: oldsecs = newsecs g.update() # also allow keyboard interaction g.dokey( g.getkey() ) for col in xrange(r.left, r.left + r.width): if g.getcell(col, row)==1: tape += extend*(4+col-r.left) + extend_left + extend*(r.top+r.height-row) tape += mark tape += retract*(r.top+r.height-row) + retract_left + retract*(4+col-r.left) elif g.getcell(col,row)!=0: g.exit('Cells in the selected area must be in states 0 or 1 only.') # finally we sheath and trigger and retract tape += extend_left + extend*4 + extend_right + extend tape += inject_sheath + '1'*50 + inject_trigger tape += retract*2 + retract_right + retract*4 + retract_left # now write the tape out x = r.left+r.width+10 y = r.top+r.height+10 g.setcell(x+1,y,2)
def checkneighbor(x, y, oldstate): # first check if x,y is outside fill limits if x < minx or x > maxx: return False if y < miny or y > maxy: return False return g.getcell(x, y) == oldstate
def floodfill(): newstate = g.getoption("drawingstate") oldstate = newstate # wait for user to click a cell g.show("Click the region you wish to fill... (hit escape to abort)") while oldstate == newstate: event = g.getevent() if event.startswith("click"): # event is a string like "click 10 20 left none" evt, xstr, ystr, butt, mods = event.split() x = int(xstr) y = int(ystr) if x < minx or x > maxx or y < miny or y > maxy: # click is outside pattern's bounding box in unbounded universe g.warn("Click within the pattern's bounding box\n"+ "otherwise the fill will be unbounded.") else: # note that user might have changed drawing state newstate = g.getoption("drawingstate") oldstate = g.getcell(x, y) if oldstate == newstate: g.warn("The clicked cell must have a different state\n"+ "to the current drawing state.") else: g.doevent(event) # tell Golly to handle all further keyboard/mouse events g.getevent(False) # do flood fill starting with clicked cell g.show("Filling clicked region... (hit escape to stop)") clist = [ (x,y) ] g.setcell(x, y, newstate) oldsecs = time() while len(clist) > 0: # remove cell from start of clist x, y = clist.pop(0) newsecs = time() if newsecs - oldsecs >= 0.5: # show changed pattern every half second oldsecs = newsecs g.update() # check if any orthogonal neighboring cells are in oldstate if checkneighbor( x, y-1, oldstate): g.setcell( x, y-1, newstate) clist.append( (x, y-1) ) if checkneighbor( x, y+1, oldstate): g.setcell( x, y+1, newstate) clist.append( (x, y+1) ) if checkneighbor( x+1, y, oldstate): g.setcell( x+1, y, newstate) clist.append( (x+1, y) ) if checkneighbor( x-1, y, oldstate): g.setcell( x-1, y, newstate) clist.append( (x-1, y) ) # diagonal neighbors are more complicated because we don't # want to cross a diagonal line of live cells if checkneighbor( x+1, y+1, oldstate) and (g.getcell(x, y+1) == 0 or g.getcell(x+1, y) == 0): g.setcell( x+1, y+1, newstate) clist.append( (x+1, y+1) ) if checkneighbor( x+1, y-1, oldstate) and (g.getcell(x, y-1) == 0 or g.getcell(x+1, y) == 0): g.setcell( x+1, y-1, newstate) clist.append( (x+1, y-1) ) if checkneighbor( x-1, y+1, oldstate) and (g.getcell(x, y+1) == 0 or g.getcell(x-1, y) == 0): g.setcell( x-1, y+1, newstate) clist.append( (x-1, y+1) ) if checkneighbor( x-1, y-1, oldstate) and (g.getcell(x, y-1) == 0 or g.getcell(x-1, y) == 0): g.setcell( x-1, y-1, newstate) clist.append( (x-1, y-1) )
soupfilepath = "/home/scorbie/Apps/ptbtest/random" nsoups = int(g.getstring("How many soups?", "1000")) soupsize = 10 # int(g.getstring('Soup size?', '10')) if g.getrule() == "LifeHistory": g.setrule("B3/S23") if g.numstates() > 2: g.exit("This script only works with two-state rules.") soups = [] x, y, w, h = r = [0, 0, soupsize, soupsize] cellchar = [".", "a"] while len(soups) < nsoups: g.new("Generating Soups") g.select(r) g.randfill(40) g.run(250) # To avoid 'Empty Pattern!' messages in status bar. if int(g.getpop()) == 0: continue if not q.testquiescence(60): g.reset() soupstr = "!".join( "".join(cellchar[g.getcell(i, j)] for j in xrange(0, soupsize + 1)) for i in xrange(0, soupsize + 1) ) soups.append(soupstr) g.show("Soup {}/{}".format(len(soups), nsoups)) with open(soupfilepath, "w") as soupfile: soupfile.write("\n".join(soups)) g.show("Soups successfully saved in {}.".format(soupfilepath))
# Invert all cell states in the current selection. # Author: Andrew Trevorrow ([email protected]), Jun 2006. # Updated to use exit command, Nov 2006. # Updated to use numstates command, Jun 2008. from glife import rect from time import time import golly as g r = rect( g.getselrect() ) if r.empty: g.exit("There is no selection.") oldsecs = time() maxstate = g.numstates() - 1 for row in xrange(r.top, r.top + r.height): # if large selection then give some indication of progress newsecs = time() if newsecs - oldsecs >= 1.0: oldsecs = newsecs g.update() # also allow keyboard interaction g.dokey( g.getkey() ) for col in xrange(r.left, r.left + r.width): g.setcell(col, row, maxstate - g.getcell(col, row)) if not r.visible(): g.fitsel()
# Change the entire pattern from emulated-Margolus states to N-state: from glife import rect import golly as g r = rect( g.getrect() ) if r.empty: g.exit("There is no pattern.") for row in xrange(r.top, r.top + r.height): for col in xrange(r.left, r.left + r.width): s = g.getcell(col,row) g.setcell(col, row, (s+s%2)/2-1)
def getstate(x, y): # first check if x,y is outside bounded grid if g.getwidth() > 0 and (x < gridl or x > gridr): return 0 if g.getheight() > 0 and (y < gridt or y > gridb): return 0 return g.getcell(x, y)
# first we write the program and marker tapes # then we encode the whole pattern onto the data tape # (program length is mostly unrelated to machine size, so it all works) write_program(program) # write the data tape data_tape = '' rect = g.getrect() # copy everything within the bounding rectangle width = rect[2] height = rect[3] x = rect[0] y = rect[1] for row in inclusive_range(y+height-1,y): far_right = x+width-1 while g.getcell(far_right,row)==0: far_right -= 1 if far_right<x: g.exit('Empty rows forbidden: '+str(row)) col = x while col <= far_right: if g.getcell(col,row)==1: data_tape+='11' # write 1 col += 1 else: if not g.getcells([col,row,n,1]): # next N cells zero? data_tape+='01' col += n else: data_tape+='10' # write 0 col += 1