首页 > 解决方案 > 尝试用 Python 重新创建一个名为“去波士顿”的骰子游戏

问题描述

我正在尝试用 python 开发“去波士顿”。代码中解释了很多规则,注释中解释了很多代码。我会回答任何问题,但我的输出有一些问题。这就是我所拥有的。

# This code aims to recreate the game "Going to Boston" for four players.
# Rules:
# - Each player rolls three dice.
# – Each player keeps their highest die and sets it aside.
# – The remaining two dice are re-rolled and the highest of the two is set aside.
# – The last die is rolled and the final score is the sum of the three dice.
# ----------------------------------------------------------------------------------------------------------------------
# We'll use the random module for the dice.
import random
# This variable is just to count rounds
RoundCount = 0
# These global variables are being used to save the score of each player.
Score1 = 0
Score2 = 0
Score3 = 0
Score4 = 0
FinalScore = []  # We'll use this to decide the winner(s).
# Here is our class for player.
class Player:
    def __init__(self, name):
        self.name = name  # Storing their name, just to more easily identify each player.
        d1 = random.randint(1,6)  # Randomly choosing a number from 1-6 to simulate a dice.
        d2 = random.randint(1,6)
        d3 = random.randint(1,6)
        self.dice = [d1, d2, d3]
        self.SavedDice = []  # We'll be using this variable to save dice.
    def score(self):
        # here is where dice are actually coded and saved.
        # Here, we'll save the max value in dice.
        self.SavedDice.append(max(self.dice))
        self.dice.remove(max(self.dice))  # We're removing the max value, and maintaining the previous two dice.
        for i in self.dice:
            i = random.randint(1,6)
            self.dice.append(i)
        #return print(self.name,"\b's score is:",sum(self.SavedDice))
        return(self.dice)
    def __str__(self):  # Here is where we return the saved dice.
        return print(self.name,'Saved Dice:',self.SavedDice)
# Here is where we actually play.
# First, we setup the players.
Player1 = Player('Player 1')
Player2 = Player('Player 2')
Player3 = Player('Player 3')
Player4 = Player('Player 4')
# We'll use a loop to manage rounds.
while RoundCount < 3:
    RoundCount += 1
    # We use the __str__ method to show the currently saved dice.
    Player1.__str__()
    Player2.__str__()
    Player3.__str__()
    Player4.__str__()
    # We'll assign values for scoring, to be used later, here
    FScore1 = Player1.score()
    FScore2 = Player2.score()
    FScore3 = Player3.score()
    FScore4 = Player4.score()
# Here is where we'll score each player.
# We'll first append all the final scores of each player to be compared.
FinalScore.append(FScore1, FScore2, FScore3, FScore4)
WinningScore = max(FinalScore)
if FScore1 == WinningScore:
    print('Player 1 Won!')
if FScore2 == WinningScore:
    print('Player 2 Won!')
if FScore3 == WinningScore:
    print('Player 3 Won!')
if FScore4 == WinningScore:
    print('Player 4 Won!')
# Just cleanly exiting the code.
print(' ')
exit('End of Game')

我最终得到这样的输出,我不知道为什么。我还需要保留 def str (self): 并使用它。

Player 1 Saved Dice: []
Player 2 Saved Dice: []
Player 3 Saved Dice: []
Player 4 Saved Dice: []

似乎它没有一直循环或以某种方式被取消,并且值没有被保存到self.SavedDiceor SavedDice

标签: python

解决方案


import random

# Here is our class for player.
class Player:
    def __init__(self, name):
        self.name = name  # Storing their name, just to more easily identify each player.
        d1 = random.randint(1,6)  # Randomly choosing a number from 1-6 to simulate a dice.
        d2 = random.randint(1,6)
        d3 = random.randint(1,6)
        self.dice = [d1, d2, d3]
        self.saved_dice = []  # We'll be using this variable to save dice.

    def roll(self):
        # here is where dice are actually coded and saved.
        # Here, we'll save the max value in dice.
        max_dice = max(self.dice)
        self.saved_dice.append( max_dice )
        self.dice.remove( max_dice )  # We're removing the max value, and maintaining the previous two dice.
        self.dice = [ random.randint(1,6) for _ in self.dice ] # recreate list with size of remained values

    @property
    def score(self):
        return sum(self.saved_dice)

    def __str__(self):  # Here is where we return the saved dice.
        return f'{self.name} Saved Dice: { self.saved_dice }'


def main():
    max_players = 4
    round_count = 0
    # Create set of players
    players = { Player(f'Player { i + 1 }') for i in range(max_players) }

    while round_count < 3:
        round_count += 1
        for player in players:
            # Roll each player per each round
            player.roll()
            print(player)
    # Choosing winner player
    winner = max( players, key=lambda p: p.score )
    print(f'{winner.name} Won!')
    print(' ')
    exit('End of Game')

if __name__ == '__main__':
    main()

推荐阅读