Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- import copy
- from pixoo import Pixoo, SimulatorConfig
- pixoo_ip = "192.168.0.116"
- grid_size = 64
- hist_length = 64*4*2+5
- idle_count = 40
- def randomize():
- return [ [ random.randint(0, 1) == 1 for x in range(0,grid_size) ] for y in range(0,grid_size) ]
- def loadstr(string):
- out = [ [ 0 for x in range(0,grid_size) ] for y in range(0,grid_size) ]
- lines=string.split("\n")
- for row in range(grid_size):
- for col in range(grid_size):
- if row >= len(lines) or col >= len(lines[row]):
- out[row][col] = 0
- else:
- #print(f'{row}, {col} :: {len(lines)} :: {lines[row][col]}')
- out[row][col] = lines[row][col] == "1"
- return out
- pixoo = Pixoo(pixoo_ip)
- #pixoo = Pixoo(pixoo_ip, simulated=True, simulation_config=SimulatorConfig(4))
- green = [ 0, 255, 0]
- red = [ 255, 0, 0 ]
- black = [ 0 , 0, 0]
- def draw_grid(grid, color=green, factor = 1.0, push = True):
- for col in range(0, len(grid)):
- for row in range(0, len(grid[col])):
- if grid[row][col]:
- pixoo.draw_pixel([col, row], [int(x * factor) for x in color])
- else:
- pixoo.draw_pixel([col, row], black)
- if push: pixoo.push()
- def step(current_grid, wrap = False):
- """
- Any live cell with two or three live neighbours survives.
- Any dead cell with three live neighbours becomes a live cell.
- All other live cells die in the next generation. Similarly, all other dead cells stay dead.
- """
- new_grid = [ [0 for x in range(0, grid_size) ] for y in range(0, grid_size) ]
- for col in range(0, len(current_grid)):
- for row in range(0, len(current_grid[col])):
- neighbor_count = 0
- for dr in [-1, 0, 1]:
- for dc in [-1, 0, 1]:
- if wrap:
- row_check = (( dr + row) + 64 ) % 64
- col_check = (( dc + col) + 64 ) % 64
- if (dr != 0) or (dc != 0):
- if current_grid[row_check][col_check]:
- neighbor_count = neighbor_count + 1
- else:
- if 0 <= (col + dc) < len(current_grid):
- if 0 <= (row + dr) < len(current_grid):
- if (dr != 0) or (dc != 0):
- if current_grid[row+dr][col+dc]:
- neighbor_count = neighbor_count + 1
- if current_grid[row][col] and (neighbor_count == 2 or neighbor_count == 3):
- new_grid[row][col] = True
- elif not current_grid[row][col] and neighbor_count == 3:
- new_grid[row][col] = True
- else:
- new_grid[row][col] = False
- return new_grid
- max_grid = loadstr("""1.111..11..11...11.111.1.111...1111.11111....111....1.1.1.1.1.11
- .1.11111......1....1.1.1.111..1...11..111...111.....1..111111111
- 1......1.1....11.1.111111.1.1....1.1..11..1.1111....1.1..1.1111.
- .......1....111.1.1.11..1..11111.1.111..1......1..11.1......111.
- ..111.11.1..11....1...1.1..111..11...11111...1111...11..1.1.1...
- ..11..1..11..11.1....111.......11.1...1.1..11.....1.1..1..11....
- 1111.1.1..11111.111111.1.1.111111...11.111....11..11....1...1.11
- 1111...1.11.111.1.1..1111111.1.1111.111..1...1111....1.11.1.111.
- .1.1.1111..11.1.111..11.1.1...1111....11.1...1.1....11.1.1..11.1
- 11...111.11......11...1.11.11.1.111..1.1...11..1.1.1.1111.1.1111
- ..1111.1.1.1.1111.1.1.1.1...11....1....1.11....1.1.1...1.1.1....
- 111.1.1111..1..1..11.11.1....1....1.1.1.11.11111111..1.11.....1.
- 1.1..1..11.11111.11111....1.1..1.1.1111.1..11.....11.1111.1.11.1
- 1..1.111.111..111..1.11.1.1...1..1.1111..11...1...1.111.....11.1
- ....11.1..11..1.111.111..11...11...11.1..1.1...11111...111.....1
- ...1.1..1.11.1....1..1111....11.11....1..1111..11....111...1....
- 1...1..1111...1...111111..1..1...11......1..111..1.11.1...1.1111
- .1..1...111..1.1.111...1..111.11.1........1..111..1111.11111..1.
- 111.1......1....111..1.111.11...111111.1.1..1.1.1.1.1...1111.1..
- 1.........111.1..1...1.11....111.1.1..1..1..1.1.1.111..1..1..111
- ..111.1..1..1.1.1.11...1111111....1.111..1..1111111.111.11.111.1
- 11..1..1.1.11.1.....111..1.1.11....1.11..11111..11.11.111....1.1
- .1....11.11111.11.111...11.1..11.1...1.111111.1....111...1....11
- .1...11.111.1...1.1..1....1.11...11.......1.1..111.1...1..11..1.
- ..111.1..111.1.1..11.11.....1..1.1111.1...1...11..11.11..1.1..1.
- 1.111...1.1.11.11.1...1.11..11...1.1.111..111...1...111..11.1..1
- ..11.111..1.111..1.111.11.1..11111.1..1.1...11.1.11.1.11..111..1
- .111..1..11..1.11...111...11..1.11...11....111.1.1..1.1..1.1.11.
- 1...1.1111.1111..111..1..111.1.1..111..1....11...1.11..11..11..1
- ..111..11...1.1111.11.1111.....1111.11111.111.1..1.1111.1111.1..
- .1..1.11.1.111.11..11111..111.1...11..1..1111....1111..11..11...
- 1.1.1.....1..1.11111.111..11111111.1.....1..11......1.1..1.11111
- ...1...1.111.1.11..1..1111.1..1.111........1..1.1.1.1...1.1..1..
- ..1..1..11...1..11...11.111111.111.1..11.111.1...11.....1.1..1.1
- 1111..1.....1.11.1.1..1..1......111......1...1.11.11.111..1..11.
- 1111.1..1.....11.1....111.1.1.1.11..1111.1..111..1..1.1...1.....
- ....11..1..1.11.111.11....111111..1111....1.111.1..1...1.11.11.1
- .1...11..1.1....11...1.11.1..111.1..1.1.1.1.1.111..1.1111.1.11.1
- 11...1.111.1...11.11.11.1.1.11.....1...1..11...1...1...11.11111.
- ..1..11..1111.1..1..1...1....1..1111....1111.1..1.11.111.111.11.
- .1.....1...1111.1.111....1111.1.1.111.....1...11..1.111..11..11.
- 11..11..11.1.11.1111.....1.111.1..11....1.11.11111.1.111.1...1.1
- .1.1.111.111.1.1...1111.1..11.111..1111111111.1.1..111.111..1.11
- 1.1..1111.1..1111...11111.111.111111..111.111.111...11...1.1...1
- ....1.1.111.11..111.1111.1..11...1111..111111.1..1..111..1.1....
- 11111....1.11111..111.11..1..1.1.1..11...1.1...1.1111.1.1.111.1.
- ..11111111.1....1.1.....111...11...1.1..1.1.111..1.111..11..1.11
- 1.1...1..11.1....1.1.1.11..1.1.1.11..1....111..1.1.1.1..111.1..1
- 1...11..1111.1.1..1....1.111..11..1...1.....1.1.1.1..1.11..1.1..
- 11.11111.11...111...1111.1.1.1...1.1..1..1.1.11...11.1..1.11.111
- .1..1...1.11.111.1..1....11.1.111111..1..1111..11111111.1.11.111
- .111..1.1..11.11.1...1.1..11.1.11..1111.1.11.111111...11..1.1..1
- 11111.11..111..11...11.1..1..1111..1.1......11.1.1....1.1.1.1.1.
- 11.11.111.1.11...1.1.1.11.1..11..1.111...1...1.1.1...1..1.111.11
- .....1.1.1111..11..1..111.1.1..1111...1...1.11.1.1.111.1...1.111
- 11.11...11...1.11....1..1.11....1111..1...11.1.1..11....11...1..
- 1.11.11.1111...11..11.1..11.1...11.1...111111.1..1...1..1111.1..
- 11.11.11.1...1..1111111....1.11111.1..1....1...1.....1.1.1..111.
- 1111.11.1.11..........1.1.....11.....1.1........11.111.11.111..1
- ..111..1...111.11....11111..11..1..1111...111..1.1..111111..111.
- ......11....1.11.111.1..11..11..1..111..11.11.11111...1111..1...
- 1..1..11..111.1..1.1...1.111....11...1.1..1.11..1.1.11..1..111.1
- 11.1.1.1.1.111111..1.1.1.11.1.1..111..1.11..111..11..111..1111..
- 1...1....111111.1.1..1.11....1111.1...1.11.111.111.1.111......11""")
- def compare(a, b):
- if len(a) != len(b): return False
- for row in range(len(a)):
- if len(a[row]) != len(b[row]): return False
- for col in range(len(a[row])):
- if a[row][col] != b[row][col]: return False
- return True
- def scoreboard(max, current, color):
- if current > max:
- newmax = current
- record = "NEW RECORD!"
- else:
- newmax = max
- record = ""
- max_str = str(newmax)
- cur_str = str(current)
- c = f"LIFESPAN: %{len(max_str)}s" % cur_str
- m = f"MAX : %{len(max_str)}s" % max_str
- c_len = len(c) *4
- m_len = len(m) *4
- pixoo.draw_text_at_location_rgb(record, 32 - int(len(record) * 4 / 2), 10, 0, 0, color)
- pixoo.draw_text_at_location_rgb(c, 32 - int(c_len / 2), 26, color, 0, 0)
- pixoo.draw_text_at_location_rgb(m, 32 - int(m_len / 2), 33, color, 0, 0)
- pixoo.push()
- return newmax
- def dumpgrid(grid):
- rows = []
- for row in grid:
- foo = [ "1" if x else "." for x in row ]
- rows.append("".join(foo))
- print("\n".join(rows))
- if __name__ == "__main__":
- history = [ [[]] for x in range(hist_length) ]
- max = 0
- init_grid = randomize()
- init_grid = max_grid
- current_grid = copy.deepcopy(init_grid)
- count = 0
- while(True):
- draw_grid(current_grid)
- current_grid = step(current_grid, True)
- match = False
- for grid in history:
- if compare(current_grid, grid):
- match = True
- break
- if match:
- for i in range(idle_count):
- current_grid = step(current_grid)
- draw_grid(current_grid, green, 1 - (float(i) / idle_count), False)
- scoreboard(max, count, int(255 * float(i) / idle_count))
- if count > max :
- max = count
- print(f"{count}:")
- dumpgrid(init_grid)
- time.sleep(10)
- current_grid = randomize()
- init_grid = copy.deepcopy(current_grid)
- count = 0
- else:
- history[count % hist_length] = copy.deepcopy(current_grid)
- count = count + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement