【问题标题】:Checking for a space in a matrix in python在python中检查矩阵中的空格
【发布时间】:2016-05-17 19:12:31
【问题描述】:

我是 python 新手,作为作业的一部分,我必须创建一个 connect 4 游戏。我有这个函数的 valid_moves 部分,但我不知道如何检查对手的棋子是否在哪里,以及我应该如何将我的棋子放在避免对手获胜但为我提供获胜优势的位置。代码如下:

def ai_player(board, turn, valid_moves):
"""
Inputs:
    board: numpy array of the disks for each player, e.g.
            [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
             [0, 0, 0, 1, 2, 0, 0, 0, 0, 0],
             [0, 0, 1, 2, 1, 0, 0, 0, 0, 0]]

            - 0 empty locations
            - 1 your disks
            - 2 your opponents disks
    turn: integer turn counter (starts from 1)
    valid_moves: numpy array of valid col index numbers where a disk can be
                 placed, e.g. [0, 1, 2, 3, 4, 5, 6]

Return:
    col index number --> an integer number of the col where you want to
                         place a disk e.g. 0 (NB: this return value must
                         appear in the valid_moves array)
"""

colIndex = range(10)

for i in range(len(valid_moves) - 1):
    if turn == 1:
        ind = colIndex[4]

    elif turn > 1 and (len(valid_moves) > 0):
        if valid_moves[colIndex[i] + 1] == 0 and colIndex[i] < 10:
            ind += 1

        elif valid_moves[colIndex[i] - 1] == 0:
            ind -= 1
    else:
        # choose a random move to make from the valid_moves list
         ind = random.randint(0, len(valid_moves)-1)



#This is my code to add my coin (1) at an empty position and after the opponents coin (2)
for j in range(len(valid_moves) - 1):

    if(colIndex[i] == 2):
        colIndex[i + 1] = 1
    elif colIndex[i] == 0 and colIndex[i] == 1 and colIndex[i] != 2:
        colIndex[i + 1] = 1

return valid_moves[ind]

任何帮助将不胜感激。

【问题讨论】:

  • 我认为这确实是一个过于宽泛的问题,但作为一种天真的第一种方法,我会检查每列 4 的每个可能位置、每行 4 的每个位置和每个位置4 的对角线。如果你发现你的颜色有 3 个,而另一个空间是一个空隙,检查你是否可以在空隙中放置一个标记,并且你是否可以移动。如果你没有找到任何获胜的动作,重复寻找你的对手可能获胜的地方,你可以阻止。如果还是没找到招式,那就随机放置一个token(然后再考虑如何改进这一步)
  • 我还会考虑将我的电路板建模为令牌堆栈而不是二维数组,这将简化添加令牌并且不会使搜索变得更加困难。

标签: python python-3.x


【解决方案1】:

这更像是一个 AI 问题,而不是 Python 问题。我不确定您是否熟悉应用于计算机科学领域的 启发式 术语,但想象一下这种情况:您开始游戏并想要采取行动。要自动计划您的行动(并做出可以让您赢得比赛的有意义行动),您需要在采取行动后考虑对手的可能性。

为此,您可以尝试列出所有可能的动作,并根据这些动作的好坏程度为这些动作分配一个“分数”(一个数值)。示例:使您赢得比赛的动作必须具有最高分。

从过于简化的角度来看,启发式的概念是为可能的动作分配一个“分数”,以决定你或机器想要做出的动作. 编写一个好的获胜策略就是编写几个好的且有意义的策略,然后将它们组合在一起。

如何决定策略?您可以考虑几种游戏情况并确定哪个动作更好。你可以问自己以下问题:

  • 哪个动作更好,将一个区域内的所有磁盘堆积起来还是分散磁盘?如果您认为分散磁盘更好,您可以为移动给出更好的“分数”。
  • 如果对方有三个磁盘,最好将磁盘放在插槽上吗?显然是的,否则机器会赢。

如果您考虑几种启发式方法,您可以自动确定给定棋盘的最佳棋步,并模拟接下来的 X 步棋(以避免出现问题computationally unsolvable)。我相信您可以在几个学术网页上找到其他示例,甚至可以在 Google 上搜索“连接 4 个启发式”找到启发式示例。

编辑:实际上,我刚刚找到了一个 webpage 解释可能适用的启发式方法。

【讨论】:

    猜你喜欢
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    相关资源
    最近更新 更多