I tried to do a tic tac toe game, so, to base myself in something, I searched through multiple tic tac toe codes and took some ideas from them to implement on my code. Here's the result and I would like anyone to make an analisys of it and tell me your opinion about it!
It's almost completely funcional, the only thing that needs to be fixed is the A.I. because there isn't any of it there, it just chooses a random number to place X or O. Later I'll try to build it by myself.
# Simple TIC TAC TOE game
import random
import time
board = {1: ' ', 2: ' ', 3: ' ',
4: ' ', 5: ' ', 6: ' ',
7: ' ', 8: ' ', 9: ' '}
winning_combinations = ((1, 2, 3), (4, 5, 6), (7, 8, 9), # Horizontals
(1, 4, 7), (2, 5, 8), (3, 6, 9), # Verticals
(1, 5, 9), (3, 5, 7)) # Diagonals
# Displays the visual output in the terminal
def print_board():
print(board[1] + '|' + board[2] + '|' + board[3])
print('-+-+-')
print(board[4] + '|' + board[5] + '|' + board[6])
print('-+-+-')
print(board[7] + '|' + board[8] + '|' + board[9])
print('\n')
# Defines who starts the game by chance
def who_starts():
sides = ['X', 'O']
print('The game is starting.')
print('Flipping coin...')
time.sleep(1)
if random.random() > 0.5:
print('The computer won.\n')
computer_side = random.choice(sides)
sides.remove(computer_side)
player_side = sides[0]
print(f"The computer has chosen to be {computer_side}.")
print(f"You will be {player_side[0]}.")
return player_side, computer_side, False # This boolean will be used later on play()
else:
print('You won.\n')
while True:
player_side = input('Choose your side: ').upper()
if player_side not in sides:
print('You can only choose X or O\n')
else:
sides.remove(player_side)
computer_side = sides[0]
print(f'You have chosen to be {player_side}.')
print(f'The computer will be {computer_side}.')
return player_side, computer_side, True # This boolean will be used later on play()
# Allows the player place X or O in the board. The 'side' means whether X or O
def player_place(side):
check_if_full()
occupied = False
while not occupied:
try:
local = int(input('Choose somewhere to place it (1-9): '))
if local not in range(1, 10):
print('Invalid number. ')
else:
if board[local] != ' ':
print('This place is already occupied. \n')
else:
board[local] = side
print_board()
time.sleep(0.5)
check_win(side, 'O') if side == 'X' else check_win(side, 'X')
occupied = True
except ValueError:
print('Enter a number! ')
# Allows the (dumb!) A.I. to place X or O in the board. The 'side' means whether X or O
def ai_place(side):
check_if_full()
local = random.randint(1, 9)
while board[local] != ' ':
local = random.randint(1, 9)
board[local] = side
time.sleep(1)
print_board()
time.sleep(0.5)
check_win('O', side) if side == 'X' else check_win('X', side)
# Analyses winning patters in the board, if it detects it, it will end the game
def check_win(player_side, computer_side):
for combination in winning_combinations:
player_index = 0
computer_index = 0
for integer in combination:
if board[integer] == player_side:
player_index += 1
if player_index == 3:
print('You won!. GAME OVER')
exit()
elif board[integer] == computer_side:
computer_index += 1
if computer_index == 3:
print('You lost!, the AI defeated you. GAME OVER')
exit()
# Checks if the board is full
def check_if_full():
count = 0
for i in range(1, 10):
if board[i] != ' ':
count += 1
if count == 9:
print('GAME OVER')
exit()
# Executes the game with a loop based on the boolean value of the variable 'value'
def play():
player, computer, value = who_starts()
while True:
if value is True:
player_place(player), ai_place(computer) # The player won, so will start first
else:
ai_place(computer), player_place(player) # The computer won, starting first
# Driver code
if __name__ == '__main__':
play()
```