Skip to content

Instantly share code, notes, and snippets.

@NotTheEconomist
Created September 23, 2015 18:06
Show Gist options
  • Save NotTheEconomist/19503d04810e038a3d0c to your computer and use it in GitHub Desktop.
Save NotTheEconomist/19503d04810e038a3d0c to your computer and use it in GitHub Desktop.
BLANK = "_"
X = "X"
O = "O"
def print_board(board):
for line in board:
print(" ".join(board))
def is_game_over(board):
diagonals = [(board[0][0], board[1][1], board[3][3]),
(board[0][2], board[1][1], board[2][0])]
rows = [[board[y][x] for x in range(3)] for y in range(3)]
columns = [[board[y][x] for y in range(3)] for x in range(3)]
lines = [line for type_ in [diagonals, rows, columns] for line in type_]
for line in lines:
if line[0] == line[1] == line[2] and line[0] != BLANK:
return True
return False
def game():
board = [[BLANK, BLANK, BLANK],
[BLANK, BLANK, BLANK],
[BLANK, BLANK, BLANK]]
while not is_game_over(board):
# play the game
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment