-
Notifications
You must be signed in to change notification settings - Fork 0
/
slot_game.py
145 lines (128 loc) · 4.57 KB
/
slot_game.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
import random
#You can change these values
MAX_LINES = 3
MAX_BET = 100
MIN_BET = 1
ROWS = 3
COLS = 3
#This decides how many times a symbol can appear in a coloumn
symbol_count = {
"A": 2,
"B": 4,
"C": 6,
"D": 8
}
#These are the points alloted to each symbol
symbol_values = {
"A": 5,
"B": 4,
"C": 3,
"D": 2
}
#This is for the spin
def spin(balance):
lines = get_number_of_lines()
while True:
bet = get_bet()
total_bet = bet*lines
if total_bet > balance:
print(f"You do not have enough balance to bet. Your current balance is {balance}")
else:
break
print(f"You bet ${bet} on {lines} lines. Total bet is {total_bet}")
slots = get_slot_machine_spin(ROWS, COLS, symbol_count)
print_slot_machine(slots)
winnings, winning_lines = check_winnings(slots, lines, bet, symbol_values)
print(f"You won {winnings}")
print(f"You won on lines: ", *winning_lines)
return winnings - total_bet
#This is the math behind calculating the winnings
def check_winnings(coloumns, lines, bet, values):
winnings = 0
winning_lines = []
for line in range(lines):
symbol = coloumns[0][line]
for coloumn in coloumns:
symbol_to_check = coloumn[line] #This takes the first symbol in every row
if symbol != symbol_to_check:
break
else:
winnings = winnings + values[symbol] * bet #This takes the help of symbol_values dictionary to allocate points
winning_lines.append(line + 1)
if winning_lines == []:
winning_lines = ["None"]
return winnings, winning_lines
#This gets the symbols ready in all the coloumns for the spin
def get_slot_machine_spin(rows, cols, symbols):
all_symbols = []
for symbol, symbol_count in symbols.items(): #Using the .items method in dictionaries we get both symbol and the count associated with it
for _ in range (symbol_count):
all_symbols.append(symbol) #This prepares the all_symbols list
coloumns = []
for _ in range(cols): #This is for every coloumn in the spin. _ is used instead of a variable to save up space
coloumn = []
current_symbols = all_symbols[:] #making a duplicate of all_symbols
for _ in range(rows): #This is for every row in that particular coloumn
value = random.choice(current_symbols) #Using random function
current_symbols.remove(value) #This prevents duplicates
coloumn.append(value)
coloumns.append(coloumn) #This adds one coloumn to the nested list "coloumns"
return coloumns
#Printing the actual slot machine
def print_slot_machine(coloumns):
for row in range(len(coloumns[0])): #This is because all the coloumns are of the same length
for i,coloumn in enumerate(coloumns):
if i == len(coloumns)-1:
print(coloumn[row], end=" ") #This prints the values of each coloumn in a given row
else:
print(coloumn[row], end=" | ")
print()
#This prompt asks the player for the number of lines to bet on
def get_number_of_lines():
while True:
lines = input("How many lines do you wanna bet on (" + str(MAX_LINES) + ")?")
if lines.isdigit():
lines = int(lines)
if 1 <= lines <= MAX_LINES:
break
else:
print("Lines must be between 0 and "+ str(MAX_LINES) +"!!")
else:
print("Enter a number")
return lines
#This prompt asks the player for the bet amount
def get_bet():
while True:
bet = input("How much would you like to bet on each line? $$ ")
if bet.isdigit():
bet = int(bet)
if MIN_BET <= bet <= MAX_BET:
break
else:
print(f"Amount must be between ${MIN_BET} - ${MAX_BET}")
else:
print("Enter valid amount")
return bet
#This is the first prompt asking for the deposit
def deposit():
while True:
amount = input("What would like to deposit? $$ ")
if amount.isdigit():
amount = int(amount)
if amount > 0:
break
else:
print("Amount must be greater than 0!!")
else:
print("Enter valid amount")
return amount
def main():
balance = deposit()
while True:
print(f"Current Balance is ${balance}")
answer = input("Press ENTER to play (q to quit)")
if answer == "q":
break
balance = balance + spin(balance)
print(f"You left with {balance}")
main()