Skip to content

Instantly share code, notes, and snippets.

@zevaverbach
Last active August 17, 2018 11:44
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 zevaverbach/d2494f36a53782afea33327bb916801b to your computer and use it in GitHub Desktop.
Save zevaverbach/d2494f36a53782afea33327bb916801b to your computer and use it in GitHub Desktop.
Tic Tac Toe!
from time import sleep
BOARD_WIDTH = 17
LEFT_PAD_30 = " " * 30
LEFT_PAD_10 = " " * 10
WIN_BOXES = [(0, 1, 2),
(3, 4, 5),
(6, 7, 8),
(0, 3, 6),
(1, 4, 7),
(2, 5, 8),
(0, 4, 8),
(2, 4, 6)]
board = [" "] * 9
whose_turn = 'X'
SLEEP_BETWEEN_LINE_DRAWS_SECONDS = .01
def main():
status = None
while status is None:
get_move_and_draw_board()
status = get_game_status()
toggle_whose_turn()
if status == "draw":
print('It\'s a draw!')
else:
who_won = status
print(f'Congratulations, {who_won}, you won!')
def get_game_status():
if len(get_available_box_nums()) == 0:
return "draw"
for a, b, c in WIN_BOXES:
if board[a] == board[b] == board[c] and board[a] != ' ':
return board[a]
def get_move_and_draw_board():
available_box_nums = get_available_box_nums()
box_num = None
while box_num is None:
draw_board()
print()
try:
box_num = int(input(
f'{LEFT_PAD_10}{whose_turn}, it\'s your turn. '
f'Which box number do you want to play? [{" ".join(map(str, available_box_nums))}] '
))
except ValueError:
print('please only provide a number')
else:
if box_num not in available_box_nums:
print('that box isn\'t available!')
box_num = None
board[box_num - 1] = whose_turn
def toggle_whose_turn():
global whose_turn
whose_turn = 'X' if whose_turn == 'O' else 'O'
def get_available_box_nums():
return [idx + 1 for idx, value in enumerate(board) if value == ' ']
def draw_board():
clear_screen()
print(f'{LEFT_PAD_30} {"1" if board[0] == " " else " "} {"2" if board[1] == " " else " "} {"3" if board[2] == " " else " "}')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print()
print(f'{LEFT_PAD_30} | |')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30} {board[0]} | {board[1]} | {board[2]}')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30} | |')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30} ', '-' * BOARD_WIDTH)
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30} | |')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30}{"4" if board[3] == " " else " "} {board[3]} | {"5" if board[4] == " " else board[4]} | {board[5]} {"6" if board[5] == " " else " "}')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30} | |')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30} ', '-' * BOARD_WIDTH)
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30} | |')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30} {board[6]} | {board[7]} | {board[8]}')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print(f'{LEFT_PAD_30} | |')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
print()
print(f'{LEFT_PAD_30} {"7" if board[6] == " " else " "} {"8" if board[7] == " " else " "} {"9" if board[8] == " " else " "}')
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
for _ in range(12):
print()
sleep(SLEEP_BETWEEN_LINE_DRAWS_SECONDS)
def clear_screen():
for _ in range(80):
print()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment