骰子游戏模拟器 Python

-2 投票
1 回答
753 浏览
提问于 2025-04-18 02:45

我花了不少时间在这里的论坛上寻找答案,但还是卡住了。

希望有人能帮我一下。我正在用Python做一个掷骰子游戏的模拟器,已经完成了很多部分。现在我遇到的问题是关于“点数”的。

也就是说:
如果第一次掷骰子的结果是其他任何值,就会确定一个点数。
如果在确定了点数后,再次掷到这个点数而不是7,玩家就赢了。
如果在确定了点数后,先掷到7而不是点数,玩家就输了。

这是我目前写的代码。我觉得离完成不远了,但就是有些地方不太明白。

这个程序是另一个类的一部分,如果需要的话我也可以把那个类的代码发出来。但我觉得问题可能出在应用的部分。 (见下文)

#
    from Dice import pairOfDice
print("Now Let's play with two dice!")
#
def MainDouble():
    bDice = pairOfDice()
    doubleDiceRoll = ''
    raw_input ("Press Enter to Roll the Dice")

    while doubleDiceRoll == '':
        bDice.toss()
        print ('The first die reads.. ' + str(bDice.getValue1()) + '\n')
        print ('The second die reads.. ' + str(bDice.getValue2()) + '\n')


        if bDice.getTotal() == 7 or bDice.getTotal() == 11:
         print("You Win, Congratulations")
        if bDice.getTotal() == 2 or bDice.getTotal() == 3 or bDice.getTotal() == 12:
         print("You Lose, Bummer")
        elif bDice.getTotal() != 7 and bDice.getTotal() != 11 and bDice.getTotal() != 2 and bDice.getTotal() != 3 and bDice.getTotal() != 12:
           pointValue = bDice.getTotal()    

           while pointValue != 7 or pointValue != bDice.getTotal():
               if pointValue == 7:
                print("You Win!")
               elif pointValue == bDice.getTotal():
                  print("You Lose!")
                  print("Roll Again")
           doubleDiceRoll = raw_input ("Roll Again?")



MainDouble()

1 个回答

2

这里有很多不必要的代码。幸运的是,这正好是我的强项,因为我曾经是赌场的发牌员,现在转行做程序员了 :)

def MainDouble():
    bDice = pairOfDice()
    point = None
    while True:
        bDice.toss()
        if point:
            if bDice.getTotal() == 7:
                # you lose
                point = None
            elif bDice.getTotal() == point:
                # you win
                point = None
            else:
                # roll again
        else: # no point, this is the come out roll
            if bDice.getTotal() in [7, 11]: # win
            elif bDice.getTotal() in [2,3,12]: # lose
            else:
                point = bDice.getTotal()

不过,你真的应该对很多部分进行重构,也就是重新整理和优化一下代码。

class CrapsGame(object):
    def __init__(self):
        self.dice = pairOfDice()
        self.point = None
    def rolldice(self):
        self.dice.toss()
    def playagain(self):
        if input("Roll again? (y/n): ").lower() == 'y':
            return True
        return False
    def score(self):
        result = self.dice.getTotal()
        if self.point:
            if result == 7:
                self.point = None
                # lose
            elif result == self.point:
                self.point = None
                # win
            else:
                # keep going
        else:
            if result in [7, 11]: # win
            elif result in [2,3,12]: # lose
            else:
                self.point = result
    def run(self):
        while True:
            self.rolldice()
            self.score()
            if not self.playagain():
                return

if __name__ == "__main__":
    game = CrapsGame()
    game.run()

撰写回答