Python老虎机:计算线路支付

问题描述 投票:2回答:1

我是一个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矩阵。有没有更好的方法来处理“如果前3个符号匹配”部分,因为我将不得不重复4个符号和5个符号?由于每一行只有一次支付,我将从5符号支付开始,然后向下工作2.我从3开始,因为它是最常见的并且最容易测试。我还必须考虑一个狂野的等于任何符号,我还没有尝试解决。同样地,存在分散支付(意味着如果在矩阵中的任何地方有X个Scatter符号,则会得到支付。这部分将很容易)。还有一个奖金游戏,我将在稍后处理:

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都未定义。我认为我可以在全球范围内定义它们吗?

python
1个回答
1
投票

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

def PayTable(i,wager,symbolCount,symbol):    
    global TotalWin
    global Message
© www.soinside.com 2019 - 2024. All rights reserved.