Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
# Neccessary imports for the project
from IPython.display import clear_output
from random import randint
# Initializers for global variables, just a <space> that will later replace with 'X' or 'O'.
board_elements = [' '] * 9
def board(elements):
'''
Function to display the board view of the game.
Here I used the order of normal number keys in the right side of the std keyboard,
you can customize this for your keyboard. try it!
'''
print('------------------------')
print(f' {elements[6]} | {elements[7]} | {elements[8]} ')
print('------------------------')
print(f' {elements[3]} | {elements[4]} | {elements[5]} ')
print('------------------------')
print(f' {elements[0]} | {elements[1]} | {elements[2]} ')
print('------------------------')
def checkWinner():
'''
Function to check for a winner.
Leave if the sequence is other than 'X' or 'O'
Winning sequences : 123,456,789,147,258,369,159,753.
'''
if(board_elements[0] != ' ' and len(set(board_elements[0:3]))==1):
return board_elements[0]
elif(board_elements[3] != ' ' and len(set(board_elements[3:6]))==1):
return board_elements[3]
elif(board_elements[6] != ' ' and len(set(board_elements[6:9]))==1):
return board_elements[6]
elif(board_elements[0] != ' ' and len(set(board_elements[0::3]))==1):
return board_elements[0]
elif(board_elements[1] != ' ' and len(set(board_elements[1::3]))==1):
return board_elements[1]
elif(board_elements[2] != ' ' and len(set(board_elements[2::3]))==1):
return board_elements[2]
elif(board_elements[2] != ' ' and len(set(board_elements[2:8:2]))==1):
return board_elements[2]
elif(board_elements[0] != ' ' and len(set(board_elements[0::4]))==1):
return board_elements[0]
else:
return 0
def firstUser():
'''
Function to ask the user to choose either X or O
Returns either X or O, default X (if entry is even wrong)
'''
value = input('Choose who are you, X or O : ').upper()
if(value in ['X','O']):
return value
else:
print("That was a invalid entry, we choose 'X' for you.")
return 'X'