|
| 1 | +import time, sys, pygcurse, pygame |
| 2 | + |
| 3 | +VIEW_DISTANCE = 15 |
| 4 | +FONTSIZE = 10 |
| 5 | +STATECOLORS = ["red", (0, 255, 255), (0, 255, 0)] |
| 6 | + |
| 7 | +class TuringStrip(): |
| 8 | + def __init__(self): |
| 9 | + self.data = [] |
| 10 | + |
| 11 | + def __setitem__(self, nn, value): |
| 12 | + n = tuple(nn) |
| 13 | + if(value and (n not in self.data)): |
| 14 | + self.data.append(n) |
| 15 | + elif((not value) and (n in self.data)): |
| 16 | + del self.data[self.data.index(n)] |
| 17 | + |
| 18 | + def __getitem__(self, nn): |
| 19 | + n = tuple(nn) |
| 20 | + return (n in self.data) |
| 21 | + |
| 22 | +def disp(): |
| 23 | + turingString = "" |
| 24 | + for i in range(head[0]-VIEW_DISTANCE, head[0]+VIEW_DISTANCE+1): |
| 25 | + for j in range(head[1]-VIEW_DISTANCE, head[1]+VIEW_DISTANCE+1): |
| 26 | + if(i == 0 and j == 0): |
| 27 | + bw = "◇◆" |
| 28 | + elif(i == 0 or j == 0): |
| 29 | + bw = "□■" |
| 30 | + else: |
| 31 | + bw = "◯●" |
| 32 | + bw = bw[0] + "█" |
| 33 | + turingString += bw[int(t[i, j])] |
| 34 | + turingString += "\n" |
| 35 | + win.putchars(turingString, fgcolor=(0, 255, 0), bgcolor="black") |
| 36 | + i, j = head |
| 37 | + if(i == 0 and j == 0): |
| 38 | + bw = "◇◆" |
| 39 | + elif(i == 0 or j == 0): |
| 40 | + bw = "□■" |
| 41 | + else: |
| 42 | + bw = "◯●" |
| 43 | + win.putchar(bw[int(t[i, j])], x=VIEW_DISTANCE, y=VIEW_DISTANCE, fgcolor=STATECOLORS[state]) |
| 44 | + win.update() |
| 45 | + time.sleep(speed) |
| 46 | + |
| 47 | +def turingDo(write, turn, nextstate): |
| 48 | + global t, head, dir, state |
| 49 | + if(write != -1): |
| 50 | + t[head] = write |
| 51 | + dir = (dir + turn) % 4 |
| 52 | + state = nextstate |
| 53 | + |
| 54 | +head = [0, 0] |
| 55 | +dir = 3 |
| 56 | + |
| 57 | +t = TuringStrip() |
| 58 | +pygame.init() |
| 59 | +win = pygcurse.PygcurseWindow((VIEW_DISTANCE * 2) + 1, |
| 60 | + (VIEW_DISTANCE * 2) + 1, |
| 61 | + "Turmites", |
| 62 | + font=pygame.font.SysFont("wenquanyimicroheimono", FONTSIZE)) |
| 63 | + |
| 64 | +speed = 0.01 |
| 65 | +state = 0 |
| 66 | +moves = 0 |
| 67 | +disp() |
| 68 | + |
| 69 | +from Worm_Lantingtons_Ant import states |
| 70 | + |
| 71 | +import random |
| 72 | +while True: |
| 73 | + read = t[head] |
| 74 | + turingDo(*states[state][read]) |
| 75 | + if(dir == 0): |
| 76 | + head[1] += 1 |
| 77 | + elif(dir == 1): |
| 78 | + head[0] += 1 |
| 79 | + elif(dir == 2): |
| 80 | + head[1] -= 1 |
| 81 | + elif(dir == 3): |
| 82 | + head[0] -= 1 |
| 83 | + moves += 1 |
| 84 | + disp() |
| 85 | + for event in pygame.event.get(): |
| 86 | + if event.type == pygame.locals.QUIT: |
| 87 | + pygame.quit() |
| 88 | + sys.exit() |
| 89 | + |
0 commit comments