Skip to content

Commit

Permalink
Load tapes from files; interface improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyzek committed Jul 21, 2015
1 parent a04d48b commit e4e978a
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 48 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TODO
* Tape Loading dialogue
* Improve loading dialogues
* accumulator, time fix, robust resetting
* Way of defining, examining machines with, e.g. symbol names longer than 1 character
* Generalise non-determinism
Expand Down
55 changes: 14 additions & 41 deletions demo.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,38 @@
import machines as m
import display as d
import time

"""
# A Unary Adder in One Dimension
unacontext = m.MachineContext(m.Plane(['0000100000']))
unacontext.create_machine("machines/arithmetic/unaryadder.tm")
unacontext.create_machine("machines/arithmetic/adders/unaryadder.tm")
# A Binary Adder in One Dimension
addcontext = m.MachineContext(m.Plane(['|10000111,101110101;']))
addcontext.create_machine("machines/arithmetic/compbinadd.tm")
addcontext.create_machine("machines/arithmetic/adders/compbinadd.tm")
# A Decimal Subtractor in Two Dimensions and with Many Working Parts
# This machine is more of a complex, working in concert.\nThe parent spawns workers whenever it needs them for subtasks.
sub = m.Plane([' 3241',
'203196'])
subcontext = m.MachineContext(sub)
subcontext.create_machine("machines/arithmetic/subtract-composite/subtract.tm", (6,0))
subcontext = m.MachineContext(m.parse_tape("machines/arithmetic/subtract-composite/subtract.tp"))
subcontext.create_machine("machines/arithmetic/subtract-composite/subtract.tm")
# Conway's Life, Turingified.
field = m.Plane(
["###########",
"> @@ #",
"# @@ #",
"# @ #",
"# @@ #",
"# @@ #",
"# @ #",
"# #",
"# #",
"# <",
"###########"])
concontext = m.MachineContext(field)
concontext.create_machine("machines/automata/conway/conway.tm", (1,1), None, 100000)
concontext = m.MachineContext(m.parse_tape("machines/automata/conway/conway.tp"))
concontext.create_machine("machines/automata/conway/conway.tm", lifespan=100000)
# A dumb tail-chaser.
course = m.Plane(
["┌ { ┐",
" ┌ ┐",
" ",
"' > u% └ ┐",
" *",
" * └ ┘",
"└ ) ^ { ┐",
"",
" *",
" , v i",
"",
" *",
" └ ( ┘"])
chacontext = m.MachineContext(course)
chacontext.create_machine("machines/snailchase.tm")
"""
chacontext = m.MachineContext(m.parse_tape("machines/misc/snailchase.tp"))
chacontext.create_machine("machines/misc/snailchase.tm")
# Langton's ant, in quadruplicate.
lancontext = m.MachineContext(m.Plane())
lancontext.create_machine("machines/automata/langton.tm", (0,0), "quad", 1000000)
"""
# Random Walk
rancontext = m.MachineContext(m.Plane())
rancontext.create_machine("machines/randwalk.tm")
"""
d.init(lancontext)
d.running = False
rancontext.create_machine("machines/misc/randwalk.tm")


d.init(rancontext)
d.running = False
d.run()
44 changes: 38 additions & 6 deletions display.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def init(initcontext):
uiicons["stopimg"] = pygame.transform.smoothscale(pygame.image.load(imgdir + "stopped.bmp"), (iconsize,iconsize))
uiicons["filerect"] = pygame.Rect(screen.get_size()[0] - iconsize*2, 0, iconsize, iconsize)
uiicons["fileimg"] = pygame.transform.smoothscale(pygame.image.load(imgdir + "file.png"), (iconsize,iconsize))
uiicons["tapeimg"] = pygame.transform.smoothscale(pygame.image.load(imgdir + "tape.png"), (iconsize,iconsize))
uiicons["tapeloadrect"] = pygame.Rect(screen.get_size()[0] - iconsize*3, 0, iconsize, iconsize)


def reload_sym_images():
for filename in os.listdir(symdir):
Expand Down Expand Up @@ -122,10 +125,10 @@ def display_tape():
symsize, symsize)
pygame.draw.rect(screen, machine.color, mrect, 2)

