检查3D numpy数组中的索引

2024-04-27 00:20:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写代码,通过命令行与一个人在计算机上玩一个名为Pig的骰子游戏

对于计算机的玩家,我使用一个3D numpy数组来存储一个游戏策略,该策略基于信息当前得分、银行得分和对手银行得分。你知道吗

其思想是,每转一圈,计算机都会检查策略数组,如果索引[AI_1_current,AI_1_bank,human_bank]处的值等于0,它会继续滚动,如果它等于1,它会存储它当前的分数。你知道吗

现在,我遇到的问题是,轮到计算机执行的功能不是正确读取数组,也不是在应该的时候存储数组。你知道吗

我有以下代码来设置数组:

AI_1_strategy = numpy.zeroes((100,100,100))

for i in range(10, 100):
    for j in range(10, 100):
        for k in range(10, 100):
            AI_1_strategy[i, j, k] = 1

理想情况下,如果i,j或k大于或等于10,计算机每转一圈都会倾斜

然后,我有以下代码来检查数组:

if AI_1_strategy[AI_1_bank, AI_1_current, human_bank] == 1: 
    AI_1_bank += AI_1_current # add current points to the bank
    AI_1_current = 0
    time.sleep(3) # 3 second delay

   if AI_1_bank >= 100: # win condition
        print "AI_1 WINS!"
    else:   
        AI_current = 0 # sets current points to 0
        print "AI has banked"
        pig_human() # moves to the player's turn

else:
    time.sleep(3) # 3 second delay
    print "AI_1 chose to keep rolling"
    pig_AI_1() # AI takes another roll

尽管有这个代码,计算机还是无法保持一致性。你知道吗

如果我改为:

AI_1_strategy = numpy.ones((100,100,100))

for i in range(10, 100):
    for j in range(10, 100):
        for k in range(10, 100):
            AI_1_strategy[i, j, k] = 0

除非在这种情况下,它将银行每一个回合,无论分数,当它真的应该停止银行当我,j或k达到10。你知道吗

如果有人能帮我,我会非常感激的,我不知道我做错了什么。你知道吗

谢谢。你知道吗


Tags: to代码innumpyfor计算机range银行
1条回答
网友
1楼 · 发布于 2024-04-27 00:20:31

看起来你犯了个小逻辑错误。你知道吗

I have this code to set up the array:

AI_1_strategy = numpy.zeroes((100,100,100))

for i in range(10, 100):
    for j in range(10, 100):
        for k in range(10, 100):
            AI_1_strategy[i, j, k] = 1

Which ideally should mean, if i, j or k are greater than or equal to 10, the computer will bank every turn

这是不正确的:i、j和k中的每一个都必须大于等于10才能从此数组中读取1。你知道吗

相关问题 更多 >