Skip to content

Commit 40f4cd2

Browse files
Merge pull request #3216 from Bhunesh386/feature/dice-roller
Add interactive dice_roller.py implementation (Closes #3202)
2 parents 9022435 + 6f3b57a commit 40f4cd2

1 file changed

Lines changed: 59 additions & 54 deletions

File tree

dice_roller.py

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,62 @@
11
import random
22

33

4-
dice_art = {
5-
1: ("┌─────────┐",
6-
"│ │",
7-
"│ ● │",
8-
"│ │",
9-
"└─────────┘"),
10-
2: ("┌─────────┐",
11-
"│ ● │",
12-
"│ │",
13-
"│ ● │",
14-
"└─────────┘"),
15-
3: ("┌─────────┐",
16-
"│ ● │",
17-
"│ ● │",
18-
"│ ● │",
19-
"└─────────┘"),
20-
4: ("┌─────────┐",
21-
"│ ● ● │",
22-
"│ │",
23-
"│ ● ● │",
24-
"└─────────┘"),
25-
5: ("┌─────────┐",
26-
"│ ● ● │",
27-
"│ ● │",
28-
"│ ● ● │",
29-
"└─────────┘"),
30-
6: ("┌─────────┐",
31-
"│ ● ● │",
32-
"│ ● ● │",
33-
"│ ● ● │",
34-
"└─────────┘")
35-
}
36-
37-
dice = []
38-
total = 0
39-
num_of_dice = int(input("How many dice?: "))
40-
41-
for die in range(num_of_dice):
42-
dice.append(random.randint(1, 6))
43-
44-
# PRINT VERTICALLY
45-
# for die in range(num_of_dice):
46-
# for line in dice_art.get(dice[die]):
47-
# print(line)
48-
49-
# PRINT HORIZONTALLY
50-
for line in range(5):
51-
for die in dice:
52-
print(dice_art.get(die)[line], end="")
53-
print()
54-
55-
for die in dice:
56-
total += die
57-
print(f"total: {total}")
4+
def get_die_art(value):
5+
"""Returns a list of 5 strings representing the box-drawing art
6+
7+
for a given die face value (1-6).
8+
"""
9+
faces = {
10+
1: ["┌───────┐", "│ │", "│ ● │", "│ │", "└───────┘"],
11+
2: ["┌───────┐", "│ ● │", "│ │", "│ ● │", "└───────┘"],
12+
3: ["┌───────┐", "│ ● │", "│ ● │", "│ ● │", "└───────┘"],
13+
4: ["┌───────┐", "│ ● ● │", "│ │", "│ ● ● │", "└───────┘"],
14+
5: ["┌───────┐", "│ ● ● │", "│ ● │", "│ ● ● │", "└───────┘"],
15+
6: ["┌───────┐", "│ ● ● │", "│ ● ● │", "│ ● ● │", "└───────┘"],
16+
}
17+
return faces.get(value, faces[1])
18+
19+
20+
def main():
21+
print("========================================")
22+
print(" Interactive Dice Roller ")
23+
print("========================================")
24+
25+
# Get valid user input for the number of dice
26+
while True:
27+
try:
28+
num_dice = int(input("How many dice would you like to roll? (1-10): "))
29+
if 1 <= num_dice <= 10:
30+
break
31+
print("Please choose a number between 1 and 10.")
32+
except ValueError:
33+
print("Invalid input. Please enter a valid integer.")
34+
35+
# Generate random values for each die
36+
rolls = [random.randint(1, 6) for _ in range(num_dice)]
37+
38+
print(f"\nRolling {num_dice} dice...")
39+
40+
# Retrieve ASCII art for each rolled die
41+
dice_arts = [get_die_art(val) for val in rolls]
42+
43+
separator = " "
44+
die_width = len(dice_arts[0][0])
45+
divider = "~" * (num_dice * die_width + (num_dice - 1) * len(separator))
46+
print(divider)
47+
48+
# Print dice side-by-side by iterating line by line (0 to 4)
49+
for line_index in range(5):
50+
row_line = separator.join(dice_art[line_index] for dice_art in dice_arts)
51+
print(row_line)
52+
53+
print(divider)
54+
55+
# Output results and total sum
56+
print(f"Individual Rolls: {rolls}")
57+
print(f"Total Sum: {sum(rolls)}")
58+
print("========================================")
59+
60+
61+
if __name__ == "__main__":
62+
main()

0 commit comments

Comments
 (0)