Python老虎机:计算线路支出

时间:2019-03-13 15:04:22

标签: python

我是一名Python新手,试图创建一个模拟真实机器支出的老虎机模拟器。我在计算线支出时遇到了一个问题,并且我确定有一种更智能的方法来遍历线并进行计算。

定义一些我将要使用的常量:

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

自旋逻辑,可正确生成3X5随机数矩阵。 是否有更好的方法来处理“如果前三个符号匹配”部分,因为我将不得不再次重复4个符号和5个符号?由于每行仅有权获得一次付款,因此我我将从5个符号的支出开始,然后向下推至2。我从3开始,因为它是最常见的,并且最容易测试。我还必须考虑任何符号的通配符,我尚未尝试解决。同样,有一个分散支付(这意味着,如果矩阵中任何地方都有X个分散符号,那么您将得到支付。这部分很容易)。还有一个奖励游戏,稍后我将进行研究:

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 

处理胜出:

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"

我收到以下错误消息:TotalWin和Message均未定义。我以为可以在全局范围内对它们进行定义?

1 个答案:

答案 0 :(得分:1)

您需要在每个函数中使用global关键字来访问在父级中定义的变量。 例如:

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