如何拥有正确数量的列?

时间:2017-01-03 02:04:01

标签: python-3.x

这是我的连接四的代码,然而,当我运行它时出现太多行,任何想法如何解决它?我知道它可能很简单,但我是初学者程序员哈哈。这就是显示:

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

玩家X转 请输入第no行:

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

1 个答案:

答案 0 :(得分:0)

__init__方法中删除以下内容。

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

另外,当您已经global __board

时,我无法理解self.__board的必要性