Skip to content

Commit 9dc224d

Browse files
Merge pull request #2525 from smty2018/die
dice roll guessing game(terminal)
2 parents 39e6c60 + 5693a8e commit 9dc224d

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

Die Roll Guessing Game/ReadMe.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Dice Roll Guessing Game
2+
3+
This simple game challenges you to guess the outcome of a dice roll or the sum of two dice rolls. Roll the dice, make your guess, and see if you can predict the outcome!
4+
5+
## How to Play
6+
7+
1. Run the `dice_roll_game.py` script using a Python interpreter.
8+
2. Choose a game level:
9+
- "easy": Guess the outcome of a single dice roll (1 to 6).
10+
- "difficult": Guess the sum of two dice rolls (2 to 12).
11+
3. Make your guess by entering a number within the valid range.
12+
4. Watch the dice rolls and see if your guess matches the actual outcome.
13+
5. The game will reveal whether you win or lose based on your guess.
14+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)