I am creating a 15x15 tic tac toe game. 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. 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. 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. You just have to take all the horizontal, vertical and diagonal points in separate lists
.
The final double for loop just checks for any continuous mark
5 times.
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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.