Пример #1
0
class GameController:
    '''A controller for a simple dice game'''
    def __init__(self):
        self.player_dice = PairOfDice()

    def start_game(self):
        '''interact with user, roll the pair of die and
        determine whether the user wins or not.
        None -> None'''

        input("Press enter to roll the dice...")
        self.player_dice.roll_dice()
        result = self.player_dice.current_value()
        if result == 7 or result == 11:
            print("You rolled " + str(result) + ". You win!")
        elif result == 2 or result == 3 or result == 12:
            print("You rolled " + str(result) + ". You lose.")
        else:
            done = False
            point = result
            while not done:
                print("Your point is " + str(point) + ".")
                input("Press enter to roll the dice...")
                self.player_dice.roll_dice()
                result = self.player_dice.current_value()
                if result == point:
                    print("You rolled " + str(result) + ". You win!")
                    done = True
                elif result == 7:
                    print("You rolled " + str(result) + ". You lose.")
                    done = True
Пример #2
0
class GameController:
    """A controller for a simple Street Craps game"""
    def __init__(self):
        ''' create the PairOfDice object '''
        self.PairOfDice = PairOfDice()

    def start_play(self):
        ''' start the play '''
        print(input("Press Enter to roll the dice...\n"))
        self.play()

    def play(self):
        ''' check if the player loses or wins in the first throw according
to game rules. If not, go to the repeat function '''
        die_sum = self.PairOfDice.current_value()

        if die_sum in (2, 3, 12):
            ''' the player loses if s/he throws 2, 3 or 12 in the first
turn '''
            self.do_busted(die_sum)

        elif die_sum in (7, 11):
            ''' the player wins if s/he throws 7 or 11 in the first
turn '''
            self.do_win(die_sum)

        else:
            self.repeat(die_sum)

    def do_busted(self, x):
        print("You rolled", x, ". You lose!")
        sys.exit()

    def do_win(self, x):
        print("You rolled", x, ". You win!")
        sys.exit()

    def repeat(self, die_sum):
        ''' check until the next throw shows the value equal to the
first point thrown or until 7 shows, which ever is first. If 7
player loses. '''
        point = die_sum
        print("Your point is : ", point)
        new_sum = 0

        while new_sum != point:
            if new_sum == 7:
                print("You rolled 7. You lose.")
                sys.exit()

            input("Press Enter to roll the dice...\n")
            new_sum = self.PairOfDice.current_value()
            print("You rolled :", new_sum)

        print("You win!")
        sys.exit()
Пример #3
0
class GameController:
    """Manage the rolling, scoring, and user interaction"""
    def __init__(self):
        self.the_pair = PairOfDice()

    def start_play(self):
        points = 0
        self.the_pair = PairOfDice()
        text = input("Press enter to roll the dice...\n")
        if text == "":
            self.the_pair.roll_dice()
            first_roll = self.the_pair.current_value()
            if (first_roll == 2 or first_roll == 3 or first_roll == 12):
                print("You rolled", first_roll, ". You lose!")
            elif (first_roll == 7 or first_roll == 11):
                print("You rolled", first_roll, ". You win!")
            else:
                points = self.the_pair.current_value()
                print("Your point is", points)
                self.the_pair.roll_dice()
                next_roll = self.the_pair.current_value()
                while (next_roll != points or next_roll != 7):
                    text = input("Press enter to roll the dice...\n")
                    if text == "":
                        self.the_pair.roll_dice()
                        next_roll = self.the_pair.current_value()
                        if next_roll == points:
                            print("You rolled", next_roll, ". You win.")
                            break
                        elif next_roll == 7:
                            print("You rolled 7. You lose.")
                            break
                        else:
                            print("You rolled", next_roll, ".")
                    else:
                        print("Press enter to roll the dice...\n")
        else:
            print("Press enter to roll the dice...\n")
