Created
April 28, 2023 04:27
-
-
Save CalK16/e0a7135f00d12f5bc34be0f98d471161 to your computer and use it in GitHub Desktop.
A tic-tac-toe in python
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
import os | |
GAME_BOARD = [[' '] * 3 for _ in range(3)] | |
COL = ROW = 3 | |
def print_game_board(): | |
count_row = 0 | |
for row in GAME_BOARD: | |
a_row = "" | |
for col in row: | |
a_row += "┃" + col | |
a_row += "┃" | |
if count_row == 0: | |
print("┏━┳━┳━┓") | |
else: | |
print("┣━╋━╋━┫") | |
count_row += 1 | |
print(a_row) | |
print("┗━┻━┻━┛") | |
def draw(who, x, y): | |
if x >= COL or x < 0: | |
print("invalid position, x should be in range (0, 2)") | |
return False | |
if y >= ROW or y < 0: | |
print("invalid position, y should be in range (0, 2)") | |
return False | |
if GAME_BOARD[x][y] != ' ': | |
print("position", x, ",", y, "is taken!") | |
return False | |
GAME_BOARD[x][y] = who | |
return True | |
def _3_in_a_row(target): | |
cross_1st_row = ( | |
GAME_BOARD[0][0] == target | |
and GAME_BOARD[0][1] == target | |
and GAME_BOARD[0][2] == target | |
) | |
cross_2nd_row = ( | |
GAME_BOARD[1][0] == target | |
and GAME_BOARD[1][1] == target | |
and GAME_BOARD[1][2] == target | |
) | |
cross_3rd_row = ( | |
GAME_BOARD[2][0] == target | |
and GAME_BOARD[2][1] == target | |
and GAME_BOARD[2][2] == target | |
) | |
return cross_1st_row or cross_2nd_row or cross_3rd_row | |
def _3_in_a_col(target): | |
cross_1st_col = ( | |
GAME_BOARD[0][0] == target | |
and GAME_BOARD[1][0] == target | |
and GAME_BOARD[2][0] == target | |
) | |
cross_2nd_col = ( | |
GAME_BOARD[0][1] == target | |
and GAME_BOARD[1][1] == target | |
and GAME_BOARD[2][1] == target | |
) | |
cross_3rd_col = ( | |
GAME_BOARD[0][2] == target | |
and GAME_BOARD[1][2] == target | |
and GAME_BOARD[2][2] == target | |
) | |
return cross_1st_col or cross_2nd_col or cross_3rd_col | |
def _3_in_a_diagonal(target): | |
left_to_right = ( | |
GAME_BOARD[0][0] == target | |
and GAME_BOARD[1][1] == target | |
and GAME_BOARD[2][2] == target | |
) | |
right_to_left = ( | |
GAME_BOARD[0][2] == target | |
and GAME_BOARD[1][1] == target | |
and GAME_BOARD[2][0] == target | |
) | |
return left_to_right or right_to_left | |
def is_win(player): | |
return _3_in_a_row(player) or _3_in_a_col(player) or _3_in_a_diagonal(player) | |
def get_pos(inp): | |
x, y = inp.split(",") | |
return int(x), int(y) | |
def swap(player): | |
if player == "O": | |
return "X" | |
return "O" | |
def play(): | |
who = "X" | |
while True: | |
print_game_board() | |
print("please enter the position (x, y) for '", who, "'") | |
user_input = input() | |
x, y = get_pos(user_input) | |
success = draw(who, x, y) | |
if not success: | |
continue | |
if is_win(who): | |
return who | |
who = swap(who) | |
winner = play() | |
print_game_board() | |
print("Congrats!", winner, "won the game!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment