Skip to content

Instantly share code, notes, and snippets.

@philsinatra
Created July 9, 2019 15:24
Show Gist options
  • Save philsinatra/ec451e8a9c1ac21f325a1191a1de5d83 to your computer and use it in GitHub Desktop.
Save philsinatra/ec451e8a9c1ac21f325a1191a1de5d83 to your computer and use it in GitHub Desktop.
Python Tic Tac Toe Game
#!/usr/local/bin/python3
import random
import time
test_board = ['#', 'X', '0', 'X', '0', 'X', '0', 'X', '0', 'X']
game_board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
def display_board(board):
print('\n' * 100)
print(board[7] + ' | ' + board[8] + ' | ' + board[9])
print('---------')
print(board[4] + ' | ' + board[5] + ' | ' + board[6])
print('---------')
print(board[1] + ' | ' + board[2] + ' | ' + board[3])
def player_input():
pawns_set = False
while pawns_set == False:
p1 = input('Player 1: Do you want to be X or 0? ')
if (p1 == 'X' or p1 == '0'):
pawns_set = True
if p1 == 'X':
p2 = '0'
else:
p2 = 'X'
print(f'Player 1 is {p1}\nPlayer 2 is {p2}')
return (p1, p2)
def place_marker(board, marker, position):
board[position] = str(marker)
def win_check(board, mark):
return ((board[7] == mark and board[8] == mark and board[9] == mark)
or # across the top
(board[4] == mark and board[5] == mark
and board[6] == mark) or # across the middle
(board[1] == mark and board[2] == mark and board[3] == mark)
or # across the bottom
(board[7] == mark and board[4] == mark
and board[1] == mark) or # down the middle
(board[8] == mark and board[5] == mark and board[2] == mark)
or # down the middle
(board[9] == mark and board[6] == mark
and board[3] == mark) or # down the right side
(board[7] == mark and board[5] == mark and board[3] == mark)
or # diagonal
(board[9] == mark and board[5] == mark and board[1] == mark)
) # diagonal
def choose_first():
first = random.randrange(1, 3)
print('Randomly choosing who goes first...\n')
time.sleep(1)
print(f'Player {first} goes first.\n')
if first == 'X':
return 'X'
else:
return '0'
def space_check(board, position):
return board[position] == ' '
def full_board_check(board):
for i in range(1, 10):
if space_check(board, i):
return False
return True
def will_it_float(value):
try:
float(value)
return True
except ValueError:
return False
def player_choice(board):
position = 0
while position not in [1, 2, 3, 4, 5, 6, 7, 8, 9
] or not space_check(board, position):
position = input('Choose your next position: (1-9) ')
if will_it_float(position):
position = int(position)
if position not in range(1, 10):
print('You must enter a number between 1-9.')
continue
else:
print('You must enter a number between 1-9.')
continue
return position
def replay():
return input(
'Do you want to play again? Enter Yes or No: ').lower().startswith('y')
def init():
print('Welcome to Tic Tac Toe!')
while True:
game_board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
player_input()
whos_turn = choose_first()
while True:
selected_position = player_choice(game_board)
place_marker(game_board, whos_turn, selected_position)
display_board(game_board)
win = win_check(game_board, whos_turn)
if win:
print(f'Player {whos_turn} wins!')
break
board_full = full_board_check(game_board)
if board_full:
print('No one wins.')
break
if whos_turn == 'X':
whos_turn = '0'
else:
whos_turn = 'X'
if not replay():
break
init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment