-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroll_dice
83 lines (72 loc) · 1.71 KB
/
roll_dice
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
#!/bin/bash
# Global variable to keep track of the grand total
GRAND_TOTAL=0
# Function to roll a dice of a specified number of sides
roll_dice() {
local sides=$1
echo $((RANDOM % sides + 1))
}
# Function to roll until a 1 is rolled
roll_until_one() {
local sides=$1
local total=0
local roll=0
echo "Rolling a d$sides until a 1 is rolled..."
while true; do
roll=$(roll_dice "$sides")
if [[ $roll -eq 1 ]]; then
echo "Rolled: 1, stopping. Final Total: $total"
break
fi
total=$((total + roll))
echo "Rolled: $roll, Total: $total"
done
GRAND_TOTAL=$((GRAND_TOTAL + total))
}
# Function to reset the grand total
reset_grand_total() {
GRAND_TOTAL=0
echo "Grand total has been reset."
}
# Function to show the current grand total
show_grand_total() {
echo "Current Grand Total: $GRAND_TOTAL"
}
# Main menu function
dice_menu() {
while true; do
clear # Clears the screen
echo -e "\n=== Dice Rolling Menu ==="
echo "1. Roll a dice"
echo "2. Show Grand Total"
echo "3. Reset Grand Total"
echo "4. Exit"
echo "========================="
read -rp "Choose an option (1-4): " choice
case $choice in
1)
read -rp "Enter the number of sides on the dice: " sides
if [[ $sides =~ ^[0-9]+$ ]]; then
roll_until_one "$sides"
else
echo "Invalid input. Please enter a positive integer."
fi
;;
2)
show_grand_total
;;
3)
reset_grand_total
;;
4)
echo "Goodbye!"
break
;;
*)
echo "Invalid choice. Please select an option from 1 to 4."
;;
esac
# Pause before showing the menu again
pressanykey
done
}