这个井字游戏怎么了

2024-09-27 21:31:24 发布

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

我最近参加了cs50人工智能python在线课程,第一个项目是使用minimax算法创建一个tic-tac-toe游戏,我已经尝试过了。但是,当我从他们的网站上运行与zip文件一起提供的runner.py文件时,它会给我一些错误,如以下语句: i=动作[0], 表示“'NoneType'对象不可下标” 你能纠正一下代码吗,或者至少告诉我到底是什么问题 谢谢

import math
import numpy as npy
import sys
import copy

X = "X"
O = "O"
EMPTY = None


def initial_state():
    """
    Returns starting state of the board.
    """
    return [[EMPTY, EMPTY, EMPTY],
            [EMPTY, EMPTY, EMPTY],
            [EMPTY, EMPTY, EMPTY]]


def player(board):
    """
    Returns player who has the next turn on a board.
    """
    if board == initial_state():
        return X

    numpy_board = npy.array(board)

    Xno = npy.count_nonzero(numpy_board = X)

    Ono = npy.count_nonzero(numpy_board = O)

    if Xno > Ono:

        return O

    elif Ono > Xno:

        return X

def actions(board):
    """
    Returns set of all possible actions (i, j) available on the board.
    """
    Result = set()

    for k in range(3):

        for l in range(3):

            if board[k][l] == EMPTY:

                Result.add(board[k][l])

    return Result


def result(board, action):
    """
    Returns the board that results from making move (i, j) on the board.
    """
    i = action[0]
    j = action[1]

    if board[i][j] != EMPTY:
        raise Exception("Invalid Action")

    new_player = player(board)

    new_board = copy.deepcopy(board)

    new_board[i][j] = new_player

    return new_board

def winner(board):
    """
    Returns the winner of the game, if there is one.
    """
    for i in range(3):

        if (board[i][0] == board[i][1] == board[i][2] and board[i][0] != EMPTY):

            return board[i][0]

        if (board[0][0] == board[1][1] == board[2][2] or (board[0][2] == board[1][1] == board[2][0]) and board[1][1] != EMPTY):

             return board[1][1]
        if (board[0][i] == board[1][i] == board[2][i] and board[0][i] != EMPTY):

             return board[1][i]
        else:

             return None
def terminal(board):
    """
    Returns True if game is over, False otherwise.
    """
    if winner(board) != None:

        return True;

    numpy_board = npy.array(board)

    empty_no = npy.count_nonzero(numpy_board == EMPTY)

    if (empty_no == 0):

        return True
    else:

        return False


def utility(board):
    """
    Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
    """
    win_player = winner(board)

    if (win_player == X):

        return 1

    elif (win_player == O):

        return -1

    else:

        return 0


def minimax(board):
    """
    Returns the optimal action for the current player on the board.
    """
    if terminal(board):
        return None

    currentPlayer = player(board)
    if currentPlayer == X:
        return max_value(board)[1]
    else:
        return min_value(board)[1]


def max_value(board):

    if terminal(board):

        return (utility(board), None)

    value = -sys.maxsize-1

    optimalAction = None
    for action in actions(board):

        possibleResult = min_value(result(board, action))

        if possibleResult[0] > value:

            value = possibleResult[0]

            optimalAction = action


        if value == 1:
            break

    return (value, optimalAction)


def min_value(board):

    if terminal(board):

        return (utility(board), None)

    value = sys.maxsize

    optimalAction = None

    for action in actions(board):

        possibleResult = max_value(result(board, action))

        if possibleResult[0] < value:

            value = possibleResult[0]

            optimalAction = action


        if value == -1:
            break

    return (value, optimalAction)

Tags: thenumpyboardnoneforreturnifvalue
1条回答
网友
1楼 · 发布于 2024-09-27 21:31:24

几个问题:

  • Xno = npy.count_nonzero(numpy_board = X)中的语法错误。你错过了一个等号。它应该是==。下一个类似语句中出现相同错误
  • elif Ono > Xno:中的条件永远不会为真(想想看)。更重要的是,这个条件允许在不输入任何一个块的情况下通过这个if..elif,从而给出一个None返回值。要么是轮到X,要么不是。在后一种情况下,总是轮到O了。你不需要第二次测试。因此,请将这一行更正为else:
  • Result.add(board[k][l])不添加坐标对,而是添加正方形的内容。这不是你想要的。您想存储坐标。所以这应该是Result.add((k, l))。注意:对于这样的名称,不要使用Pascal大小写,而是使用camel大小写
  • 在函数winnerfor循环将在第一次迭代时退出。它从不执行其他迭代。在第一次迭代中,您不可能知道足够的信息来返回None。所以删除else: return None:在这种情况下,循环必须继续。注意:对角线的测试最好移到循环之外,因为重复测试3次是没有意义的。它不依赖于循环变量

如果你做了这些修正,它应该会起作用

其他一些评论:

  • 如果要从列表中创建numpy数组,那么为什么不从一开始就只创建一次numpy数组,并且只使用该数组而不使用列表?每次在playerterminal中进行转换都会影响性能
  • 此外,计算X的数量和O的数量需要两次迭代,而您可以在一次扫描中计算空单元格的数量,并从中扣除有多少不是空的。更快的方法是保持一个计数器,在移动时增加计数器,回溯时减少计数器
  • 上述计数器可用于快速确定当前玩家。如果移动次数为偶数,则轮到X,否则轮到O
  • deepcopy具有性能成本。考虑使用相同的列表/数组而不复制它。您只需要在递归调用之后添加一个“undo”操作<> LI>而不是重新创建一组可能的动作,也考虑增量地保持一个集合:在移动时删除该集合中的一个动作,并在回溯时将其放回。这将提高性能
  • 不要使用此模式:

    if (empty_no == 0):
       return True
    else:
       return False
    

    首先,括号不是必需的,但更重要的是:当您已经一个布尔表达式(empty_no == 0)时,只返回它。不要做这种if..else的事情:

    return empty_no == 0
    
  • minimax算法只返回值-1、0或1,这意味着它不支持快速胜于慢速胜出。这可能导致出人意料的举动,而不是直接获胜。为了改进这一点,考虑使用更动态的值。一个想法是更改utility函数,以便对于使用X的win,它返回空闲单元数加1。对于O来说,它将是该值的否定。这样的话,快速获胜是有利的

相关问题 更多 >

    热门问题