当我需要多次运行sim调用函数时,避免退出代码-1073741571(0xc00000fd)

时间:2020-08-16 20:23:39

标签: python

所以我刚刚在pycharm中编程了一种二十一点模拟卡计数。目标是使用完美的基本策略和数卡来进行二十一点的二十一点操作,并根据用户的资金状况进行赌注,并下注散布其损失所有金钱的风险。问题是,为了做到这一点,我需要在二十一点的虚拟手中进行多次操作,直到他们失去资金或以设定的数量获利。他们每次玩直到失败或获胜的每次迭代都由NumberOfSims范围内的n进行处理。当玩家最终损失全部金钱或利润x时,将转到函数simwin()或simlost(),在该函数中循环停止,NumberOfSims范围内n中的下一个值重置资金并循环功能,直到再次获得到simwin()或simlost()。我得到最大的递归错误,并使用更高的设置递归限制。然后我得到了错误:python进程完成,退出代码为-1073741571(0xc00000fd)。我发现了这个Process finished with exit code -1073741571,其中提到了更改线程大小的方法,但是我对此还是有点陌生​​,不知道它是否适用于我的情况以及如何使用它。

这是我代码的基本版本。

import random
import math
import sys
deck = z = [2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,
            10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
numberOfSims = 5 # This in the real code is user input
profit = 100 # User input
sys.setrecursionlimit(20000)
bet = 10
bankroll = 100 # User Input
ogbankroll = 100 # User input
simlosses = 0
simwins = 0
def start():
    for n in range(numberOfSims):
        playgame()
    print("Sim wins: ", simwins)
    print("Sim losses: ", simlosses)

def playgame():
    global deck
    global numberOfSims
    global profit
    global bet
    global bankroll

    random.shuffle(deck)
    random.shuffle(deck)
    random.shuffle(deck)
    if bankroll == ogbankroll + profit:
        simwin()
    elif bankroll <= 0:
        simlost()
    else:
        dealerhand = [deck[0], deck[1]]
        playerhand = [deck[2], deck[3]]
        if dealerhand[0] + dealerhand[1] == playerhand[0] + playerhand[1]:
            #Push
            playgame()
        elif dealerhand[0] + dealerhand[1] > playerhand[0] + playerhand[1]:
            # Lost
            bankroll = bankroll - bet
            playgame()
        else: # This else means player wins
            # dealerhand[0] + dealerhand[1] < playerhand[0] + playerhand[1]
            bankroll = bankroll + bet
            playgame()
def simwin():
    global bankroll
    global ogbankroll
    global simwins
    global simlosses
    bankroll = ogbankroll
    simwins = simwins + 1

def simlost():
    global bankroll
    global ogbankroll
    global simwins
    global simlosses
    bankroll = ogbankroll
    simlosses = simlosses + 1


start()

我遗漏了一些实际控制游戏的功能,例如当玩家击中发牌手时和ETC时击中了游戏,但这基本上就是代码流。它必须经历很多次,因为通常资金大约是5000,而玩家的最大下注大约是20,所以玩家要花很长时间才能全部输掉或获利。

1 个答案:

答案 0 :(得分:0)

所以我想出了如何进行更多迭代的方法。我创建了一个变量simIsDone,然后在主函数playgame()定义了所有函数(如hit()Stand()等)的最后,我放置了一会儿simIsDone == 0:playgame()然后我删除了所有函数调用playgame()而不是范围内的n。然后,无论何时您以simwin()获胜或以simlost()输掉,我都将simIsDone =1。这将停止while循环,这意味着您获利或损失了所有资金。然后结果是for n范围内的循环将检查我们是否已完成所有模拟,如果还有更多工作要做,则将simIsDone = 0设置,然后转到playgame(),其中for循环将再次接管直到发生了盈亏。以下是此解决方案固定的代码 注意:对于这种基本的最小代码,仍然会出现错误,因为任何手牌都有50%的赔率或获胜的机会,因此,如果您将获利设置得太高,则从数学上讲不可能获胜,因此这是唯一的方法如果您损失了所有金钱,则结束每次迭代。因此,很可能您最终领先一堆,但由于设定的利润如此之高,您很可能会继续在连续输赢的地方牵手,因此永远不会达到bankroll == 0或bankroll> = profit + ogbankroll < / p>

import random
import math
import sys
deck = z = [2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9,
            10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
numberOfSims = 5
# This in the real code is user input
profit = 5 # User input
sys.setrecursionlimit(20000)
bet = 1
bankroll = 10 # User Input
ogbankroll = 10 # User input
simlosses = 0
simwins = 0
simdone = 0
numberr = 0
def start():
    global simdone
    global numberr
    for n in range(numberOfSims):
        numberr = 0
        simdone = 0
        playgame()
    simdone = 1
    print("Sim wins: ", simwins)
    print("Sim losses: ", simlosses)

def playgame():
    global deck
    global numberOfSims
    global profit
    global bet
    global bankroll
    global numberr

    random.shuffle(deck)
    random.shuffle(deck)
    random.shuffle(deck)
    if bankroll == ogbankroll + profit:
        simwin()
    elif bankroll <= 0:
        simlost()
    else:
        dealerhand = [deck[0], deck[1]]
        playerhand = [deck[2], deck[3]]
        if dealerhand[0] + dealerhand[1] == playerhand[0] + playerhand[1]:
            ab = 1
            #Push

        elif dealerhand[0] + dealerhand[1] > playerhand[0] + playerhand[1]:
            # Lost
            bankroll = bankroll - bet

        else: # This else means player wins
            # dealerhand[0] + dealerhand[1] < playerhand[0] + playerhand[1]
            bankroll = bankroll + bet
    while simdone == 0:
        numberr = numberr + 1
        # print(numberr)
        playgame()

def simwin():
    global bankroll
    global ogbankroll
    global simwins
    global simlosses
    global simdone
    bankroll = ogbankroll
    simwins = simwins + 1
    simdone = 1
def simlost():
    global bankroll
    global ogbankroll
    global simwins
    global simlosses
    global simdone
    bankroll = ogbankroll
    simlosses = simlosses + 1
    simdone = 1


start()