Beispiel #1
0
class CommandlinePlayer(Player):

    def __init__(self, game, uid=1, max_turns=3, point_config='STD_CONFIG'):
        super(CommandlinePlayer, self).__init__(game, uid=uid, point_config=point_config, max_turns=max_turns)
        self.commands = {
            'd': self.roll_dice,
            's': self.save_dice,
            'save': self.save_dice,
            'help': print_help,
            'p': self.show_points,
            'p_all': self.show_all_points
        }

        self.point_commands = [
            'one', 'two', 'three', 'four', 'five', 'six', 'threesome', 'foursome', 'onepair', 'twopair',
            'smallstreet', 'bigstreet', 'kniffel', 'fullhouse', 'chance'
        ]

    def play(self):
        self.dice = Dice()
        while True:
            try:
                self.command_prompt()
            except WrongCommandException:
                print_message('wrongcommand')
            except TurnEndException:
                break

        self.turn = 0

    def roll_dice(self):
        super(CommandlinePlayer, self).roll_dice()
        print_dice(self.dice)

    def show_points(self):
        print_points(self)

    def print_points(self):
        print_points(self.points)

    def command_prompt(self):
        value = prompt('playerprompt', self.id)
        try:
            if value.split(' ')[0] is 's':
                for pos in value.split(' ')[1].split(','):
                    try:
                        self.save_dice([int(pos)])
                    except ValueError:
                        print_message('unknown_param', pos)
                print_dice(self.dice)
            elif value in self.commands:
                try:
                    self.commands[value]()
                except NoTurnsLeftException:
                    print_message('noturnsleft')
            elif value.split(' ')[0] in self.point_commands:
                try:
                    try:
                        column = value.split(' ')[1]
                        self.entry_points(value, column, self.dice.valuelist())
                        raise TurnEndException()
                    except IndexError:
                        print_message('specify_column')
                except FieldAlreadyAssignedException:
                    print_message('fieldblocked')
            else:
                raise WrongCommandException()
        except IndexError:
            print_dice(self.dice)

    def show_all_points(self):
        print_points(self.game.players)