簡體   English   中英

連接四個Python打印問題

[英]Connect Four Python printing problems

我已經為游戲連接四塊板做了一個初始化板,現在我想跟蹤每個turn的位置和比賽,如果我這樣做:

b = ConnectFour()
b.play_turn(1, 3)
b.play_turn(1, 3)
b.play_turn(1, 4)
b.print_board()   

木板應打印出:

-----------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
-----------------------------
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   | x |   |   |   |
|   |   |   | x | x |   |   |
-----------------------------

更新:

#initializes board

class ConnectFour(object):
        def __init__(self):
            self.board = [[0 for i in range(7)] for j in range(8)]



def get_position(self, row, column):
  """ Returns either None or an integer 1 or 2 depending 
  on which player is occupying the given row or column.  
  Row is an integer between 0 and 5 and column is an integer between 0 and 6. """
assert row >= 0 and row < 6 and column >= 0 and column < 7
return self.board[row][column]


def play_turn(self, player, column):
    """ Updates the board so that player plays in the given column.

    player: either 1 or 2
    column: an integer between 0 and 6
    """
    assert player == 1 or player == 2
    assert column >= 0 and column < 7

    for row in range(6):
        if self.board[row][column] == None:
            self.board[row][column] = player
            return



def print_board(self):
    print "-" * 29
    print "| 0 | 1 | 2 | 3 | 4 | 5 | 6 |"
    print "-" * 29
    for row in range(5,-1,-1):
        s = "|"
        for col in range(7):
            p = self.get_position(row, col)
            if p == None:
                s += "   |"
            elif p == 1:
                s += " x |"
            elif p == 2:
                s += " o |"
            else:                     
                s += " ! |"
        print s
print "-" * 29

解決:

這是當前輸出:

-----------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
-----------------------------
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   |   |   |   |   |
|   |   |   | x |   |   |   |
|   |   |   | x | x |   |   |
-----------------------------

我看到的問題是您的self.get_position不執行您想要的操作。

def get_position(self, row, column):
    assert row >= 0 and row < 6 and column >= 0 and column < 7
    if self.get_position == 1: # the type of self.get_position is a function, and it will never be equal to 1, or 2, so it always goes to None
            return 1
        elif self.get_position == 2:
            return 2
        else:
            return None

另一個問題是您從未使用過開發板,也從未將數據存儲在任何地方。
如果您是我,那么我將首先考慮如何存儲數據。
一個明顯的選擇是二維數組。

self.board = [[0 for i in range(6)] for j in range(7)]

當您調用play_turn時,嘗試更新該列,例如,您已經在第3列上具有[0,0,0,0,2,1],您將嘗試獲取不為0的第一個元素的索引。新播放器在其之前的元素上的索引,因此它將變為[0,0,0,1,2,1](如果該片段屬於播放器1)。

然后,實際上要使用列號和行號來檢查板上的內容。 是0還是1或2。
您最好將其稱為get_player_num或類似的名稱。 您沒有獲得位置,但獲得了該位置的棋手編號。

一個例子。 如果我在一個班級有一個2 * 2數組:

class CF(object):
    def __init__(self):
        board = [[0, 0], [0, 0]]

這代表一個2 * 2字段,現在它們都是空的。
0 0
0 0

我可以設計一個更新方法:

def update(self, col, row, newVal):
    self.board[col - 1][row - 1] = newVal

如果這樣做:

game = CF()
game.update(1, 2, 1) 

然后董事會變成
0 0
1 0

在閱讀游戲板時,我知道第一列第二行是玩家1。

您的get_position代碼不正確。 您需要檢查self.board ,而不是查看self.get_position (這是方法本身,沒有任何值)。

由於您當前不在任何地方使用self.board,因此我不確定您打算使用哪種格式(它是單個列表還是列表列表?)。

我認為您需要添加以下內容:

  1. 將板更好地初始化為__init__ 我建議將self.board為具有0None (無論您要表示“空”為)的列表,重復7 * 6次。 或者,您也可以使其成為列表列表(可能是六個列表,每個列表七個值)。

  2. 檢查電路板上的get_position值。 如果您只列出一個列表,則應檢查self.board[row*7+column]

  3. play_turn方法需要檢查木板,以找出將一塊放入指定列時該塊落在哪一行。 該作品將進入該列為空的最低行,因此需要循環。 找到精確位置后,即可在self.board設置值。

固定get_position ,您的print_board函數應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM