extensionhelp
extensionhelp

Reputation: 574

Python Slot Machine: Calculating Line Payouts

I'm a Python newbie attempting to create a slot machine simulator that mimics the payouts of the real machines. I'm running into an issue in calculating the line payouts, and I'm sure there's a smarter way of iterating through the lines and calculating them.

Defining some constants that I'll be using:

SymbolMap = ["Boats","Bonus","Buoys","Clams","Light Houses","Lobsters","Scatter","Seagulls","Starfish","Tuna","Wilds"]

#The ints in reels below are a simpler way to express the SymbolMap above

Reels = [[9,8,3,4,6,3,8,1,5,6,2,3,8,2,3,8,5,4,3,10,7,8,10,1,3,0,8,9,3,8,9,5,3,8,0,4,3,8,0,9,2,7,5,3,8,0,7],
        [3,2,4,3,2,4,1,7,3,0,7,9,0,1,8,7,10,1,7,4,5,10,2,3,1,7,3,6,5,9,7,6,8,3,0,5,7,3,1,8,7,2,4,3,9,7,0],
        [0,8,3,1,4,0,5,8,1,4,8,1,9,8,3,7,8,10,1,4,7,8,9,3,0,9,8,1,9,4,8,6,4,5,7,8,6,2,9,5,1,8,4,7,2,0,9],
        [7,9,2,7,6,2,8,7,9,10,2,9,8,5,7,9,10,5,4,2,7,0,3,8,4,7,0,3,2,7,0,4,8,9,7,2,8,3,2,7,8,3,5,10,2,7,8],
        [3,10,0,5,2,8,4,9,8,4,7,10,9,2,0,3,9,2,8,3,6,2,8,9,3,2,0,4,9,5,4,7,3,5,8,0,4,9,7,8,4,3,5,7,8,3,7]]


# Lines are the row to look for on each reel. i.e. Lines[0] is a straight line of the 2nd row. 
# Lines[3] starts in top left corner of matrix, and forms inverted V shape.

Lines = [[1,1,1,1,1],
        [0,0,0,0,0],
        [2,2,2,2,2],
        [0,1,2,1,0],
        [2,1,0,1,2],
        [2,2,1,0,0],
        [0,0,1,2,2],
        [1,2,1,0,1],
        [1,0,1,2,1],
        [2,1,1,1,0],
        [0,1,1,1,2],
        [1,2,2,1,0],
        [1,0,0,1,2],
        [1,1,2,1,0],
        [1,1,0,1,2]]

#Payouts are how many credits won for symbols in a row. For example, Symbols[0] is Boats. 
#2 boats is 0 credits, 3 boats is 25 credits, 4 boats is 100 credits, 5 boats is 500 credits.
#They must be continuous and from left to right. I.e. BOAT-BOAT-CLAM-BOAT-BOAT on a payline wins 0. 
#Similarly, CLAM-CLAM-BOAT-BOAT-BOAT wins 0.

Payouts = [[0,25,100,500],
          [0,0,0,0],
          [0,25,100,500],
          [0,5,30,200]]

#Initializing a 3X5 matrix to represent reels

SpinValues = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]

#Initializing message
Message = ''

#Initializing TotalWin
TotalWin = 0

Spin logic, which properly generates a 3X5 matrix of random numbers. Is there a better way to handle the "if first 3 symbols match" portion, since I'll have to repeat again for 4 symbols and 5 symbols? Since each line is only entitled to one payout, I'll start with the 5 symbol payouts, then work down toward 2. I started with 3 because it's the most common and will be easiest to test. I'll also have to account for a wild equaling any symbol, which I haven't tried to tackle yet. Likewise, there is a scatter pay (meaning if you have X number of Scatter symbols anywhere in the matrix, you get a payout. That part will be easy). There is also a bonus game, which I'll be working on later:

def spin(linesPlayed, wager):
    for i, object in enumerate(Reels):
        length = len(Reels[i])
        StopValue = random.randint(0,length-1)
        SpinValues[1][i] = Reels[i][StopValue]
        if StopValue == 0:
            SpinValues[0][i] = Reels[i][-1]
        else:
            SpinValues[0][i] = Reels[i][StopValue - 1]
        if StopValue == len(Reels[i])-1:
            SpinValues[2][i] = Reels[i][0]
        else:
            SpinValues[2][i] = Reels[i][StopValue +1]
    print(SpinValues[0])
    print("\n")
    print(SpinValues[1])
    print("\n")
    print(SpinValues[2])

    for i in range(linesPlayed):
        #if first 3 symbols match
        if SpinValues[Lines[i][0]] == SpinValues[Lines[i][1]] == SpinValues[Lines[i][2]]:
            PayTable(i,wager,3,SpinValues[Lines[i][0]])
        #if first 4 symbols match
        #if first 5 symbols match
        #handle scatter pay
        #wilds?
        #handle bonus trigger 

Handling wins:

def PayTable(i,wager,symbolCount,symbol):
    LineWin = Payouts[symbol][symbolCount] * wager
    TotalWin += Payouts[symbol][symbolCount] * wager
    Message += "Line " + str(i) +" wins " + str(LineWin) + " credits with " + str(symbolCount) + " " + SymbolMap[symbol] + "!" + "\n"

I'm getting the error that both TotalWin and Message are undefined. I thought that I could defined them globally up top?

Upvotes: 2

Views: 781

Answers (1)

Jonathan
Jonathan

Reputation: 284

You need to use the global keyword in each function to access variables defined in a parent. For example:

def PayTable(i,wager,symbolCount,symbol):    
    global TotalWin
    global Message

Upvotes: 1

Related Questions