Created
December 14, 2018 18:49
-
-
Save jfkelley/6009fcc9b6686040b8667c52576804e2 to your computer and use it in GitHub Desktop.
This file contains 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 threes(board): | |
for x in [0, 1, 2]: | |
yield [board[x][0], board[x][1], board[x][2]] | |
yield [board[0][x], board[1][x], board[2][x]] | |
yield [board[0][0], board[1][1], board[2][2]] | |
yield [board[0][2], board[1][1], board[2][0]] | |
def is_done(board): | |
for th in threes(board): | |
if th[0] is not None and th[0] == th[1] and th[1] == th[2]: | |
return True | |
return False | |
def count_games(board, player): | |
if is_done(board): | |
return 1 | |
next_player = 0 if player == 1 else 1 | |
total = 0 | |
for r in [0, 1, 2]: | |
for c in [0, 1, 2]: | |
if board[r][c] is None: | |
board[r][c] = player | |
total += count_games(board, next_player) | |
board[r][c] = None | |
return total | |
start_board = [ | |
[None, None, None], | |
[None, None, None], | |
[None, None, None] | |
] | |
print(count_games(start_board, 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment