簡體   English   中英

如何使用二維數組檢查“連續 4 個”? 請注意,我沒有使用“numpy”,因為我對編碼非常陌生

[英]How do I check for a "4 in a row" with a 2d array? Note that I'm not using "numpy" as I am extremely new to coding

我有一個使用海龜的 connect 4 游戲。 我有一個 6 行 7 的二維數組。我不知道如何連續檢查 4 個列表。 它連接四個,因此需要檢查對角線、水平和垂直方向。 這是我的清單:

c4board = [[0]*7 for _ in range(6)]

我在每一列上都有一個烏龜,單擊它時將通過以下方式將一個項目插入列表中:

def x2click(x,y):
  global currentturn
  global x2rowcor
  checker = trtl.Turtle()
  checker.ht()
  checker.shape("circle")
  checker.turtlesize(2)
  checker.color(currentturn)
  checker.penup()
  checker.goto(-80, x2rowcor)
  checker.st()
  x2rowcor += 40
  listxcor2 = ((x2rowcor+80)/40)-1
  print(listxcor2)
  checkerplaced()
  if c4board[5][1] == 0:
    c4board[5][1] = onetwo
    print(c4board)
  elif c4board[4][1] == 0:
    c4board[4][1] = onetwo
    print(c4board)
  elif c4board[3][1] == 0:
    c4board[3][1] = onetwo
    print(c4board)
  elif c4board[2][1] == 0:
    c4board[2][1] = onetwo
    print(c4board)
  elif c4board[1][1] == 0:
    c4board[1][1] = onetwo
    print(c4board)
  elif c4board[0][1] == 0:
    c4board[0][1] = onetwo
    print(c4board)
  if x2rowcor > 120:
    c2.ht()
    x2.ht()

我曾嘗試檢查烏龜 colors,但發現這是一個死胡同。

我曾嘗試使用 Numpy,但正如我所說,我對編碼有些陌生(我只做了幾個月的 python)Numpy 讓我很困惑,我不知道如何讓它工作。

如果沒有 Numpy 就沒有辦法做到這一點,請像 10 歲的孩子一樣向我解釋。

如果你想要我完整的 500 多行代碼,你可以問,但我不想把所有這些都放在我的問題中。

你不應該害怕numpy它是一個非常方便的工具。 要安裝它,只需在與 python 相同的環境中從命令行使用pip install numpy numpy。


手頭有 numpy,我使用的條件是1代表player 1 2代表player 2 現在我們可以瀏覽游戲板的每個4x4子字段以檢查“四個連續12

# Import numpy to your script via this statement at the top of the file.
import numpy as np


def check_victory(board: np.array) -> int:
    """
    Navigating through all 4x4 subfields and check for victory condition
    of either player. 
    :returns: 1 or 2 of player 1 or 2 has won, 0 otherwise.
    """
    for row in range(7 - 4):
        for col in range(8 - 4):
            v = check_subfield(board[row:row + 4, col:col + 4])
            if v > 0:
                return v
    return 0


def check_subfield(field: np.array) -> int:
    # check diagonal.
    v = same_number(np.diagonal(field))
    if v > 0:
        return v
    for i in range(4):
        # check column
        v = same_number(field[i, :])
        if v > 0:
            return v
        # check row
        v = same_number(field[:, i])
        if v > 0:
            return v
    return 0

def same_number(values: list) -> int:
    return int((values == values[0]).all() * values[0])

一旦滿足勝利條件,所有方法都會返回,以避免多余的檢查。 有更復雜的方法來檢查這個(參見rolling windows等),但這個版本應該對初學者有點友好,抽象和庫使用的級別不太高。 我希望這能為您提供一個可讀且易於理解的示例。


現在,有了這組方法,我們可以很容易地測試它:

# We can create an empty 6x7 game board (zeros everywhere) 
# by using the `numpy.zeros` method with the desired shape:
c4board = np.zeros((6, 7))
print(check_victory(c4board))
# Out[0] = 0

c4board[2, 1] = 1
c4board[2, 2] = 1
c4board[2, 3] = 1
c4board[2, 4] = 1
print(check_victory(c4board))
# Out[1] = 1

c4board = np.zeros((6, 7))
c4board[2, 1] = 1
c4board[2, 2] = 1
c4board[2, 4] = 1
c4board[5, 3] = 2
c4board[5, 4] = 2
c4board[5, 5] = 2
c4board[5, 6] = 2
print(check_victory(c4board))
# Out[2] = 2

c4board = np.zeros((6, 7))
c4board[2, 2] = 1
c4board[3, 3] = 1
c4board[4, 4] = 1
c4board[5, 5] = 1
c4board[5, 4] = 2
print(check_victory(c4board))
# Out[3] = 1

好吧,如果你堅持不使用numpy的話:給你 go:(不過我真的建議你習慣 numpy)

def check_victory(board: list) -> int:
    for row in range(7 - 4):
        for col in range(8 - 4):
            # This part is now much more effort
            sub_field = [[board[i][j] for i in range(row, row+4)] for j in range(col, col+4)]
            v = check_subfield(sub_field)
            if v > 0:
                return v
    return 0


def check_subfield(field: list) -> int:
    # Note the change in syntax and list construction.
    # check diag.
    v = same_number([field[i][i] for i in range(4)])
    if v > 0:
        return v
    for i in range(4):
        # check row
        v = same_number(field[i])
        if v > 0:
            return v
        # check column
        v = same_number([field[j][i] for j in range(4)])
        if v > 0:
            return v
    return 0


def same_number(values: list) -> int:
    if all(v == 1 for v in values):
        return 1
    if all(v == 2 for v in values):
        return 2
    return 0

這可以像上面一樣容易檢查

c4board = [[0 for _ in range(7)] for _ in range(6)]
c4board[2][2] = 1
c4board[3][3] = 1
c4board[4][4] = 1
c4board[5][5] = 1
c4board[5][4] = 2
print(check_victory(c4board))
# Out[4] = 1

暫無
暫無

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

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