DEV Community

Akhil Athuluri
Akhil Athuluri

Posted on

Learn 7 Python Concepts From One Code

The program is an implementation of a slot machine game that allows the user to deposit money and play multiple rounds. The game involves spinning a slot machine and checking if the symbols in the machine match up in a specific pattern.

The following concepts are used in this program:

  • Input/output: The program prompts the user for input to deposit money and place bets, and displays the results of each spin.
  • Variables: The program uses variables to keep track of the game state, such as the player's balance and the symbols and their values.
  • Data structures: The program uses a list to represent the columns in the slot machine and a dictionary to store the count and value of each symbol.
  • Functions: The program defines several functions, including check_winnings, get_slot_machine_spin, print_slot_machine, deposit, get_number_of_lines, get_bet, and spin. These functions help to organize the program and perform specific tasks.
  • Control flow: The program uses loops and conditional statements to control the flow of the game and ensure that the user inputs valid values.
  • Randomness: The program uses the random module to randomly select symbols for each column in the slot machine.
import random

MAX_LINES = 3
MAX_BET = 100
MIN_BET = 1

ROWS = 3
COLS = 3

symbol_count  ={
    "A" : 2,
    "B" : 4,
    "C" : 6,
    "D" : 8
}

symbol_values  ={
    "A" : 5,
    "B" : 4,
    "C" : 3,
    "D" : 2
}

def check_winnings(columns, lines, bet, values):
    winnings = 0
    winning_lines = []
    for line in range(lines):
        symbol = columns[0][line]
        for column in columns:
            symbol_to_check = column[line]
            if symbol != symbol_to_check:
                break
        else:
            winnings += values[symbol] * bet
            winning_lines.append(line + 1)

    return winnings, winning_lines


def get_slot_machine_spin(rows, cols, symbols):
    all_symbols = []
    for symbol, symbol_count in symbols.items():
        for _ in range(symbol_count):
            all_symbols.append(symbol)

    columns = []
    for _ in range(cols):
        column = []
        current_symbols = all_symbols[:]
        for _ in range(rows):
            value = random.choice(current_symbols)
            current_symbols.remove(value)
            column.append(value)

        columns.append(column)
    return columns


def print_slot_machine(columns):
    for row in range(len(columns[0])):
        for i, column in enumerate(columns):
            if i != len(columns) - 1:
                print(column[row], end=" | ")
            else:
                print(column[row], end="")
        print()


def deposit():
    while True:
        amount = input("What Would You Like To Deposit? $")
        if amount.isdigit():
            amount = int(amount)
            if amount > 0:
                break
            else:
                print("Amount must be grater than 0.")
        else:
            print("Please Enter A Number: ")
    return amount

def get_number_of_lines():
    while True:
        lines = input("Enter The No.Of Lines To Bet On(1-"+str(MAX_LINES)+")? ")
        if lines.isdigit():
            lines = int(lines)
            if 1 <= lines <= MAX_LINES:
                break
            else:
                print("Enter A Valid Number Of Lines.")
        else:
            print("Please Enter A Number: ")
    return lines

def get_bet():
    while True:
        amount = input("What Would You Like To Bet On Each Line? $")
        if amount.isdigit():
            amount = int(amount)
            if MIN_BET <= amount <= MAX_BET:
                break
            else:
                print(f"Amount must be between {MIN_BET} - {MAX_BET}.")
        else:
            print("Please Enter A Number: ")
    return amount

def spin(balance):
    lines = get_number_of_lines()
    while True:
        bet = get_bet()
        total_bet = bet * lines

        if total_bet > balance:
            print(f"You don;t have enough to bet that amount, your current balance is: ${balance}")
        else:
            break
    print(f"You're Betting ${bet} on {lines} lines. Total Bet Is Equal To: ${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

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 += spin(balance)

    print(f"You Left With ${balance}")
main()
Enter fullscreen mode Exit fullscreen mode

If Code Is Not Working Correctly Ping Me

Slot Machine Game Documentation

Game Overview

This is a simple slot machine game where players can place bets on multiple lines and spin the slot machine. The game is played on a 3x3 grid with symbols randomly generated each time the player spins. If the symbols match on a winning line, the player will win money based on the value of the symbols and their bet.

Game Rules

  1. The player must deposit money to play the game.
  2. The player can bet on up to 3 lines, and can adjust their bet per line.
  3. The player must have enough balance to place the bet.
  4. The player spins the slot machine to generate random symbols.
  5. If the symbols match on a winning line, the player will win money based on the value of the symbols and their bet.
  6. The game ends when the player chooses to quit or when their balance is 0.

Code Overview

  1. check_winnings(columns, lines, bet, values) - This function checks if there are any winning lines in the provided columns. If there is a winning line, it calculates the winnings based on the value of the symbols and the player's bet.

  2. get_slot_machine_spin(rows, cols, symbols) - This function generates a 3x3 grid of symbols based on the provided symbols and their respective counts.

  3. print_slot_machine(columns) - This function prints the provided columns in a slot machine-like format.

  4. deposit() - This function prompts the player to deposit money and returns the amount as an integer.

  5. get_number_of_lines() - This function prompts the player to select the number of lines they want to bet on and returns the selected number as an integer.

  6. get_bet() - This function prompts the player to select their bet amount and returns the selected amount as an integer.

  7. spin(balance) - This function executes a single spin of the slot machine based on the player's input. It generates the slot machine symbols, prints the slot machine, checks for winning lines, calculates the winnings, and returns the difference between the winnings and the total bet.

  8. main() - This function runs the main game loop. It prompts the player to deposit money, allows the player to spin the slot machine, updates the player's balance, and ends the game when the player chooses to quit or their balance is 0.

Game Configuration

  • MAX_LINES - The maximum number of lines the player can bet on.
  • MAX_BET - The maximum amount the player can bet per line.
  • MIN_BET - The minimum amount the player can bet per line.
  • ROWS - The number of rows in the slot machine.
  • COLS - The number of columns in the slot machine.
  • symbol_count - A dictionary of symbol names and their respective counts in the game.
  • symbol_values - A dictionary of symbol names and their respective values in the game.

Thank You
Akhil

Top comments (0)