-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
153 lines (127 loc) · 4.97 KB
/
main.py
File metadata and controls
153 lines (127 loc) · 4.97 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import tkinter
from tkinter import *
import customtkinter
from board import Board
from mediumbot import mediumbot
from player import Player
from easybot import easybot
from hardbot import hardbot
board = Board()
player1 = Player.x
player2 = Player.o
bot = None
status = None
def raise_frame(frames, mode):
global status
global bot
global board
global buttons
global winningLabel
if mode == "Easy":
status = "AI"
bot = easybot("O")
elif mode == "Medium":
status = "AI"
bot = mediumbot("O")
elif mode == "Hard":
status = "AI"
bot = hardbot("O")
elif mode == "co-op":
status = "co-op"
elif mode == "Main Menu":
board = Board()
bot = None
status = None
buttons = create_board()
winningLabel.configure(text="")
frames.tkraise()
def handle_click(shape, move, box):
global board
global buttons
global bot
global status
gameBoard = board
if gameBoard.find_winner() is None and len(gameBoard.moves) < 9:
if gameBoard.is_space_empty(move[0], move[1]):
if len(gameBoard.moves) in [0, 2, 4, 6, 8] and status == "AI" and len(gameBoard.moves) != 8:
gameBoard.make_move(move[0], move[1], "X")
buttons[move[0]][move[1]].configure(background="grey", text="X")
if gameBoard.find_winner() is None:
botmove = bot.select_move(gameBoard)
board.make_move(botmove[0], botmove[1], "O")
buttons[botmove[0]][botmove[1]].configure(background="grey", text="O")
elif len(gameBoard.moves) in [1, 3, 5, 7, 9] and status == "co-op":
gameBoard.make_move(move[0], move[1], "O")
buttons[move[0]][move[1]].configure(background="grey", text="O")
elif len(gameBoard.moves) in [0, 2, 4, 6, 8]:
gameBoard.make_move(move[0], move[1], "X")
buttons[move[0]][move[1]].configure(background="grey", text="X")
if len(gameBoard.moves) >= 5:
if gameBoard.find_winner() == 'X':
box.configure(text="X is the Winner!")
box.lift()
elif gameBoard.find_winner() == 'O':
box.configure(text="O is the Winner!")
box.lift()
elif len(gameBoard.moves) == 9 and gameBoard.find_winner() is None:
box.configure(text="Tie!")
box.lift()
def create_board():
res = []
for a in range(3):
temp = []
for b in range(3):
button = tkinter.Button(gameFrame, text=".", height=5)
button.grid(row=a, column=b, sticky="nsew", padx=10, pady=10)
button.configure(command=lambda row=a, column=b: handle_click(button['text'],
[row, column], winningLabel))
temp.append(button)
res.append(temp)
return res
# System Settings
customtkinter.set_appearance_mode("System")
# App
root = tkinter.Tk()
root.geometry("800x600")
root.title("TicTacToe")
# root.resizable(False, False)
# Frame
startingFrame = tkinter.Frame(root)
AIFrame = tkinter.Frame(root)
gameFrame = tkinter.Frame(root)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
for frame in (startingFrame, AIFrame, gameFrame):
frame.grid(row=0, column=0, sticky="nsew")
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
# UI Elements
title = tkinter.Label(startingFrame, text="Select a Mode!")
title.pack(side=tkinter.TOP, padx=310, pady=25)
oneplayer = Button(startingFrame, text="One Player", command=lambda: raise_frame(AIFrame, "AI"))
oneplayer.pack(side=tkinter.TOP, pady=10)
twoplayer = tkinter.Button(startingFrame, text="Two Player", command=lambda: raise_frame(gameFrame, "co-op"))
twoplayer.pack(side=tkinter.TOP)
# choosing AI difficulty
back = tkinter.Button(AIFrame, text="Back", command=lambda: raise_frame(startingFrame, "start"))
back.pack(side=tkinter.BOTTOM, anchor=SW, padx=10, pady=10)
aimode = tkinter.Label(AIFrame, text="Select a Difficulty!")
aimode.pack(side=tkinter.TOP, padx=310, pady=10)
easy = Button(AIFrame, text="Easy", command=lambda: raise_frame(gameFrame, "Easy"))
easy.pack(side=tkinter.TOP, pady=10)
medium = Button(AIFrame, text="Medium", command=lambda: raise_frame(gameFrame, "Medium"))
medium.pack(side=tkinter.TOP, pady=10)
hard = Button(AIFrame, text="Hard", command=lambda: raise_frame(gameFrame, "Hard"))
hard.pack(side=tkinter.TOP, pady=10)
# gameboard
winningLabel = tkinter.Label(gameFrame, text="", height=15, width=15)
winningLabel.grid(row=3, column=0, columnspan=3, pady=10)
buttons = create_board()
back2 = tkinter.Button(gameFrame, text="Main Menu", command=lambda: raise_frame(startingFrame, "Main Menu"))
back2.grid(row=4, column=0, sticky="w", padx=5, pady=5)
for i in range(3):
gameFrame.grid_rowconfigure(i, weight=1)
gameFrame.grid_columnconfigure(i, weight=1)
raise_frame(startingFrame, "start")
# Running
root.mainloop()