Skip to content

Instantly share code, notes, and snippets.

@rinti
Last active August 29, 2015 14:26
Show Gist options
  • Save rinti/15dcc82a81f2ecf9f58e to your computer and use it in GitHub Desktop.
Save rinti/15dcc82a81f2ecf9f58e to your computer and use it in GitHub Desktop.
board_row = [
'x', 'x', 'x',
'o', 'o', 'o',
'', '', ''
]
board_col = [
'x', 'o', '',
'x', 'o', '',
'x', 'o', ''
]
board_diag = [
'x', '', '',
'', 'x', '',
'', '', 'x'
]
board_diag_2 = [
'', '', 'x',
'', 'x', '',
'x', '', ''
]
width, height = 3, 3
def is_complete(iterable):
if '' in iterable:
return False
if len(set(iterable)) == 1:
return iterable[0]
def is_row_complete(board, row):
selected_row = board[row * 3:row * 3 + 3]
return is_complete(selected_row)
def is_col_complete(board, col):
selected_column = board[col::width]
return is_complete(selected_column)
def is_top_diagonal_complete(board):
top_diagonal = [board[x * (width + 1)] for x in range(width)]
return is_complete(top_diagonal)
def is_bot_diagonal_complete(board):
bot_diagonal = [board[(width - 1) * (x + 1)] for x in range(width)]
return is_complete(bot_diagonal)
assert is_bot_diagonal_complete(board_diag) is False
assert is_bot_diagonal_complete(board_diag_2) == 'x'
assert is_top_diagonal_complete(board_diag) == 'x'
assert is_top_diagonal_complete(board_diag_2) is False
assert is_row_complete(board_row, 0) == 'x'
assert is_row_complete(board_row, 1) == 'o'
assert is_row_complete(board_row, 2) is False
assert is_col_complete(board_col, 0) == 'x'
assert is_col_complete(board_col, 1) == 'o'
assert is_col_complete(board_col, 2) is False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment