Skip to content

Commit 7939403

Browse files
Merge pull request #2292 from khushimarothi/slotmachine
New Python Script Added
2 parents 3c38e9a + 3768f69 commit 7939403

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

Slot-Machine-Game/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Slot Machine Game
2+
3+
This is a simple command-line slot machine game written in Python. The game allows you to bet on multiple lines and spin the slot machine. The game has predefined symbols with corresponding values. If the symbols on a line match, you win money based on the symbol's value and your bet amount.
4+
5+
## How to Play
6+
7+
1. Run the `slot_machine.py` script.
8+
2. You will be prompted to deposit an initial amount of money.
9+
3. Enter the number of lines you want to bet on (between 1 and the maximum allowed lines).
10+
4. Specify the bet amount for each line (between the minimum and maximum allowed bets).
11+
5. The slot machine will be displayed, showing the randomized symbols in a 3x3 grid.
12+
6. If you win, the game will display the amount you won and the line(s) on which you won.
13+
7. The game will then prompt you for another spin until you decide to quit by pressing 'q'.
14+
8. Once you quit the game, it will display the final balance you have left.
15+
16+
## Symbols and Values
17+
18+
The slot machine has four predefined symbols: A, B, C, and D. Each symbol has a specific count and value associated with it, which determines the winnings.
19+
20+
Symbol Count:
21+
- A: 2
22+
- B: 4
23+
- C: 6
24+
- D: 8
25+
26+
Symbol Value:
27+
- A: $5
28+
- B: $4
29+
- C: $3
30+
- D: $2
31+
32+
## Betting Rules
33+
34+
- You can bet on a minimum of 1 line and a maximum of 3 lines.
35+
- The bet amount per line must be between $1 and $100.
36+
37+
## Notes
38+
39+
- This is a simple simulation of a slot machine for entertainment purposes only.
40+
- The game uses Python's `random` module to generate random symbols for each spin.
41+
42+
Have fun playing the slot machine game!
43+
44+
## Author
45+
[Khushi Marothi](https://github.com/khushimarothi)

Slot-Machine-Game/slotmachine.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import random
2+
MAX_LINES = 3
3+
MAX_BET = 100
4+
MIN_BET = 1
5+
6+
ROWS = 3
7+
COLS = 3
8+
9+
symbol_count = {
10+
"A": 2,
11+
"B": 4,
12+
"C": 6,
13+
"D": 8
14+
}
15+
16+
symbol_value = {
17+
"A": 5,
18+
"B": 4,
19+
"C": 3,
20+
"D": 2
21+
}
22+
23+
def check_winnings(columns, lines, bet, values):
24+
winnings = 0
25+
winning_lines = []
26+
for line in range(lines):
27+
symbol = columns[0][line]
28+
for column in columns:
29+
symbol_to_check = column[line]
30+
if symbol != symbol_to_check:
31+
break
32+
else:
33+
winnings +=values[symbol]* bet
34+
winning_lines.append(line + 1)
35+
36+
return winnings, winning_lines
37+
38+
def get_slot_machine_spin(rows,cols, symbols):
39+
all_symbols = []
40+
for symbol, symbol_count in symbols.items():
41+
for _ in range(symbol_count):
42+
all_symbols.append(symbol)
43+
44+
columns = []
45+
for _ in range(cols):
46+
column = []
47+
current_symbols = all_symbols[:]
48+
for _ in range(rows):
49+
value = random.choice(all_symbols)
50+
current_symbols.remove(value)
51+
column.append(value)
52+
53+
columns.append(column)
54+
55+
return columns
56+
57+
def print_slot_machine(columns):
58+
for row in range(len(columns[0])):
59+
for i, column in enumerate(columns):
60+
if i!= len(columns)-1:
61+
print(column[row], end=" | ")
62+
else:
63+
print(column[row], end="")
64+
65+
print()
66+
67+
def deposit():
68+
while True:
69+
amount = input("What would you like to deposit? $")
70+
if amount.isdigit():
71+
amount = int(amount)
72+
if amount > 0:
73+
break
74+
else:
75+
print("Amount must be greater than 0.")
76+
else:
77+
print("please enter a number. ")
78+
79+
return amount
80+
81+
def get_number_of_lines():
82+
while True:
83+
lines = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
84+
if lines.isdigit():
85+
lines = int(lines)
86+
if 1<= lines <= MAX_LINES:
87+
break
88+
else:
89+
print("enter a valid number of lines.")
90+
else:
91+
print("please enter a number. ")
92+
93+
return lines
94+
95+
def get_bet():
96+
while True:
97+
amount = input("What would you like to bet on each line? $ ")
98+
if amount.isdigit():
99+
amount = int(amount)
100+
if MIN_BET <= amount <= MAX_BET:
101+
break
102+
else:
103+
print(f"Amount must be between ${MIN_BET} - ${MAX_BET}.")
104+
else:
105+
print("please enter a number. ")
106+
107+
return amount
108+
109+
def spin(balance):
110+
lines = get_number_of_lines()
111+
while True:
112+
bet = get_bet()
113+
total_bet = bet*lines
114+
115+
if total_bet > balance:
116+
print(f"You do not have enough money to bet that amount, your current balance is: ${balance}.")
117+
118+
else:
119+
break
120+
121+
print(f"You are betting ${bet} on {lines} lines. Total be is equal to: ${total_bet}")
122+
123+
slots = get_slot_machine_spin(ROWS, COLS, symbol_count)
124+
print_slot_machine(slots)
125+
winnings, winning_lines = check_winnings(slots, lines, bet, symbol_value)
126+
print(f"You won ${winnings}.")
127+
print(f"You won on lines: ", *winning_lines)
128+
return winnings - total_bet
129+
130+
def main():
131+
balance = deposit()
132+
while True:
133+
print(f"Current balance is ${balance}")
134+
answer = input("Press enter to play (q to quit).")
135+
if answer == "q":
136+
break
137+
balance += spin(balance)
138+
139+
print(f"You left with ${balance}")
140+
141+
main()

0 commit comments

Comments
 (0)