Tic Tac Toe
#TicTacToe
#dictionary
#--get
#--set
#functions
#logic
#if
#while
theBoard = {
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
}
def printBoard(board):
print(board['1'] + '|' + board['2'] + '|' + board['3'])
print('-+-+-')
print(board['4'] + '|' + board['5'] + '|' + board['6'])
print('-+-+-')
print(board['7'] + '|' + board['8'] + '|' + board['9'])
def clearTheBoard(board):
for key in board:
board[key] = " "
def checkForWinner(board, whoJustWent):
#CHECK ROWS
# top row
if board['1'] == whoJustWent and board ['2'] == whoJustWent and board['3'] == whoJustWent:
print(f'{whoJustWent} is the WINNER!!!!')
return True
#mid row
if board['4'] == whoJustWent and board ['5'] == whoJustWent and board['6'] == whoJustWent:
print(f'{whoJustWent} is the WINNER!!!!')
return True
#end row
if board['7'] == whoJustWent and board ['8'] == whoJustWent and board['9'] == whoJustWent:
print(f'{whoJustWent} is the WINNER!!!!')
return True
#CHECK COLUMNS
#left col
if board['1'] == whoJustWent and board ['4'] == whoJustWent and board['7'] == whoJustWent:
print(f'{whoJustWent} is the WINNER!!!!')
return True
#mid col
if board['2'] == whoJustWent and board ['5'] == whoJustWent and board['8'] == whoJustWent:
print(f'{whoJustWent} is the WINNER!!!!')
return True
#right col
if board['3'] == whoJustWent and board ['6'] == whoJustWent and board['9'] == whoJustWent:
print(f'{whoJustWent} is the WINNER!!!!')
return True
return False
#our title
print('Tic-Tac-Toe \n\n\n')
isThereAWinner = False
printBoard(theBoard)
clearTheBoard(theBoard)
currentPlayer = 'X'
while isThereAWinner == False:
userMove = input(f'Choose your space {currentPlayer}:')
theBoard[userMove] = currentPlayer
printBoard(theBoard)
isThereAWinner = checkForWinner(theBoard, currentPlayer)
print(f'winner: {isThereAWinner}')
if currentPlayer == 'X':
currentPlayer = 'O'
elif currentPlayer == 'O':
currentPlayer = 'X'