Skip to content

Instantly share code, notes, and snippets.

@ltbringer
Last active September 3, 2018 23:22
Show Gist options
  • Save ltbringer/7a470eb160ec7dbbbb44dfc036e3d006 to your computer and use it in GitHub Desktop.
Save ltbringer/7a470eb160ec7dbbbb44dfc036e3d006 to your computer and use it in GitHub Desktop.
reinforcement_tic_tac_toe_snippet_5.py
def have_same_val(self, axis, item, item_x, item_y):
"""
Oh boy! without the documentation this would be just 12-14 lines of code.
Checks if a row(if axis = 0) of the board matrix has same values throughout.
or
Checks if a column(if axis = 1) of the board matrix has same values throughout.
This is useful to check if a row or column is filled up by the symbol which was added the latest.
params:
- axis int: The direction along which operations are to be performed. Can have a value of 0 or 1 only.
- 0 means row
- 1 means column
- 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
# Get the number of rows in the board.
result = True
# Optimistic approach, assume the result to be true,
# unless proven wrong in the further steps.
row_idx = col_idx = 0
# set row_idx and col_idx iteration variables as 0
# they don't get used much, they are present for code readability.
main_idx, fixed_idx, ignore_idx = (col_idx, item_x, item_y) \
if axis == 0 \
else (row_idx, item_y, item_x)
# main_idx: Update this index each iteration of the loop.
# fixed_idx: Don't modify this index ever.
# ignore_idx: this is the index of the inserted element
# which doesn't need to be evaluated, so ignore.
# The if-else ensures weather to increment the row index
# or the column index according to the value of the axis.
while main_idx < max_limit:
# If the main_idx which starts at 0 is less than number of rows/cols in matrix.
if main_idx != ignore_idx:
# And main_idx is not equal to the index of the latest item inserted (ignore_idx)
# because for a fixed_index if we compare main_idx and ignore_idx it would give us the
# latest element added, which will be equal to itself.
# Learning algorithms are costly, ain't nobady got time fo that!
board_item = self.board[fixed_idx][main_idx] \
if axis == 0 \
else self.board[main_idx][fixed_idx]
# find the item(board_item) in the matrix
# corresponding to main_idx and the fixed_index.
# It should be an element in the same row or column depending on the axis.
if board_item != item or board_item == 0:
# If the board_item found is not equal to the latest item added
# or if the board item is 0, which is still not marked by bot or player,
# 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
main_idx += 1
return result
def cols_have_same_values(self, item, item_x, item_y):
"""
Check if any of the columns 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.
"""
axis = 1
return self.have_same_val(axis, item, item_x, item_y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment