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 bool(not player.ai and len(valid_moves) > 1) self.textinput = TextInput('%hd') 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: try: return valid_moves[ self.textinput.getval() ] except IndexError: print(self.textinput.invalid_move)
class RockPaperScissors(object): def run(self): self.textinput = TextInput("(r|p|s)") scores = [0, 0] while True: choice1, choice2 = (self.get_move(p) for p in players) if choice1 != choice2: winplayer = 1 if (choice1+choice2 in wins) else 0 scores[winplayer] += 1 print(status % (players[0], scores[0], players[1], scores[1], choice1, choice2)) sleep(pause_time) def get_move(self, player): if player in ai_players : return randchoice(moves) else : return self.textinput.getval()
class RockPaperScissors(object): def run(self): self.textinput = TextInput("(r|p|s)") scores = [0, 0] while True: choice1, choice2 = (self.get_move(p) for p in players) if choice1 != choice2: winplayer = 1 if (choice1 + choice2 in wins) else 0 scores[winplayer] += 1 print(status % (players[0], scores[0], players[1], scores[1], choice1, choice2)) sleep(pause_time) def get_move(self, player): if player in ai_players: return randchoice(moves) else: return self.textinput.getval()