class Robot: def __init__(self, m): self.__cpu = Machine(m) ''' {current, min, max} ''' self.__x, self.__y = (0, 0, 0), (0, 0, 0) self.__panels = defaultdict(lambda: '.') self.__color = 0 ''' < : 0 ^ : 1 > : 2 v : 3 ''' self.__direction = 1 if verbose: self.__cpu.toggle_verbose() def start(self, start_color): if start_color: self.__panels[(0, 0)] = '#' while not self.__cpu.halted(): self.__cpu.run(1 if self.__panels[(self.__x[0], self.__y[0])] == '#' else 0) self.paint(self.__cpu.output()) self.turn(self.__cpu.output()) self.move() def display(self): _, minx, maxx = self.__x _, miny, maxy = self.__y grid = [[' ']*(maxx+1) for _ in range(maxy+1)] for (x, y), v in self.__panels.items(): if x < 0 or y < 0: break if v == '#': grid[y][x] = v for l in grid: print(''.join(l)) print('\n', '--------------------------------------\n', 'COUNT', len(self.__panels), '\n\n') def turn(self, lr): if verbose: print('FROM ', ['<', '^', '>', 'v'][self.__direction]*4, lr) if lr == 0: self.left() else: self.right() if verbose: print('TURNED ', ['<', '^', '>', 'v'][self.__direction]*4) def left(self): self.__direction += (3 if self.__direction == 0 else -1) return self.__direction def right(self): self.__direction += (-3 if self.__direction == 3 else 1) return self.__direction def move(self): x, minx, maxx = self.__x y, miny, maxy = self.__y # <- left if self.__direction == 0: if verbose: print('MOV <----', x) x -= 1 minx = min(minx, x) if verbose: print('x:', x) # -> right elif self.__direction == 2: if verbose: print('MOV ---->', x) x += 1 maxx = max(maxx, x) if verbose: print('x:', x) # -> up elif self.__direction == 1: if verbose: print('MOV ^^^^^^', y) y -= 1 miny = min(miny, y) if verbose: print('y:', y) # -> down elif self.__direction == 3: if verbose: print('MOV vvvvvv', y) y += 1 maxy = max(maxy, y) if verbose: print('y:', y) self.__x, self.__y = (x, minx, maxx), (y, miny, maxy) def paint(self, color = None): if color != None: self.__color = color self.__panels[(self.__x[0], self.__y[0])] = ('.' if self.__color == 0 else '#')
class Arcade9000Turbo: def __init__(self, m): self.__cpu = Machine(m) self.__minx = self.__miny = self.__maxx = self.__maxy = 0 ''' we only need to track (x) ''' self.__player_x = 0 self.__ball_x = 0 self.__canvas = defaultdict(lambda: ' ') self.__scores = 0 self.__tiles = {0: ' ', 1: '|', 2: '⬜', 3: '➖', 4: '⚪'} if verbose: self.__cpu.toggle_verbose() def play(self, auto: bool = False): self.__cpu.run() while not self.__cpu.halted() or self.__cpu.has_output(): x = self.__cpu.output() y = self.__cpu.output() if x == -1 and y == 0: ''' player's current score ''' self.__scores = self.__cpu.output() self.display() elif x != None and y != None: ''' draw a tile to the screen ''' self.draw(x, y, self.__cpu.output()) self.display() elif auto and self.__cpu.waiting(): m = 0 if self.__ball_x > self.__player_x: m = 1 print('_/_ >') elif self.__ball_x < self.__player_x: m = -1 print('_\\_ >') else: print('_|_ >') self.__cpu.run(m) elif self.__cpu.waiting(): print('_|_ >') self.input() def input(self): try: self.__cpu.run(int(sys.stdin.readline())) except ValueError: print('Invalid input (hint: enter -1, 0, or 1 ):') self.input() def display(self): grid = [[' '] * (self.__maxx + 1) for _ in range(self.__maxy + 1)] count = 0 for (x, y), v in self.__canvas.items(): if x < 0 or y < 0: break if v == self.__tiles[2]: count += 1 grid[y][x] = v subprocess.call("clear") for l in grid: print(''.join(l)) print('\n') print('.' * (self.__maxx + 1)) print('Score: {0}, Blocks: {1}'.format(self.__scores, count)) def draw(self, x, y, tile_id): self.__canvas[(x, y)] = self.__tiles[tile_id] self.__minx = min(self.__minx, x) self.__maxx = max(self.__maxx, x) self.__miny = min(self.__miny, y) self.__maxy = max(self.__maxy, y) if tile_id == 4: ''' track ball displacement ''' self.__ball_x = x elif tile_id == 3: ''' track player position ''' self.__player_x = x
def read(self, n: Machine): o = [] for _ in range(3): o.append(n.output()) return o