Created
December 22, 2022 10:21
-
-
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.
This file contains hidden or 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
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