コード例 #1
0
ファイル: aoc_25.py プロジェクト: Airymon/aoc2019

def to_ascii_instruction(instruction):
    ascii_instr = []
    for char in instruction:
        ascii_instr.append(ord(char))
    return ascii_instr


def print_output(intcode_instruction):
    str = ''
    for digit in intcode_instruction:
        str = str + chr(digit)
    print(str)


adventure = IntcodeComputer(input_prog, [], "Cryostasis")
adventure.run_program()
output = adventure.get_output()
print_output(output)

while not adventure.has_stopped():
    get_input = input(">")
    input_instruction = to_ascii_instruction(get_input + "\n")
    adventure.add_prog_input(input_instruction)
    adventure.run_program()
    output = adventure.get_output()
    print_output(output)

print("Game Over.")
コード例 #2
0
ファイル: aoc_13.py プロジェクト: Airymon/aoc2019
class ArcadeCabinet():
    def __init__(self, program, free_play=False, auto=False, display=True):
        if free_play:
            program = program[:]
            program[0] = 2
        self.computer = IntcodeComputer(program, [], "Arcade Cabinet")
        self.gamestate = dict()
        self.ball, self.tile = None, None
        self.auto = auto
        self.display = display

    def draw_screen(self):
        max_x = max(x for x, y in self.gamestate.keys())
        max_y = max(y for x, y in self.gamestate.keys())
        score = self.gamestate[(-1, 0)] if (-1, 0) in self.gamestate else 0
        printstr = "\tSCORE:\t %i\n" % score
        for y in range(max_y + 1):
            for x in range(max_x + 1):
                printstr = printstr + "%s" % tiles[self.gamestate[(x, y)]]
            printstr = printstr + "\n"
        sys.stdout.write(printstr)

    def parse_instruction(self, instruction):
        x, y, tile = instruction
        self.gamestate[(x, y)] = tile
        if tile == 4:
            self.ball = x
        elif tile == 3:
            self.tile = x

    def joystick(self, key='n'):
        if keyboard.is_pressed('a') or key == 'a':
            self.computer.add_input(-1)
        elif keyboard.is_pressed('d') or key == 'd':
            self.computer.add_input(1)
        elif keyboard.is_pressed('s') or key == 's':
            self.computer.add_input(0)

    def self_play(self):
        if self.tile > self.ball:
            return 'a'
        elif self.tile < self.ball:
            return 'd'
        else:
            return 's'

    def score_counter(self):
        score = self.gamestate[(-1, 0)] if (-1, 0) in self.gamestate else 0
        sys.stdout.write("\r")
        sys.stdout.flush()
        sys.stdout.write("Score: %i" % score)

    def run_game(self):
        while not self.computer.has_stopped():
            output = self.computer.run_program()
            for instruction in chunks(output, 3):
                self.parse_instruction(instruction)
            if self.display:
                self.draw_screen()
                time.sleep(0.07)
            else:
                self.score_counter()
            self.computer.input_l.clear()
            key = self.self_play() if self.auto else None
            self.joystick(key)
        sys.stdout.write('\n')
        self.draw_screen()