-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (64 loc) · 2.36 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import tkinter as tk
from tkinter import messagebox
import random
def check_win(board, player):
return ((board[0] == board[1] == board[2] == player) or
(board[3] == board[4] == board[5] == player) or
(board[6] == board[7] == board[8] == player) or
(board[0] == board[3] == board[6] == player) or
(board[1] == board[4] == board[7] == player) or
(board[2] == board[5] == board[8] == player) or
(board[0] == board[4] == board[8] == player) or
(board[2] == board[4] == board[6] == player))
def check_tie(board):
return " " not in board
def get_available_moves(board):
return [i for i, x in enumerate(board) if x == " "]
def ai_move(board):
for move in get_available_moves(board):
board[move] = "X"
if check_win(board, "X"):
return move
board[move] = " "
for move in get_available_moves(board):
board[move] = "O"
if check_win(board, "O"):
board[move] = "X"
return move
board[move] = " "
return random.choice(get_available_moves(board))
def player_move(button, board):
index = buttons.index(button)
if board[index] == " ":
board[index] = "O"
button.config(text="O", state="disabled")
if check_win(board, "O"):
messagebox.showinfo("Tic Tac Toe", "You win!")
reset_game()
elif check_tie(board):
messagebox.showinfo("Tic Tac Toe", "It's a tie!")
reset_game()
else:
ai_index = ai_move(board)
buttons[ai_index].config(text="X", state="disabled")
if check_win(board, "X"):
messagebox.showinfo("Tic Tac Toe", "AI wins!")
reset_game()
elif check_tie(board):
messagebox.showinfo("Tic Tac Toe", "It's a tie!")
reset_game()
def reset_game():
for button in buttons:
button.config(text=" ", state="normal")
global board
board = [" " for _ in range(9)]
root = tk.Tk()
root.title("Tic Tac Toe")
board = [" " for _ in range(9)]
buttons = []
for i in range(9):
button = tk.Button(root, text=" ", font=("Helvetica", 20), width=3, height=1,
command=lambda i=i: player_move(buttons[i], board))
button.grid(row=i // 3, column=i % 3)
buttons.append(button)
root.mainloop()