def run(self): self.term = Term() while True: board.draw() loc, val = self.get_move() board[loc] = Number(val) sudoku.check_end()
class BasicInterface: def __init__(self): self.term = Term() def run(self): # board is only used to check if location is within range (using board.valid()) for player in cycle(players): bship.draw() tile = self.ai_move(player) if player.ai else self.get_move(player) tile.hit() bship.draw() for _ in range(2): self.blink_tile(tile) bship.check_end(player.enemy) def blink_tile(self, tile): tile.hidden = not tile.hidden sleep(blink_speed) bship.draw() def get_move(self, player): """Get user command and return the tile to attack.""" commands.player = player while True: cmd = self.term.getch() try: val = commands[cmd]() if val: return val except KeyError: print("unknown command:", cmd) def ai_move(self, player): """Very primitive 'AI', always hits a random location.""" return player.enemy.board.random_unhit()
class BasicInterface(object): def run(self): self.term = Term() while True: board.draw() loc, val = self.get_move() board[loc] = Number(val) sudoku.check_end() def get_move(self): "UNUSED." while True: cmd = self.textinput.getinput() if sudoku.valid_move(*cmd): return cmd else: print(self.textinput.invalid_move) def get_move(self): while True: cmd = self.term.getch() try: if cmd.isdigit(): val = commands.move(int(cmd)) else: val = commands[cmd]() if val: return val except KeyError: print("invalid command:", cmd)
class BlockyBlocks(object): winmsg = "player %s wins!" counter = Loop(range(check_moves)) def __init__(self): self.term = Term() def check_end(self, player): """Check if game is finished.""" return all(tile.player==player for tile in board) def end(self, player): board.draw() print(nl, self.winmsg % player) sleep(2) sys.exit() def run(self): for p in cycle(players.keys()): board.draw() tile = board.ai_move(p) if p in ai_players else self.get_move(p) tile.increment(p) if self.check_end(p): self.end(p) def get_move(self, player): commands.player = player while True: cmd = self.term.getch() try: val = commands[cmd]() if val: return val except KeyError: print("unknown command:", cmd)
def run(self): self.term = Term() moves = board.get_valid_moves player = rndchoice(players) player = first(players) while True: board.draw() move = player.get_random_move() if player.ai else self.get_move( player) tiles = player.make_move(move) self.blink_tiles(tiles) # give next turn to enemy OR end game if no turns left, FALLTHRU: current player keeps the turn if moves(player.enemy()): player = player.enemy() elif not moves(player): versi.game_end()
def main_loop(self): t = Term() while True: self.board.display() if self.player.message: for line in self.player.message: print(line) # pause for reading a message t.getch() self.player.message = False self.board.display() if self.player.go_mode: self.player.go() # continue with go mode.. continue c = '' while not c or c[-1].isdigit() or c[-1]=='g': c += t.getch().decode("utf-8") self.run_cmd(c)
def main_loop(self): t = Term() while True: self.board.display() if self.player.message: for line in self.player.message: print(line) # pause for reading a message t.getch() self.player.message = False self.board.display() if self.player.go_mode: self.player.go() # continue with go mode.. continue c = '' while not c or c[-1].isdigit() or c[-1] == 'g': c += t.getch().decode("utf-8") self.run_cmd(c)
def run(self): """ Run main game loop. If more than one valid move is available to the human player, let him make the choice with `get_move()`. """ def offer_choice(): return not player.ai and len(valid_moves) > 1 self.term = Term() pchar = first(p for p in player_chars if p not in ai_players) if pchar: print("You are playing:", pchar) for player in cycle(players): race.draw() movedist = dice.rollsum() valid_moves = race.valid_moves(player, movedist) getmove = self.get_move if offer_choice() else rndchoice race.move(*getmove(valid_moves)) player.check_end()
def run(self): self.term = Term() moves = board.get_valid_moves player = rndchoice(players) player = first(players) while True: board.draw() move = player.get_random_move() if player.ai else self.get_move(player) tiles = player.make_move(move) self.blink_tiles(tiles) # give next turn to enemy OR end game if no turns left, FALLTHRU: current player keeps the turn if moves(player.enemy()): player = player.enemy() elif not moves(player): versi.game_end()
class BasicInterface: def run(self): self.term = Term() while True: board.draw() tile = self.get_move() mines.check_end(tile) def get_move(self): """Get user command and return the tile to reveal.""" while True: cmd = self.term.getch() try: val = commands[cmd]() if val: return val except KeyError: print("unknown command:", cmd)
class BasicInterface(object): def run(self): """ Run main game loop. If more than one valid move is available to the human player, let him make the choice with `get_move()`. """ def offer_choice(): return not player.ai and len(valid_moves) > 1 self.term = Term() pchar = first(p for p in player_chars if p not in ai_players) if pchar: print("You are playing:", pchar) for player in cycle(players): race.draw() movedist = dice.rollsum() valid_moves = race.valid_moves(player, movedist) getmove = self.get_move if offer_choice() else rndchoice race.move(*getmove(valid_moves)) player.check_end() def get_move(self, valid_moves): """Get player's choice of move.""" moves = [space] * (length + 6) for n, (loc, _) in enumerate1(valid_moves): moves[loc] = n print(sjoin(moves)) while True: val = self.term.getch() if val=='q': sys.exit() try: return valid_moves[int(val)-1] except (ValueError, IndexError): print("Invalid move")
class BasicInterface(object): def run(self): self.term = Term() moves = board.get_valid_moves player = rndchoice(players) player = first(players) while True: board.draw() move = player.get_random_move() if player.ai else self.get_move( player) tiles = player.make_move(move) self.blink_tiles(tiles) # give next turn to enemy OR end game if no turns left, FALLTHRU: current player keeps the turn if moves(player.enemy()): player = player.enemy() elif not moves(player): versi.game_end() def get_move(self, player): commands.player = player while True: val = self.term.getch() try: val = commands[val]() if val: return val except KeyError: print("Invalid move") def blink_tiles(self, tiles): for _ in range(2): for tile in tiles: tile.blank = not tile.blank sleep(blink_speed) board.draw()
class BasicInterface(object): def run(self): self.term = Term() moves = board.get_valid_moves player = rndchoice(players) player = first(players) while True: board.draw() move = player.get_random_move() if player.ai else self.get_move(player) tiles = player.make_move(move) self.blink_tiles(tiles) # give next turn to enemy OR end game if no turns left, FALLTHRU: current player keeps the turn if moves(player.enemy()): player = player.enemy() elif not moves(player): versi.game_end() def get_move(self, player): commands.player = player while True: val = self.term.getch() try: val = commands[val]() if val: return val except KeyError: print("Invalid move") def blink_tiles(self, tiles): for _ in range(2): for tile in tiles: tile.blank = not tile.blank sleep(blink_speed) board.draw()
def __init__(self): self.term = Term()
def run(self): self.term = Term() while True: board.draw() tile = self.get_move() mines.check_end(tile)
msg = self.winmsg % (self.random_reveals * 3 + self.guesses) if won else self.losemsg print(msg) sys.exit() class BasicInterface(object): def run(self): while True: words.display() self.get_move() words.check_end() def get_move(self): while True: cmd = term.getch() try: val = commands[cmd]() if val: return val except KeyError: print("unknown command:", cmd) words.check_end() if __name__ == "__main__": term = Term() words = Words(open(wordsfn).readlines()) commands = Commands(term, commands) BasicInterface().run()