-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathslots.py
164 lines (134 loc) · 4.67 KB
/
slots.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
########################################################
#
# Slots
#
# From Basic Computer Games (1978)
#
# "The slot machine or one-arm bandit is a mechanical
# device that will absorb coins just about as fast as
# you can feed it. After inserting a coin, you pull a
# handle that sets three indepent reels spining. If the
# reels stop with certain symbols appearing in the pay
# line, you get a certain payoff. The original slot
# machine, called the Liberty bell, was invented in 1895
# by Charles Fey in San Francisco. Fey refused to sell
# or lease the manufacturing rights, so H.S. Mills in
# Chicago built a similar, but much improved, machine
# called the Operators Bell. This has survived nearly
# unchanged to today.
# On the operators Bell and other standard slot
# machines, there are 20 symbols on each wheel but they
# are not distributed evenly among the objects(cherries,
# bar, apples, etc). Of the 8000 possible combinations,
# the expected payoff(to the player) is 7049 or $89.11
# for every $100.00 put in, one of the lowest expected
# payoffs of all casino games.
# In the program here, the payoff is considerably more
# liberal; indeed it appears to favor the player by 11%
# -- i.e., an expected payoff of $111 for each $100 bet."
# The program was originally written by Fred Mirabelle
# and Bob Harper
#
########################################################
import sys
from collections import Counter
from random import choices
from typing import List
def initial_message() -> None:
print(" " * 30 + "Slots")
print(" " * 15 + "Creative Computing Morrison, New Jersey")
print("\n" * 3)
print("You are in the H&M Casino, in front of one of our")
print("one-arm Bandits. Bet from $1 to $100.")
print("To pull the arm, punch the return key after making your bet.")
def input_betting() -> int:
print("\n")
b = -1
while b < 1 or b > 100:
try:
b = int(input("Your bet:"))
except ValueError:
b = -1
if b > 100:
print("House limits are $100")
elif b < 1:
print("Minium bet is $1")
beeping()
return b
def beeping() -> None:
# Function to produce a beep sound.
# In the original program is the subroutine at line 1270
for _ in range(5):
sys.stdout.write("\a")
sys.stdout.flush()
def spin_wheels() -> List[str]:
possible_fruits = ["Bar", "Bell", "Orange", "Lemon", "Plum", "Cherry"]
wheel = choices(possible_fruits, k=3)
print(*wheel)
beeping()
return wheel
def adjust_profits(wheel: List[str], m: int, profits: int) -> int:
# we remove the duplicates
s = set(wheel)
if len(s) == 1:
# the three fruits are the same
fruit = s.pop()
if fruit == "Bar":
print("\n***Jackpot***")
profits = ((100 * m) + m) + profits
else:
print("\n**Top Dollar**")
profits = ((10 * m) + m) + profits
print("You Won!")
elif len(s) == 2:
# two fruits are equal
c = Counter(wheel)
# we get the fruit that appears two times
fruit = sorted(c.items(), key=lambda x: x[1], reverse=True)[0][0]
if fruit == "Bar":
print("\n*Double Bar*")
profits = ((5 * m) + m) + profits
else:
print("\nDouble!!")
profits = ((2 * m) + m) + profits
print("You Won!")
else:
# three different fruits
print("\nYou Lost.")
profits -= m
return profits
def final_message(profits: int) -> None:
if profits < 0:
print("Pay up! Please leave your money on the terminal")
elif profits == 0:
print("Hey, You broke even.")
else:
print("Collect your winings from the H&M cashier.")
def main() -> None:
profits = 0
keep_betting = True
initial_message()
while keep_betting:
m = input_betting()
w = spin_wheels()
profits = adjust_profits(w, m, profits)
print(f"Your standings are ${profits}")
answer = input("Again?")
try:
if answer[0].lower() != "y":
keep_betting = False
except IndexError:
keep_betting = False
final_message(profits)
if __name__ == "__main__":
main()
######################################################################
#
# Porting notes
#
# The selections of the fruits(Bar, apples, lemon, etc.) are made
# with equal probability, accordingly to random.choices documentation.
# It could be added a weights list to the function and therefore
# adjust the expected payoff
#
######################################################################