|
|
|
# ----------global variables------------
|
|
|
|
# https://www.youtube.com/watch?v=BHh654_7Cmw
|
|
|
|
# Create a board
|
|
|
|
import random
|
|
|
|
|
|
|
|
board = ['-', '-', '-',
|
|
|
|
'-', '-', '-',
|
|
|
|
'-', '-', '-']
|
|
|
|
|
|
|
|
# if game is still going
|
|
|
|
GAME_GOING = True
|
|
|
|
# who won?
|
|
|
|
WINNER = None
|
|
|
|
# define valid symbols
|
|
|
|
SYMBOLS = {'X', 'O'}
|
|
|
|
# define players
|
|
|
|
PLAYER1 = ""
|
|
|
|
PLAYER2 = ""
|
|
|
|
# define how many players
|
|
|
|
SINGLE_PLAYER = True
|
|
|
|
|
|
|
|
CURRENT_PLAYER = random.choice(list(SYMBOLS))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global CURRENT_PLAYER
|
|
|
|
global SINGLE_PLAYER
|
|
|
|
global PLAYER1
|
|
|
|
global PLAYER2
|
|
|
|
|
|
|
|
mode = input("Single player mode y/n: ")
|
|
|
|
if mode not in "yn":
|
|
|
|
print("You did not select a valid option no game for you")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if mode == "y":
|
|
|
|
SINGLE_PLAYER = True
|
|
|
|
else:
|
|
|
|
SINGLE_PLAYER = False
|
|
|
|
|
|
|
|
player_1 = input("Choose X or O for Player 1: ")
|
|
|
|
|
|
|
|
if player_1 not in SYMBOLS:
|
|
|
|
print("You did not select a valid option no game for you")
|
|
|
|
exit(1)
|
|
|
|
PLAYER1 = player_1
|
|
|
|
PLAYER2 = (SYMBOLS - {player_1}).pop()
|
|
|
|
|
|
|
|
# while the game is still going
|
|
|
|
while GAME_GOING:
|
|
|
|
display_board()
|
|
|
|
# handle a turn
|
|
|
|
handle_turn()
|
|
|
|
# import pdb; pdb.set_trace()
|
|
|
|
# check if he game has ended
|
|
|
|
check_if_game_over()
|
|
|
|
# cahnge to the other player
|
|
|
|
CURRENT_PLAYER = (SYMBOLS - {CURRENT_PLAYER}).pop()
|
|
|
|
|
|
|
|
# the game has ended
|
|
|
|
display_board()
|
|
|
|
if WINNER == 'X' or WINNER == 'O':
|
|
|
|
print(WINNER + " won.")
|
|
|
|
elif WINNER == None:
|
|
|
|
print("Tie.")
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
def display_board():
|
|
|
|
print(board[0] + '|' + board[1] + '|' + board[2])
|
|
|
|
print(board[3] + '|' + board[4] + '|' + board[5])
|
|
|
|
print(board[6] + '|' + board[7] + '|' + board[8])
|
|
|
|
|
|
|
|
|
|
|
|
# play game of tic tac toe
|
|
|
|
|
|
|
|
def handle_turn():
|
|
|
|
# import pdb; pdb.set_trace()
|
|
|
|
global board
|
|
|
|
print("Current Player {}".format(CURRENT_PLAYER))
|
|
|
|
while True:
|
|
|
|
if SINGLE_PLAYER:
|
|
|
|
if CURRENT_PLAYER == PLAYER1:
|
|
|
|
# take player input
|
|
|
|
choice = input('Choose a position from 1 to 9: ')
|
|
|
|
else:
|
|
|
|
choice = random.choice(find_open_spaces()) + 1
|
|
|
|
else:
|
|
|
|
# take player input
|
|
|
|
choice = input('Choose a position from 1 to 9: ')
|
|
|
|
choice = int(choice) - 1
|
|
|
|
if validate_choice(choice):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
print("Invalid choice")
|
|
|
|
board[choice] = CURRENT_PLAYER
|
|
|
|
|
|
|
|
|
|
|
|
def validate_choice(choice):
|
|
|
|
is_valid = False
|
|
|
|
try:
|
|
|
|
choice = int(choice)
|
|
|
|
except ValueError:
|
|
|
|
return is_valid
|
|
|
|
|
|
|
|
if choice in list(range(0, 9)) and choice in find_open_spaces():
|
|
|
|
is_valid = True
|
|
|
|
|
|
|
|
return is_valid
|
|
|
|
|
|
|
|
|
|
|
|
def check_if_game_over():
|
|
|
|
check_if_win()
|
|
|
|
|
|
|
|
check_if_tie()
|
|
|
|
|
|
|
|
|
|
|
|
def check_if_win():
|
|
|
|
# call global variable
|
|
|
|
global WINNER
|
|
|
|
# check rows
|
|
|
|
row_winner = check_rows()
|
|
|
|
# check columns
|
|
|
|
column_winner = check_columns()
|
|
|
|
# check diagonals
|
|
|
|
diagonal_winner = check_diagonal()
|
|
|
|
|
|
|
|
if row_winner:
|
|
|
|
# there is a winner
|
|
|
|
WINNER = row_winner
|
|
|
|
elif column_winner:
|
|
|
|
# there is a winner
|
|
|
|
WINNER = column_winner
|
|
|
|
elif diagonal_winner:
|
|
|
|
# there is a winner
|
|
|
|
WINNER = diagonal_winner
|
|
|
|
else:
|
|
|
|
WINNER = None
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def check_rows():
|
|
|
|
global GAME_GOING
|
|
|
|
row_1 = board[0] == board[1] == board[2] != '-'
|
|
|
|
row_2 = board[3] == board[4] == board[5] != '-'
|
|
|
|
row_3 = board[6] == board[7] == board[8] != '-'
|
|
|
|
if row_1 or row_2 or row_3:
|
|
|
|
GAME_GOING = False
|
|
|
|
# display the winner
|
|
|
|
if row_1:
|
|
|
|
return board[0]
|
|
|
|
if row_2:
|
|
|
|
return board[3]
|
|
|
|
if row_3:
|
|
|
|
return board[6]
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def check_columns():
|
|
|
|
global GAME_GOING
|
|
|
|
column_1 = board[0] == board[3] == board[6] != '-'
|
|
|
|
column_2 = board[1] == board[4] == board[7] != '-'
|
|
|
|
column_3 = board[2] == board[5] == board[8] != '-'
|
|
|
|
# display the winner
|
|
|
|
if any((column_1, column_2, column_3)):
|
|
|
|
GAME_GOING = False
|
|
|
|
if column_1:
|
|
|
|
return board[0]
|
|
|
|
if column_2:
|
|
|
|
return board[1]
|
|
|
|
if column_3:
|
|
|
|
return board[2]
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def check_diagonal():
|
|
|
|
global GAME_GOING
|
|
|
|
diagonal_1 = board[0] == board[4] == board[8] != '-'
|
|
|
|
diagonal_2 = board[2] == board[4] == board[6] != '-'
|
|
|
|
# display the winner
|
|
|
|
if diagonal_1 or diagonal_2:
|
|
|
|
GAME_GOING = False
|
|
|
|
if diagonal_1:
|
|
|
|
return board[0]
|
|
|
|
if diagonal_2:
|
|
|
|
return board[2]
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# check_if_tie
|
|
|
|
|
|
|
|
def check_if_tie():
|
|
|
|
global GAME_GOING
|
|
|
|
if "-" not in board:
|
|
|
|
GAME_GOING = False
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def find_open_spaces():
|
|
|
|
# find a random space to select
|
|
|
|
open_spaces = []
|
|
|
|
for i in range(0, len(board)):
|
|
|
|
if board[i] == "-":
|
|
|
|
open_spaces.append(i)
|
|
|
|
|
|
|
|
return open_spaces
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|