def check_draw(ttt):
for slot in ttt.slots:
if slot == "":
return False
return True
def display_detail():
print("File : tic_tac_toe.py")
print("Author : Dinh Chu Vi Co")
print("Student ID : 43793")
print("Email ID : dinchu.vico@gmail.com")
print("This is my own work as defined by the University’s")
print("Academic Misconduct Policy")
def check_win(slots, letter):
Check horizontal rows
if slots[0] == letter and slots[1] == letter and slots[2] == letter:
return True
elif slots[3] == letter and slots[4] == letter and slots[5] == letter:
return True
elif slots[6] == letter and slots[7] == letter and slots[8] == letter:
return True
# Check vertical columns
elif slots[0] == letter and slots[3] == letter and slots[6] == letter:
return True
elif slots[1] == letter and slots[4] == letter and slots[7] == letter:
return True
elif slots[2] == letter and slots[5] == letter and slots[8] == letter:
return True
# Check diagonal lines
elif slots[0] == letter and slots[4] == letter and slots[8] == letter:
return True
elif slots[2] == letter and slots[4] == letter and slots[6] == letter:
return True
return False
# Rule 1: Check for winning move
index = 0
while index < len(slots):
if slots[index] == '':
slots[index] = 'O'
if check_win(slots, 'O'):
ttt.move_computer(index)
index += 10
else:
slots[index] = ''
index += 1
# Rule 2: Check for block of a player win
if not win_found:
index = 0
while index < len(slots):
if slots[index] == '':
slots[index] = 'X'
if check_win(slots, 'X'):
ttt.move_computer(index)
index += 10
else:
slots[index] = ''
index += 1
# Rule 3: Check for any corners free
if not win_found and not block_found:
if slots[0] == '':
ttt.move_computer(0)
elif slots[2] == "":
ttt.move_computer(2)
elif slots[6] == "":
ttt.move_computer(6)
elif slots[8] == "":
ttt.move_computer(8)
# Rule 4: Check if the center is free
elif slots[4] == "":
ttt.move_computer(4)
# Rule 5: Move to any free side space
elif slots[1] == '':
ttt.move_computer(1)
elif slots[3] == "":
ttt.move_computer(3)
elif slots[5] == "":
ttt.move_computer(5)
elif slots[7] == "":
ttt.move_computer(7)
print("That was fun!")
valid_choices = ["y", "n"]
enter = ""
while enter not in valid_choices:
enter = input("Do you want to play again (y/n): ").lower()
if enter not in valid_choices:
print("Invalid input. Please enter 'y' or 'n'.")
return enter
The rest of your functions
Main game loop
games_played = 0
display_detail()
play_game = input("\nWould you like to play Tic Tac Toe? [y/n]: ").lower()
Validate input
valid_choices = ["y","n"]
while play_game not in valid_choices:
play_game = input("Would you like to play Tic Tac Toe? [y/n]: ").lower()
If they enter 'y' the prompt for name
if play_game == "y":
name = input("Please enter your name: ")
ttt = tic_tac_toe_gui.TicTacToeGUI(name)
while play_game == "y":
while not check_win(ttt.slots, "X") and not check_win(ttt.slots, "O") and not check_draw(ttt):
if not ttt.player_turn:
move_computer(ttt)
try:
ttt.main_window.update()
except (tkinter.TclError, KeyboardInterrupt):
quit(0)
# Check if the player or the computer has won, or if it's a draw
if check_win(ttt.slots, "X"):
print("-"*3,"Player wins","-"*3)
play_game = end_game()
ttt.increment_wins()
elif check_win(ttt.slots, "O"):
print("-"*3,"Computer wins","-"*3)
play_game = end_game()
ttt.increment_losses()
elif check_draw(ttt):
play_game = end_game()
ttt.draw()
games_played += 1
print("Finished")
Display game statistics (wins, losses, and draws) after the player finishes playing
import tic_tac_toe_gui import tkinter
def display_game(slots): print(slots)
def check_draw(ttt): for slot in ttt.slots: if slot == "": return False
def display_detail(): print("File : tic_tac_toe.py") print("Author : Dinh Chu Vi Co") print("Student ID : 43793") print("Email ID : dinchu.vico@gmail.com") print("This is my own work as defined by the University’s") print("Academic Misconduct Policy")
def check_win(slots, letter):
Check horizontal rows
def move_computer(ttt): slots = list(ttt.slots) win_found = False block_found = False
def end_game(): print("-" 23, "END GAME", "-" 23) if check_win(ttt.slots, "X"): print("-"3,"Player wins!","-"3) elif check_win(ttt.slots, "O"): print("-"3,"Computer wins!","-"3) else: print("Tie game!")
The rest of your functions
Main game loop
games_played = 0 display_detail() play_game = input("\nWould you like to play Tic Tac Toe? [y/n]: ").lower()
Validate input
valid_choices = ["y","n"] while play_game not in valid_choices: play_game = input("Would you like to play Tic Tac Toe? [y/n]: ").lower()
If they enter 'y' the prompt for name
if play_game == "y": name = input("Please enter your name: ") ttt = tic_tac_toe_gui.TicTacToeGUI(name)
while play_game == "y": while not check_win(ttt.slots, "X") and not check_win(ttt.slots, "O") and not check_draw(ttt):
print("Finished")
Display game statistics (wins, losses, and draws) after the player finishes playing
print("Wins:", ttt.get_wins()) print("Losses:", ttt.get_losses())
keep track of games played
use formula: games_played - ttt.get_wins() - ttt.get_losses()
print("Draws:", games_played - ttt.get_wins() - ttt.get_losses())