Craps模拟器Python

时间:2014-04-15 01:15:04

标签: python if-statement point

我花了几个小时在这里查看论坛,但我仍然卡住了。

我希望有人能帮助我。我在python中使用掷骰子模拟器,我做了很多。我只是对#34;点"有问题。

即。 如果第一个滚动是任何其他值,则它建立一个点。 如果在建立点的情况下,该点在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 个答案:

答案 0 :(得分: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()