Skip to content

Commit 1273a11

Browse files
committed
Fixed syntax error in TicTacToe file
1 parent e72713b commit 1273a11

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

AI Game/Tic-Tac-Toe-AI/tictactoe.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import tkinter as tk
2-
from tkinter import messagebox
1+
import tkinter as tk #provides a library of basic elements of GUI widgets
2+
from tkinter import messagebox #provides a different set of dialogues that are used to display message boxes
33
import random
44

55
def check_winner(board, player):
@@ -19,30 +19,31 @@ def minimax(board, depth, is_maximizing):
1919
return -1
2020
if check_winner(board, 'O'):
2121
return 1
22-
if is_board_full(board):
22+
if is_board_full(board): #if game is full, terminate
2323
return 0
2424

25-
if is_maximizing:
25+
if is_maximizing: #recursive approach that fills board with Os
2626
max_eval = float('-inf')
2727
for i in range(3):
2828
for j in range(3):
2929
if board[i][j] == ' ':
3030
board[i][j] = 'O'
31-
eval = minimax(board, depth + 1, False)
31+
eval = minimax(board, depth + 1, False) #recursion
3232
board[i][j] = ' '
3333
max_eval = max(max_eval, eval)
3434
return max_eval
35-
else:
35+
else: #recursive approach that fills board with Xs
3636
min_eval = float('inf')
3737
for i in range(3):
3838
for j in range(3):
3939
if board[i][j] == ' ':
4040
board[i][j] = 'X'
41-
eval = minimax(board, depth + 1, True)
41+
eval = minimax(board, depth + 1, True) #recursion
4242
board[i][j] = ' '
4343
min_eval = min(min_eval, eval)
4444
return min_eval
4545

46+
#determines the best move for the current player and returns a tuple representing the position
4647
def best_move(board):
4748
best_val = float('-inf')
4849
best_move = None
@@ -74,6 +75,7 @@ def make_move(row, col):
7475
else:
7576
messagebox.showerror("Error", "Invalid move")
7677

78+
#AI's turn to play
7779
def ai_move():
7880
row, col = best_move(board)
7981
board[row][col] = 'O'
@@ -88,7 +90,7 @@ def ai_move():
8890
root = tk.Tk()
8991
root.title("Tic-Tac-Toe")
9092

91-
board = [[' ' for _ in range(3)] for _ in range(3]
93+
board = [[' ' for _ in range(3)] for _ in range(3)]
9294
buttons = []
9395

9496
for i in range(3):

0 commit comments

Comments
 (0)