名称未定义

时间:2017-01-03 15:55:15

标签: python-3.x

这是我目前为止连接四的代码,错误即将出现 NameError:名称'CheckWinVt'未定义

我不确定如何解决这个问题,任何人都可以提供帮助吗?

class ConnectFourBoard:

    def __init__(self, cols = 7, rows = 6, requiredToWin = 4):
        self.__space = ''
        self.__board = []
        self.cols = cols
        self.rows = rows
        self.Win = requiredToWin
        self.__board = [[''] * cols for i in range(rows)]

    def MakeMove(self, row, col, element):
        global __board
        self.__board[row][col] = element

    def CheckWin(board):
        print("Check Winner")
        CheckWinVt(board)
        CheckWinHz(board)
        CheckWinDiag(board)

    def CheckWinVt(board):
        for x in range(rows):
            for y in range(columns):
                if (board[(x,y)] == "X" and board[(x+1,y)] == "X" and board[(x+2,y)] == "X" and board[(x+3,y)] == "X"):
                    print ("Winner == HumanPlayer")
                    break
                elif (board[(x,y)] == "O" and board[(x+1,y)] == "O" and board[(x+2,y)] == "O" and board[(x+3,y)] == "O"):
                    print ("Winner == ComputerPlayer")
                    break

    def CheckWinHz(board):
        for x in range(rows):
            for y in range(columns):
                if (board[(x,y)] == "X" and board[(x,y+1)] == "X" and board[(x,y+2)] == "X" and board[(x,y+3)] == "X"):
                    print ("Winner == HumanPlayer")
                    break
                elif (board[(x,y)] == "O" and board[(x,y+1)] == "O" and board[(x,y+2)] == "O" and board[(x,y+3)] == "O"):
                    print ("Winner == ComputerPlayer")
                    break

    def CheckWinDiag(board):
        for x in range(rows):
            for y in range(columns):
                if (board[x,y] == "X" and board[(x+1,y+1)] == "X" and board[(x+2,y+2)] == "X" and board[(x+3,y+3)] =="X"):
                    print ("Winner == HumanPlayer")
                elif (board[x,y] == "O" and board[(x+1,y+1)] == "O" and board[(x+2,y+2)] == "O" and board[(x+3,y+3)] =="O"):
                    print ("Winner == ComputerPlayer")
                elif (board[x,y] == "X" and board[(x-1,y-1)] == "X" and board[(x-2,y+2)] == "X" and board[(x-3,y+3)] =="X"):
                    print ("Winner == HumanPlayer")
                elif (board[x,y] == "O" and board[(x-1,y-1)] == "O" and board[(x-2,y+2)] == "O" and board[(x-3,y+3)] =="O"):
                    print ("Winner == ComputerPlayer")



    def FullBoard(self):
        row = ""
        for x in range (6):
            for y in range (7):
                row += self.__board[x][y]

            if "" in row:
                return False
            row = ""
        return True

    def FreeSpace(self, row, col):
        if self.__board[row][col] == "":
            return True
        return False

    def show_board_dynamic(self):
        print()
        print("-------")
        for i in range(len(self.__board)):
            for j in range(len(self.__board[0])):
                print("|", end = "")
                print(self.__board[i][j], end = ""),
            print("|")
            print("-------")
        print()

在我的主要课程中:

winner = gboard.CheckWin()

1 个答案:

答案 0 :(得分:1)

表单的所有功能

def CheckWin(board):
    ...
    CheckWinVt(board)

应该以这种方式实现(并以这种方式访问​​董事会):

def CheckWin(self):
    board = self.__board # if you need to do stuff with board
    # or just access self.__board directly
    self.CheckWinVt()
    ....