2017-01-03 51 views
-2

这是我的代码连接四,但是,当我运行它时,显示了太多的行,任何想法如何解决它?我知道这可能很简单,但我是一个初学者程序员哈哈。这是表明:如何获得正确的列数?

------- 
||||||| 
------ 
||||||| 
------ 
||||||| 
------ 
||||||| 
------ 
||||||| 
------ 
||||||| 
------ 
||||||| 
------ 
|||||||| 
------ 
|||||||| 
------ 
|||||||| 
------ 
|||||||| 
------ 
|||||||| 
------ 
|||||||| 
------ 
|||||||| 
------ 

玩家X转 请输入行号:

class ConnectFourBoard: 

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

     for i in range(cols): 
      row = ['']*cols 
      self.__board.append(row) 

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

    def CheckForWin(self): 

     winner = ((self.__board[0][0] == self.__board[0][1] and self.__board[0][1] == self.__board[0][2] and self.__board[0][2] == self.__board[0][3] and self.__board[0][3] == self.__board[0][4] and self.__board[0][4] == self.__board[0][5] and self.__board[0][5] != self.__space) or 
        (self.__board[1][0] == self.__board[1][1] and self.__board[1][1] == self.__board[1][2] and self.__board[1][2] == self.__board[1][3] and self.__board[1][3] == self.__board[1][4] and self.__board[1][4] == self.__board[1][5] and self.__board[1][5] != self.__space) or 
        (self.__board[2][0] == self.__board[2][1] and self.__board[2][1] == self.__board[2][2] and self.__board[2][2] == self.__board[2][3] and self.__board[2][3] == self.__board[2][4] and self.__board[2][4] == self.__board[2][5] and self.__board[2][5] != self.__space) or 
        (self.__board[3][0] == self.__board[3][1] and self.__board[3][1] == self.__board[3][2] and self.__board[3][2] == self.__board[3][3] and self.__board[3][3] == self.__board[3][4] and self.__board[3][4] == self.__board[3][5] and self.__board[3][5] != self.__space) or 
        (self.__board[4][0] == self.__board[4][1] and self.__board[4][1] == self.__board[4][2] and self.__board[4][2] == self.__board[4][3] and self.__board[4][3] == self.__board[4][4] and self.__board[4][4] == self.__board[4][5] and self.__board[4][5] != self.__space) or 
        (self.__board[5][0] == self.__board[5][1] and self.__board[5][1] == self.__board[5][2] and self.__board[5][2] == self.__board[5][3] and self.__board[5][3] == self.__board[5][4] and self.__board[5][4] == self.__board[5][5] and self.__board[5][5] != self.__space) or 
        (self.__board[0][0] == self.__board[1][0] and self.__board[1][0] == self.__board[2][0] and self.__board[2][0] == self.__board[3][0] and self.__board[3][0] == self.__board[4][0] and self.__board[4][0] == self.__board[5][0] and self.__board[5][0] != self.__space) or 
        (self.__board[0][1] == self.__board[1][1] and self.__board[1][1] == self.__board[2][1] and self.__board[2][1] == self.__board[3][1] and self.__board[3][1] == self.__board[4][1] and self.__board[4][1] == self.__board[5][1] and self.__board[5][1] != self.__space) or 
        (self.__board[0][2] == self.__board[1][2] and self.__board[1][2] == self.__board[2][2] and self.__board[2][2] == self.__board[3][2] and self.__board[3][2] == self.__board[4][2] and self.__board[4][2] == self.__board[5][2] and self.__board[5][2] != self.__space) or 
        (self.__board[0][3] == self.__board[1][3] and self.__board[1][3] == self.__board[2][3] and self.__board[2][3] == self.__board[3][3] and self.__board[3][3] == self.__board[4][3] and self.__board[4][3] == self.__board[5][3] and self.__board[5][3] != self.__space) or 
        (self.__board[0][4] == self.__board[1][4] and self.__board[1][4] == self.__board[2][4] and self.__board[2][4] == self.__board[3][4] and self.__board[3][4] == self.__board[4][4] and self.__board[4][4] == self.__board[5][4] and self.__board[5][4] != self.__space) or 
        (self.__board[0][5] == self.__board[1][5] and self.__board[1][5] == self.__board[2][5] and self.__board[2][5] == self.__board[3][5] and self.__board[3][5] == self.__board[4][5] and self.__board[4][5] == self.__board[5][5] and self.__board[5][5] != self.__space)) 
     return winner 

    def CheckHz(): 
     for x in range (6): 
      for y in range (7): 
       row += board[x][y] 
       print("%s" %row) 
       row = "" 
       if "XXXX" in row: 
        print("Winner is X") 
        break; 

    def CheckVt(): 
     for y in range(7): 
      for x in range(6): 
       column += board[y][x] 
       print("%s" %column) 
       column = "" 
       if "XXXX" in column: 
        print ("Winner is X") 
        break; 




    def FullBoard(self): 
     return True 

    def FreeSpace(self, row, col): 
     return True 

    def show_board_dynamic(self): 
     print() 
     print("-------") 
     for col in self.__board: 
      for val in col: 
       print("|", end = "") 
       print(val, end = ""), 
      print("|") 
      print("------") 

回答

0

从您的__init__方法如下。

for i in range(cols): 
    row = ['']*cols 
    self.__board.append(row) 

另外,我无法理解,需要global __board当你已经有self.__board