سؤال

I've spent quite a few hours looking through the forums here and I'm still stuck.

I hope someone can give me a hand. I'm working on a craps simulator in python, and I've done a lot of it. I'm just having an issue with the "point".

I.E. If the first roll is any other value, it establishes a point. If, with a point established, that point is rolled again before a 7, the player wins. If, with a point established, a 7 is rolled before the point is rolled again, the player loses.

Here's the code I have so far. I feel like I'm close but I can't quite grasp it.

This program is part of another class which I can post too if desired. But I think the cause is somewhere in the application portion. (below)

#
    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()
هل كانت مفيدة؟

المحلول

Lots of unnecessary code here. Luckily this is kind of my wheelhouse, as a former casino dealer turned programmer :)

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()

You should really refactor a lot of this, though.

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()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top