mrect = pygame.Rect(m*110 + 2, screen.get_size()[1]-(uisize+5), 100, uisize)
mrect = pygame.Rect(m*uisize*4 + 2, screen.get_size()[1]-(uisize+5), uisize*4 - 10, uisize)
pygame.draw.rect(screen, black, mrect, 0)
screen.blit(font.render(machine.path.split(os.sep)[-1], 1, white), (m*110 + 5, screen.get_size()[1]-uisize))
screen.blit(font.render(str(machine.pos) + " " + str(machine.state), 1, white), (m*110 + 5, screen.get_size()[1]-uisize//2))
screen.blit(font.render(machine.path.split(os.sep)[-1], 1, white), (m*uisize*4 + 5, screen.get_size()[1]-uisize))
screen.blit(font.render(str(machine.pos) + " " + str(machine.state), 1, white), (m*uisize*4 + 5, screen.get_size()[1]-uisize//2))
pygame.draw.rect(screen, machine.color, mrect, 2)

m += 1
Expand All @@ -136,17 +139,39 @@ def display_UI():
screen.blit(font.render("Elapsed ticks: " + str(elapsed), 1, white), (0, fontsize))
screen.blit(uiicons["runimg"] if running else uiicons["stopimg"], uiicons["runrect"])
screen.blit(uiicons["fileimg"], uiicons["filerect"])
screen.blit(uiicons["tapeimg"], uiicons["tapeloadrect"])

def load_tape_dialog():
global running
try:
running = False
options = {}
options['title'] = 'Load Tape'
options['defaultextension'] = '.tp'
options['filetypes'] = [('tapes', '.tp'), ('all files', '.*')]
options['initialdir'] = 'machines'

mcontext.set_tape(machines.parse_tape(relpath(askopenfilename(**options))))
mcontext.checkpoint()
except Exception as e:
print(e)

def load_machine_dialog():
global running, mcontext, tileoffset
try:
options = {}
options['title'] = 'Load Machine'
options['defaultextension'] = '.tm'
options['filetypes'] = [('machines', '.tm'), ('all files', '.*')]
options['initialdir'] = 'machines'

running = False
tileoffset[0] = tileoffset[1] = elapsed = 0
mcontext = machines.MachineContext(machines.Plane())
mcontext.create_machine(relpath(askopenfilename()))
mcontext.create_machine(relpath(askopenfilename(**options)))
mcontext.checkpoint()
except:
pass
except Exception as e:
print(e)

def handle_events():
global running, simrate, timestep, display_machines, mcontext, symsize, elapsed
Expand All @@ -161,6 +186,9 @@ def handle_events():
running = not running
elif uiicons["filerect"].collidepoint(event.pos):
load_machine_dialog()
elif uiicons["tapeloadrect"].collidepoint(event.pos):
load_tape_dialog()


if event.type == pygame.KEYDOWN:
if event.key == K_EQUALS:
Expand Down Expand Up @@ -194,11 +222,15 @@ def handle_events():
running = False
elif event.key == K_m:
display_machines = not display_machines
elif event.key == K_c:
mcontext.checkpoint()
elif event.key == K_r:
mcontext = mcontext.restore()
tileoffset[0] = tileoffset[1] = elapsed = 0
elif event.key == K_f:
load_machine_dialog()
elif event.key == K_t:
load_tape_dialog()


def render():
Expand Down
Binary file added img/tape.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions machines.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ def __init__(self, tape):
self.halted = []
self.delay = 0

def set_tape(self, tape):
self.tape = tape
for m in self.running + self.halted:
m.tape = self.tape

def checkpoint(self):
self.copy = copy.deepcopy(self)

Expand Down Expand Up @@ -298,6 +303,18 @@ def get_n_spawn_args(rule, index):

return args


def parse_tape(path):
with open(normpath(path), 'r', encoding='utf-8') as f:
zero = [int(x) for x in f.readline().split(',')]
if len(zero) != 2:
zero = [0,0]

tape = f.read().split('\n')

return Plane(tape, zero)


def parse_machine(path, maxiter=MAXLIFE):
path = normpath(path)
btck_to_none = lambda c: None if c == '`' else c
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions machines/arithmetic/subtract-composite/subtract.tp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
6,0
3241
203196
12 changes: 12 additions & 0 deletions machines/automata/conway/conway.tp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
1,1
###########
> @@ #
# @@ #
# @ #
# @@ #
# @@ #
# @ #
# #
# #
# <
###########
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions machines/misc/snailchase.tp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
0,0
┌ { ┐
┌ ┐

' > u% └ ┐
*
* └ ┘
└ ) ^ { ┐

*
, v i

*
└ ( ┘
File renamed without changes.

0 comments on commit e4e978a

Please sign in to comment.