Пример #4
0
 def start_play(self):
     pairofdice = PairOfDice()
     # If you roll 7 or 11 on your first roll, you win.
     if pairofdice.current_value() in FIRST_ROUND_WIN:
         print(f"You rolled {pairofdice.current_value()}. You win!")
     # If you roll 2, 3, or 12 on your first role, you lose.
     elif pairofdice.current_value() in FIRST_ROUND_LOSE:
         print(f"You rolled {pairofdice.current_value()}. You lose.")
     # If you roll anything else, that's your 'point', and
     # you keep rolling until you either roll your point
     # again (win) or roll a 7 (lose)
     else:
         point = pairofdice.current_value()
         print(f"Your point is {point}")
         input("Press enter to roll the dice...\n")
         pairofdice.roll_dice()
         while pairofdice.current_value() not in [point, LOSING_NUM]:
             print(f"You rolled {pairofdice.current_value()}.")
             input("Press enter to roll the dice...\n")
             pairofdice.roll_dice()
         if pairofdice.current_value() == point:
             print(f"You rolled {pairofdice.current_value()}. You win!")
         else:
             print(f"You rolled {pairofdice.current_value()}. You lose.")
Пример #5
0
class GameController:
    def __init__(self):
        """creat a PairOfDice() object, initialize the point and score value"""
        self.pair = PairOfDice()
        self.point = None
        self.origin_point = None

    def start_play(self):
        """Start to play the game, roll the dice and get current value"""
        input("Please enter to roll the dice...\n")
        self.pair.roll_dice()
        self.point = self.pair.current_value()

    def show_result(self):
        """get the result based on the rules"""
        WIN_POINTS = [7, 11]
        LOSE_POINTS = [2, 3, 12]
        if self.point in LOSE_POINTS:
            print("You rolled {}. You lose.".format(self.point))
        elif self.point in WIN_POINTS:
            print("You rolled {}. You win.".format(self.point))
        else:
            print("You point is {}".format(self.point))
            self.origin_point = self.point
            self.continue_game()

    def continue_game(self):
        """didn't get the result, continue to play,
        compare the point with the origin_point"""
        LOSE_POINT = 7
        self.start_play()
        while self.point != self.origin_point and self.point != LOSE_POINT:
            print("You rolled {}.".format(self.point))
            self.start_play()
        else:
            if self.point == self.origin_point:
                print("You rolled {}. You win.".format(self.point))
            else:
                print("You rolled {}. You lose.".format(self.point))
Пример #6
0
class GameController:
    '''game controller for the dice game'''
    def __init__(self):
        self.pair_of_dice = PairOfDice()

    print("-------------------------")
    print("Welcome to street craps!")
    print("If you roll 7 or 11 on your first roll, you win.")
    print("If you roll 2, 3, or 12 on your first role, you lose.")
    print("If you roll anything else, that's your 'point', and")
    print("you keep rolling until you either roll your point")
    print("again (win) or roll a 7 (lose)")
    enter = input("Press enter to roll the dice...")

    def situations(self):
        WIN = [7, 11]
        LOSE = [2, 3, 12]
        SEVEN = 7
        # situation: win at the first time
        if (self.pair_of_dice.current_value() in WIN):
            print("You rolled",
                  str(self.pair_of_dice.current_value()) + ". You Win!")
        # situation: lose at the first time
        elif (self.pair_of_dice.current_value() in LOSE):
            print("You rolled",
                  str(self.pair_of_dice.current_value()) + ". You Lose.")
        # situation: roll until same point or magic num 7
        else:
            print("Your point is", self.pair_of_dice.current_value())
            point = self.pair_of_dice.current_value()
            self.pair_of_dice.roll_dice()
            while self.pair_of_dice.current_value() != point:
                enter = input("Press enter to roll the dice...")
                self.pair_of_dice.roll_dice()
                print("You rolled", self.pair_of_dice.current_value())
                if self.pair_of_dice.current_value() == SEVEN:
                    print(
                        "You rolled",
                        str(self.pair_of_dice.current_value()) + ". You Lose.")
                    break
            else:
                print("You rolled",
                      str(self.pair_of_dice.current_value()) + ". You Win!")
        # I will revisit this part to try to put win/lose/continue rolling
        # situations in three objects once I learned more OOD

    def start_play(self):
        self.pair_of_dice.roll_dice()
        self.situations()