forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.py
130 lines (101 loc) · 4.15 KB
/
TicTacToe.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
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
import tkinter
def set_title(row,column):
if game_over:
return
global currentPlayer
# Taken spot
if board[row][column]["text"] != "":
return
board[row][column]["text"] = currentPlayer
#switch player
if currentPlayer == playerO:
currentPlayer = playerX
else:
currentPlayer = playerO
label["text"] = currentPlayer+"'s turn"
# check winner
check_winner()
def check_winner():
global turns, game_over
turns += 1
# horizontally check 3 rows
for row in range(3):
if board[row][0]["text"]== board[row][1]["text"] == board[row][2]["text"] and board[row][0]["text"]!="":
label.config(text=board[row][0]["text"]+" is the winner!", foreground=color_yellow)
for column in range(3):
board[row][column].config(foreground=color_yellow,background=color_ligh_gray)
game_over = True
return
# vertically check 3 columns
for column in range(3):
if board[0][column]["text"] == board[1][column]["text"] == board[2][column]["text"] and board[0][column]["text"] != "":
label.config(text=board[0][column]["text"]+" is the winner!", foreground=color_yellow)
for row in range(3):
board[row][column].config(foreground=color_yellow,background=color_ligh_gray)
game_over = True
return
# Diagonally check
if board[0][0]["text"] == board[1][1]["text"] == board[2][2]["text"] and board[0][0]["text"] !="":
label.config(text=board[0][0]["text"]+" is the winner!", foreground=color_yellow)
for i in range(3):
board[i][i].config(foreground=color_yellow,background=color_ligh_gray)
game_over = True
return
#anti-diagionally
if (board[0][2]["text"] == board[1][1]["text"] == board[2][0]["text"]
and board[0][2]["text"] != ""):
label.config(text=board[0][2]["text"]+" is the winner!", foreground=color_yellow)
board[0][2].config(foreground=color_yellow, background=color_ligh_gray)
board[1][1].config(foreground=color_yellow, background=color_ligh_gray)
board[2][0].config(foreground=color_yellow, background=color_ligh_gray)
game_over = True
return
#tie
if (turns == 9):
game_over = True
label.config(text="Tie!", foreground=color_yellow)
def new_game():
global game_over,turns
turns = 0
game_over = False
label.config(text=currentPlayer+"'s turn", foreground="white")
for row in range(3):
for column in range(3):
board[row][column].config(text="", foreground=color_blue, background=color_gray)
playerX = "X"
playerO = "O"
currentPlayer = playerX
board = [["0","0","0"],
["0","0","0"],
["0","0","0"]]
turns = 0
game_over = False
color_blue = "#4584b6"
color_yellow = "#ffde57"
color_gray = "#343434"
color_ligh_gray = "#646464"
window = tkinter.Tk()
window.title("Tic Tac Toe")
window.resizable(False,False)
frame = tkinter.Frame(window)
label = tkinter.Label(frame,text=currentPlayer+"'s turn",font=("Consolas",20), background= color_gray,foreground="white")
label.grid(row=0,column=0,columnspan=3,sticky="we")
for row in range(3):
for column in range(3):
board[row][column] = tkinter.Button(frame,text="",font=("Consolas",50,"bold"),background= color_gray,foreground=color_blue,width=4,height=1,
command=lambda row=row, column=column:set_title(row,column))
board[row][column].grid(row = row+1,column = column)
button = tkinter.Button(frame,text="Restart",font=("Consolas",20), background= color_gray,foreground="white",command=new_game)
button.grid(row=4,column=0,columnspan=3,sticky="we")
frame.pack()
# center the window
window.update()
window_width = window.winfo_width()
window_height = window.winfo_height()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
window_x = int((screen_width/2) - (window_width/2))
window_y = int((screen_height/2) - (window_height/2))
#formate
window.geometry(f"{window_width}x{window_height}+{window_x}+{window_y}")
window.mainloop()