2017-01-03 149 views
0

這是我連四碼到目前爲止,錯誤快到了 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

[Python NameError:name is not duplicate定義](http://stackoverflow.com/questions/14804084/python-nameerror-name-is-not-defined) – fredtantini

回答

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() 
    ....