Last active
September 3, 2018 22:44
-
-
Save ltbringer/1c19416cfef0c3aa08effc8c0f5cdf42 to your computer and use it in GitHub Desktop.
reinforcement_tic_tac_toe_snippet_4.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def element_diagonal_has_same_value(self, item, item_x, item_y): | |
""" | |
Check if any of the diagonals have same values | |
params | |
- item_x int: The row of the matrix in which item has been inserted. | |
- item_y int: The column of the matrix in which the item has been inserted. | |
- item int: The latest integer inserted into the matrix at row-index = item_x, and column-index = item_y. | |
""" | |
max_limit, _ = self.board.shape | |
if item_x == item_y: | |
# elements on the left diagonal have same row and column value. | |
return self.left_diagonal_has_same_values(item, item_x, item_y) | |
elif item_x + item_y == max_limit - 1: | |
# elements on the right diagonal have sum of the row and column value as the same number. | |
return self.right_diagonal_has_same_values(item, item_x, item_y) | |
else: | |
# Else, it is not either of the diagonals | |
return False | |
def left_diagonal_has_same_values(self, item, item_x, item_y): | |
""" | |
params | |
- item_x int: The row of the matrix in which item has been inserted. | |
- item_y int: The column of the matrix in which the item has been inserted. | |
- item int: The latest integer inserted into the matrix at row-index = item_x, and column-index = item_y. | |
""" | |
i = j = 0 | |
# set i, j to 0 | |
result = True | |
# Optimistic approach, assume the result to be true, | |
# unless proven wrong in the further steps. | |
max_limit, _ = self.board.shape | |
# Get the number of rows in the board. | |
while i < max_limit: | |
# The row index i is sufficient as i and j are incremented | |
# by same factor resulting in same values (Either would do) | |
if i != item_x: | |
# Avoid checking for the latest item added as that's what we are comparing with | |
if self.board[i][j] != item or self.board[i][j] == 0: | |
# If the board_item found is not equal to the latest item added | |
# result is false as the function didn't find all | |
# values to be same across the row, or column. | |
# and exit the loop because a single-mismatch is sufficient | |
# to confirm that all elements are not same. | |
result = False | |
break | |
i += 1 | |
j += 1 | |
return result | |
def right_diagonal_has_same_values(self, item, item_x, item_y): | |
""" | |
params | |
- item_x int: The row of the matrix in which item has been inserted. | |
- item_y int: The column of the matrix in which the item has been inserted. | |
- item int: The latest integer inserted into the matrix at row-index = item_x, and column-index = item_y. | |
""" | |
result = True | |
max_limit, _ = self.board.shape | |
i = 0 | |
j = max_limit - 1 | |
while i < max_limit: | |
# The row index i is sufficient as i and j are incremented | |
# by same factor resulting in same values (Either would do) | |
if i != item_x: | |
# Avoid checking for the latest item added as that's what we are comparing with | |
if self.board[i][j] != item or self.board[i][j] == 0: | |
# If the board_item found is not equal to the latest item added | |
# result is false as the function didn't find all | |
# values to be same across the row, or column. | |
# and exit the loop because a single-mismatch is sufficient | |
# to confirm that all elements are not same. | |
result = False | |
break | |
i += 1 | |
j -= 1 | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment