├── README.md └── Slot_Machine_Python └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # Python Slot Machine 2 | 3 | Get ready for the thrill of the spin with this classic slot machine game built using Python. Immerse yourself in a world of chance and see if you have the luck to win big. With its user-friendly interface and easy-to-use controls, this game will have you feeling like a high-roller in no time. So put on your lucky charm and let the reels roll! Download now or fork and give a star to experience the excitement of the casino right from your computer. 4 | 5 | ## Game in the terminal 6 | ![Game_In_Terminal](https://i.imgur.com/NqaMzvX.png) 7 | 8 | # Tech Stack 9 | > Python 10 | 11 | - Developed by [Andrew Tsegaye](github.com/Andrew-Tsegaye) 12 | -------------------------------------------------------------------------------- /Slot_Machine_Python/main.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | MAX_LINES = 3 4 | MAX_BET = 100 5 | MIN_BET = 1 6 | 7 | ROWS = 3 8 | COLS = 3 9 | 10 | symbol_count = { 11 | "A": 2, 12 | "B": 4, 13 | "C": 6, 14 | "D": 8 15 | } 16 | 17 | symbol_value = { 18 | "A": 5, 19 | "B": 4, 20 | "C": 3, 21 | "D": 2 22 | } 23 | 24 | 25 | def check_winnings(columns, lines, bet, values): 26 | winnings = 0 27 | winning_lines = [] 28 | for line in range(lines): 29 | symbol = columns[0][line] 30 | for column in columns: 31 | symbol_to_check = column[line] 32 | if symbol != symbol_to_check: 33 | break 34 | else: 35 | winnings += values[symbol] * bet 36 | winning_lines.append(line + 1) 37 | 38 | return winnings, winning_lines 39 | 40 | 41 | def get_slot_machine_spin(rows, cols, symbols): 42 | all_symbols = [] 43 | for symbol, symbol_count in symbols.items(): 44 | for _ in range(symbol_count): 45 | all_symbols.append(symbol) 46 | 47 | columns = [] 48 | for _ in range(cols): 49 | column = [] 50 | current_symbols = all_symbols[:] 51 | for _ in range(rows): 52 | value = random.choice(current_symbols) 53 | current_symbols.remove(value) 54 | column.append(value) 55 | 56 | columns.append(column) 57 | 58 | return columns 59 | 60 | 61 | def print_slot_machine(columns): 62 | for row in range(len(columns[0])): 63 | for i, column in enumerate(columns): 64 | if i != len(columns) - 1: 65 | print(column[row], end=" | ") 66 | else: 67 | print(column[row], end="") 68 | 69 | print() 70 | 71 | 72 | def deposit(): 73 | while True: 74 | amount = input("What would you like to deposit? $") 75 | if amount.isdigit(): 76 | amount = int(amount) 77 | if amount > 0: 78 | break 79 | else: 80 | print("Amount must be greater than 0.") 81 | else: 82 | print("Please enter a number.") 83 | 84 | return amount 85 | 86 | 87 | def get_number_of_lines(): 88 | while True: 89 | lines = input( 90 | "Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ") 91 | if lines.isdigit(): 92 | lines = int(lines) 93 | if 1 <= lines <= MAX_LINES: 94 | break 95 | else: 96 | print("Enter a valid number of lines.") 97 | else: 98 | print("Please enter a number.") 99 | 100 | return lines 101 | 102 | 103 | def get_bet(): 104 | while True: 105 | amount = input("What would you like to bet on each line? $") 106 | if amount.isdigit(): 107 | amount = int(amount) 108 | if MIN_BET <= amount <= MAX_BET: 109 | break 110 | else: 111 | print(f"Amount must be between ${MIN_BET} - ${MAX_BET}.") 112 | else: 113 | print("Please enter a number.") 114 | 115 | return amount 116 | 117 | 118 | def spin(balance): 119 | lines = get_number_of_lines() 120 | while True: 121 | bet = get_bet() 122 | total_bet = bet * lines 123 | 124 | if total_bet > balance: 125 | print( 126 | f"You do not have enough to bet that amount, your current balance is: ${balance}") 127 | else: 128 | break 129 | 130 | print( 131 | f"You are betting ${bet} on {lines} lines. Total bet is equal to: ${total_bet}") 132 | 133 | slots = get_slot_machine_spin(ROWS, COLS, symbol_count) 134 | print_slot_machine(slots) 135 | winnings, winning_lines = check_winnings(slots, lines, bet, symbol_value) 136 | print(f"You won ${winnings}.") 137 | print(f"You won on lines:", *winning_lines) 138 | return winnings - total_bet 139 | 140 | 141 | def main(): 142 | balance = deposit() 143 | while True: 144 | print(f"Current balance is ${balance}") 145 | answer = input("Press enter to play (q to quit).") 146 | if answer == "q": 147 | break 148 | balance += spin(balance) 149 | 150 | print(f"You left with ${balance}") 151 | 152 | 153 | main() --------------------------------------------------------------------------------