예제 #1
0
class Game:

    def __init__(self, *players):
        self.players = players
        self.dice = DiceSet()
        self._winner = None

    def play_round(self):
        round_scores = list()
        for player in self.players:
            self.dice.roll(5)
            rolled_score = score(self.dice.values)
            round_scores.append(rolled_score)
        return round_scores

    @property
    def winner(self):
        return self._winner
예제 #2
0
class Player(object):

    def __init__(self, name, dice_number=5):
        super(Player, self).__init__()
        self._name = name
        self._dice_set = DiceSet()
        self._next_dice_number = dice_number
        self._records = []  # 记录每次点数
        self.score = 0
        self.is_alive = True
        self.inited_game = False

    @property
    def name(self):
        """player的名字"""
        return self._name

    @property
    def records(self):
        """每次摇骰子的记录 --> [[1, 3, 2, 1, 4],[1, 2, 3]]"""
        return self._records

    def roll(self):
        """player摇一次骰子

        会将这次摇骰子的结果添加到records列表中保存

        """
        record = self._dice_set.roll(self._next_dice_number)
        self._records.append(record)
        return record

    def set_next_dice_number(self, dice_number):
        """设置下次可以摇的骰子数目"""
        self._next_dice_number = dice_number

    def __str__(self):
        return '<选手 {0}: 总分{1}>\n记录({2})'.format(
            self.name,
            self.score,
            len(self.records)
        )