|
| 1 | +import random |
| 2 | +import time |
| 3 | + |
| 4 | + |
| 5 | +dice_faces = [ |
| 6 | + [' ------- ', |
| 7 | + '| |', |
| 8 | + '| O |', |
| 9 | + '| |', |
| 10 | + ' ------- '], |
| 11 | + |
| 12 | + [' ------- ', |
| 13 | + '| O |', |
| 14 | + '| |', |
| 15 | + '| O |', |
| 16 | + ' ------- '], |
| 17 | + |
| 18 | + [' ------- ', |
| 19 | + '| O |', |
| 20 | + '| O |', |
| 21 | + '| O |', |
| 22 | + ' ------- '], |
| 23 | + |
| 24 | + [' ------- ', |
| 25 | + '| O O |', |
| 26 | + '| |', |
| 27 | + '| O O |', |
| 28 | + ' ------- '], |
| 29 | + |
| 30 | + [' ------- ', |
| 31 | + '| O O |', |
| 32 | + '| O |', |
| 33 | + '| O O |', |
| 34 | + ' ------- '], |
| 35 | + |
| 36 | + [' ------- ', |
| 37 | + '| O O |', |
| 38 | + '| O O |', |
| 39 | + '| O O |', |
| 40 | + ' ------- '] |
| 41 | + |
| 42 | + |
| 43 | +] |
| 44 | + |
| 45 | +def roll_dice(): |
| 46 | + return random.randint(1, 6) |
| 47 | + |
| 48 | +def print_dice(dice_value): |
| 49 | + for line in dice_faces[dice_value - 1]: |
| 50 | + print(line) |
| 51 | + |
| 52 | +def print_two_dice(dice_values): |
| 53 | + for i in range(5): |
| 54 | + print(dice_faces[dice_values[0] - 1][i], " ", dice_faces[dice_values[1] - 1][i]) |
| 55 | + |
| 56 | +def play_game(): |
| 57 | + level = input("Select level (easy/difficult): ").lower() |
| 58 | + |
| 59 | + if level == "easy": |
| 60 | + actual_roll = roll_dice() |
| 61 | + print("Guess the outcome of a dice roll (1 to 6).") |
| 62 | + elif level == "difficult": |
| 63 | + actual_rolls = [roll_dice(), roll_dice()] |
| 64 | + actual_roll = sum(actual_rolls) |
| 65 | + print("Guess the sum of two dice rolls (2 to 12).") |
| 66 | + else: |
| 67 | + print("Invalid level choice.") |
| 68 | + return |
| 69 | + |
| 70 | + try: |
| 71 | + user_guess = int(input()) |
| 72 | + if (1 <= user_guess <= 6 and level == "easy") or (2 <= user_guess <= 12 and level == "difficult"): |
| 73 | + print("\nRolling the dice...") |
| 74 | + time.sleep(1) |
| 75 | + |
| 76 | + if level == "difficult": |
| 77 | + print_two_dice(actual_rolls) |
| 78 | + else: |
| 79 | + print_dice(actual_roll) |
| 80 | + |
| 81 | + if user_guess == actual_roll: |
| 82 | + print("Congratulations! You win!") |
| 83 | + else: |
| 84 | + print("Oops! You lose.") |
| 85 | + else: |
| 86 | + print("Invalid guess. Please enter a valid number.") |
| 87 | + except ValueError: |
| 88 | + print("Invalid input. Please enter a number.") |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + print("Welcome to the Dice Roll Guessing Game!") |
| 92 | + while True: |
| 93 | + play_game() |
| 94 | + play_again = input("Do you want to play again? (yes/no): ").lower() |
| 95 | + if play_again != "yes": |
| 96 | + break |
| 97 | + print("Thank you for playing!") |
0 commit comments