[英]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.