简体   繁体   English

如何检查 python 列表以查看 5 个连续项目是否相同?

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

I am creating a 15x15 tic tac toe game.我正在创建一个 15x15 井字游戏。 One of the conditions is to check if the user has won, which in this case, the user needs to get 5 in a row...whether that be horizontally, diagonally, or vertically.其中一个条件是检查用户是否中奖,在这种情况下,用户需要连续获得 5 个……无论是水平的、对角的还是垂直的。 I am having trouble figuring out how to check this.我无法弄清楚如何检查这个。 Any help is appreciated, and please stick to the more basic capabilities of Python as I am just starting.感谢您提供任何帮助,并且在我刚开始时,请坚持使用 Python 的更基本功能。 My code is below.我的代码如下。

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

This function will work for you.这 function 将为您工作。 You just have to take all the horizontal, vertical and diagonal points in separate lists .您只需在单独的lists中获取所有水平、垂直和对角点。

The final double for loop just checks for any continuous mark 5 times.最后的双 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.

相关问题 如何检查 python 列表中的相同字符串 - How to check for identical strings in a list in python 检查列表中的列表是否相同(python) - check identical for list in list(python) 如何检查python中的一个列表中的所有项目是否都在第二个列表中? - How do I check to see if all of the items in one list are in a second list in python? 在Python中,如何检查项目列表是否是一个列表中的一个列表? - In Python, how to you check to see if a list of items is exclusive to one list out of two? 如何从列表中删除相同的项目并在Python中对其进行排序? - How do I remove identical items from a list and sort it in Python? Python:如何查找列表中特定数量的项目是否相同? - Python: How to find whether a specific number of items in a list are identical? 如何检查列表中是否存在超过3个相同的字符串,Python - How to check if there are more than 3 identical string in a list, Python Python:列表中相同的项目被视为相同 - Python: identical items in list are treated as the same Python 检查项目是否在列表中 - Python check if Items are in list 如何使用 python/pandas 计算列中相同的顺序值的数量? - How to count number of identical, sequential values in a column with python/pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM