-
Notifications
You must be signed in to change notification settings - Fork 402
/
dice.py
146 lines (123 loc) · 4.85 KB
/
dice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Simulate a six-sided dice roll.
Usage:
$ python dice.py
How many dice do you want to roll? [1-6] 5
~~~~~~~~~~~~~~~~~~~~~~~~~ RESULTS ~~~~~~~~~~~~~~~~~~~~~~~~~
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
│ ● ● │ │ ● │ │ ● ● │ │ ● ● │ │ │
│ ● │ │ │ │ ● │ │ ● │ │ ● │
│ ● ● │ │ ● │ │ ● ● │ │ ● ● │ │ │
└─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘
"""
import random
DICE_ART = {
1: (
"┌─────────┐",
"│ │",
"│ ● │",
"│ │",
"└─────────┘",
),
2: (
"┌─────────┐",
"│ ● │",
"│ │",
"│ ● │",
"└─────────┘",
),
3: (
"┌─────────┐",
"│ ● │",
"│ ● │",
"│ ● │",
"└─────────┘",
),
4: (
"┌─────────┐",
"│ ● ● │",
"│ │",
"│ ● ● │",
"└─────────┘",
),
5: (
"┌─────────┐",
"│ ● ● │",
"│ ● │",
"│ ● ● │",
"└─────────┘",
),
6: (
"┌─────────┐",
"│ ● ● │",
"│ ● ● │",
"│ ● ● │",
"└─────────┘",
),
}
DIE_HEIGHT = len(DICE_ART[1])
DIE_WIDTH = len(DICE_ART[1][0])
DIE_FACE_SEPARATOR = " "
def parse_input(input_string):
"""Return `input_string` as an integer between 1 and 6.
Check if `input_string` is an integer number between 1 and 6.
If so, return an integer with the same value. Otherwise, tell
the user to enter a valid number and quit the program.
"""
if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
return int(input_string)
else:
print("Please enter a number from 1 to 6.")
raise SystemExit(1)
def roll_dice(num_dice):
"""Return a list of integers with length `num_dice`.
Each integer in the returned list is a random number between
1 and 6, inclusive.
"""
roll_results = []
for _ in range(num_dice):
roll = random.randint(1, 6)
roll_results.append(roll)
return roll_results
def generate_dice_faces_diagram(dice_values):
"""Return an ASCII diagram of dice faces from `dice_values`.
The string returned contains an ASCII representation of each die.
For example, if `dice_values = [4, 1, 3, 2]` then the string
returned looks like this:
~~~~~~~~~~~~~~~~~~~ RESULTS ~~~~~~~~~~~~~~~~~~~
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
│ ● ● │ │ │ │ ● │ │ ● │
│ │ │ ● │ │ ● │ │ │
│ ● ● │ │ │ │ ● │ │ ● │
└─────────┘ └─────────┘ └─────────┘ └─────────┘
"""
dice_faces = _get_dice_faces(dice_values)
dice_faces_rows = _generate_dice_faces_rows(dice_faces)
# Generate header with the word "RESULTS" centered
width = len(dice_faces_rows[0])
diagram_header = " RESULTS ".center(width, "~")
dice_faces_diagram = "\n".join([diagram_header] + dice_faces_rows)
return dice_faces_diagram
def _get_dice_faces(dice_values):
dice_faces = []
for value in dice_values:
dice_faces.append(DICE_ART[value])
return dice_faces
def _generate_dice_faces_rows(dice_faces):
dice_faces_rows = []
for row_idx in range(DIE_HEIGHT):
row_components = []
for die in dice_faces:
row_components.append(die[row_idx])
row_string = DIE_FACE_SEPARATOR.join(row_components)
dice_faces_rows.append(row_string)
return dice_faces_rows
# ~~~ App's main code block ~~~
# 1. Get and validate user's input
num_dice_input = input("How many dice do you want to roll? [1-6] ")
num_dice = parse_input(num_dice_input)
# 2. Roll the dice
roll_results = roll_dice(num_dice)
# 3. Generate the ASCII diagram of dice faces
dice_face_diagram = generate_dice_faces_diagram(roll_results)
# 4. Display the diagram
print(f"\n{dice_face_diagram}")