|
| 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