簡體   English   中英

如何檢查 python 列表以查看 5 個連續項目是否相同?

[英]How to check a python list to see if 5 sequential items are identical?

我正在創建一個 15x15 井字游戲。 其中一個條件是檢查用戶是否中獎,在這種情況下,用戶需要連續獲得 5 個……無論是水平的、對角的還是垂直的。 我無法弄清楚如何檢查這個。 感謝您提供任何幫助,並且在我剛開始時,請堅持使用 Python 的更基本功能。 我的代碼如下。

class FiveBoard:
    """"""

    def __init__(self):
        """"""
        self._board_list = [
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 1
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 2
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 3
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 4
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 5
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 6
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 7
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 8
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 9
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 10
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 11
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 12
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 13
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''],  # 14
                            ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '']  # 15
                            ]
        self._current_state = "UNFINISHED"

    def get_current_state(self):
        """"""
        return self._current_state

    def print_board(self):
        for row in self._board_list:
            print(row)

    def make_move(self, row, col, mark):
        """"""

        for row in self._board_list:
            for col in row:
                if col == 'x':

                elif col == 'o':


        if self._current_state == "DRAW":
            return False
        if mark == 'o':
            if self._board_list[row][col] == '':
                self._board_list[row][col] = mark
                return True
            else:
                return False
        elif mark == 'x':
            if self._board_list[row][col] == '':
                self._board_list[row][col] = mark
                return True
            else:
                return False
        count = 0
        for row in self._board_list:
            for col in row:
                if col != '':
                    count += 1
        if count == 225:
            self._current_state = "DRAW"




board = FiveBoard()
board.print_board()
print("--------------------------------------------------------------")
board.make_move(0, 14, 'x')
board.print_board()
print("--------------------------------------------------------------")
board.make_move(6, 11, 'x')
board.print_board()
print("--------------------------------------------------------------")

這 function 將為您工作。 您只需在單獨的lists中獲取所有水平、垂直和對角點。

最后的雙 for 循環只檢查任何連續mark 5 次。

def check_winner(self, row, col, mark):
    board_len = len(self._board_list)

    hor_points = [(row + x, col) if 0 <= row + x < board_len else None for x in range(-4, 4)]
    ver_points = [(row, col + x) if 0 <= col + x < board_len else None for x in range(-4, 4)]
    diag1_points = [(row + x, col + x) if all([0 <= y < board_len for y in [row + x, col + x]]) else None for x in
                    range(-4, 4)]
    diag2_points = [(row + x, col - x) if all([0 <= y < board_len for y in [row + x, col - x]]) else None for x in
                    range(-4, 4)]

    for x in [hor_points, ver_points, diag1_points, diag2_points]:
        count = 0
        for y in x:
            if y and ((self._board_list[y[0]][y[1]] == mark) or (y[0] == row and y[1] == col)):
                count += 1
            else:
                count = 0
            if count == 5:
                return 'WINNER'
    return 'NO WIN'

暫無
暫無

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

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