Skip to content

Instantly share code, notes, and snippets.

@bronumski
Last active March 19, 2025 18:07
Show Gist options
  • Save bronumski/e061857e2ed9428379a1806808dc0a21 to your computer and use it in GitHub Desktop.
Save bronumski/e061857e2ed9428379a1806808dc0a21 to your computer and use it in GitHub Desktop.
board = [
[None, None, None],
[None, None, None],
[None, None, None]
]
def available_goes():
return len([item for item in [x for sublist in [board[0], board[1], board[2]] for x in sublist] if item is None]) > 0
def set_position(player, x, y):
if (board[x][y] == None):
board[x][y] = player
return True
return False
def render_position(x, y):
return board[x][y] or " "
def check_winner():
if len(set(board[0])) == 1 and board[0][0] is not None:
return board[0][0]
elif len(set(board[1])) == 1 and board[1][0] is not None:
return board[1][0]
elif len(set(board[2])) == 1 and board[2][0] is not None:
return board[2][0]
elif board[0][0] is not None and board[0][0] == board[1][0] and board[1][0] == board[2][0]:
return board[0][0]
elif board[0][1] is not None and board[0][1] == board[1][1] and board[1][1] == board[2][1]:
return board[0][1]
elif board[0][2] is not None and board[0][2] == board[1][2] and board[1][2] == board[2][2]:
return board[0][2]
elif board[0][0] is not None and board[0][0] == board[1][1] and board[1][1] == board[2][2]:
return board[0][0]
elif board[0][2] is not None and board[0][2] == board[1][1] and board[1][1] == board[2][0]:
return board[0][2]
return None
def print_board():
print(" 0 1 2")
print(f"0 {render_position(0, 0)} | {render_position(0, 1)} | {render_position(0, 2)}")
print(" ---+---+---")
print(f"1 {render_position(1, 0)} | {render_position(1, 1)} | {render_position(1, 2)}")
print(" ---+---+---")
print(f"2 {render_position(2, 0)} | {render_position(2, 1)} | {render_position(2, 2)}")
winner = None
player = None
print_board()
while winner == None:
if player == "X":
player = "O"
else:
player = "X"
move = input(f"Player {player}, where do you want to go?")
x, y = [int(v) for v in move.split(" ", 1)]
if set_position(player, x, y) == False:
print("That position is already taken, try again")
continue
print_board()
winner = check_winner()
if winner is not None:
print(f"The winner was '{winner}'")
elif not available_goes():
print(f"It was a tie")
break
board = {
"A": [None, None, None],
"B": [None, None, None],
"C": [None, None, None]
}
def available_goes():
return len([item for item in [x for sublist in [board["A"], board["B"], board["C"]] for x in sublist] if item is None]) > 0
def set_position(player, x, y):
if (board[x][y] == None):
board[x][y] = player
return True
return False
def render_position(x, y):
return board[x][y] or " "
def check_winner():
if len(set(board["A"])) == 1 and board["A"][0] is not None:
return board["A"][0]
elif len(set(board["B"])) == 1 and board["B"][0] is not None:
return board["B"][0]
elif len(set(board["C"])) == 1 and board["C"][0] is not None:
return board["C"][0]
elif board["A"][0] is not None and board["A"][0] == board["B"][0] and board["B"][0] == board["C"][0]:
return board["A"][0]
elif board["A"][1] is not None and board["A"][1] == board["B"][1] and board["B"][1] == board["C"][1]:
return board["A"][1]
elif board["A"][2] is not None and board["A"][2] == board["B"][2] and board["B"][2] == board["C"][2]:
return board[0][2]
elif board["A"][0] is not None and board["A"][0] == board["B"][1] and board["B"][1] == board["C"][2]:
return board["A"][0]
elif board["A"][2] is not None and board["A"][2] == board["B"][1] and board["B"][1] == board["C"][0]:
return board["A"][2]
return None
def print_board():
print(" A B C")
print(f"0 {render_position('A', 0)} | {render_position('B', 0)} | {render_position('C', 0)}")
print(" ---+---+---")
print(f"1 {render_position('A', 1)} | {render_position('B', 1)} | {render_position('C', 1)}")
print(" ---+---+---")
print(f"2 {render_position('A', 2)} | {render_position('B', 2)} | {render_position('C', 2)}")
winner = None
player = None
print_board()
while winner == None:
if player == "X":
player = "O"
else:
player = "X"
move = input(f"Player {player}, where do you want to go?")
x, y = [char if index == 0 else int(char) for index, char in enumerate(move)]
if set_position(player, x, y) == False:
print("That position is already taken, try again")
continue
print_board()
winner = check_winner()
if winner is not None:
print(f"The winner was '{winner}'")
elif not available_goes():
print(f"It was a tie")
break
board = {
"A": [None, None, None],
"B": [None, None, None],
"C": [None, None, None]
}
def render_position(x, y):
return board[x][y] or " "
def print_board():
print(f" A B C")
print(f"0 {render_position('A', 0)} | {render_position('B', 0)} | {render_position('C', 0)}")
print(f" ---+---+---")
print(f"1 {render_position('A', 1)} | {render_position('B', 1)} | {render_position('C', 1)}")
print(f" ---+---+---")
print(f"2 {render_position('A', 2)} | {render_position('B', 2)} | {render_position('C', 2)}")
def set_position(x, y, player):
if (x not in ["A", "B", "C"] or y not in [0, 1, 2]):
print("Not a valid position, try again")
return True
if (board[x][y] == None):
board[x][y] = player
print("That position is already taken, try again")
return True
return False
winner = None
player = None
print_board()
while winner == None:
if player == "X":
player = "O"
else:
player = "X"
move = input(f"Player {player}, where do you want to go?")
x, y = [char if index == 0 else int(char) for index, char in enumerate(move)]
if set_position(x, y, player) == False:
print("That position is already taken, try again")
continue
print_board()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment