Skip to content

Instantly share code, notes, and snippets.

@razodactyl
Created December 22, 2022 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razodactyl/0cb0121f2678e26d931c4f536a3fbf13 to your computer and use it in GitHub Desktop.
Save razodactyl/0cb0121f2678e26d931c4f536a3fbf13 to your computer and use it in GitHub Desktop.
Print out a previous game of Tic-Tac-Toe given an encoded replay string.
from typing import List
if __name__ == "__main__":
def pretty_print_board(board: List[List[str]]) -> None:
print("\n".join([",".join(row) for row in board]))
def recreate_board(replay_str: str, print_moves=False) -> List[List[str]]:
board = [['#' for _ in range(3)] for _ in range(3)]
for move in replay_str.split(';'):
if move:
player = move[0]
row, col = int(move[1]) - 1, int(move[3]) - 1
board[int(row)][int(col)] = player
if print_moves:
print("Player '{}' moved to ({}, {})".format(player, row+1, col+1))
return board
games = [
"x1,1;o1,3;x3,1;o1,2;x2,1;",
"x2,2;o3,3;x1,1;o3,1;x1,3;o3,2;",
]
for game in games:
board = recreate_board(game, True)
pretty_print_board(board)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment