-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.oo.py
110 lines (96 loc) · 3.22 KB
/
x.oo.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
import tkinter as tk
from tkinter.messagebox import showinfo
window = tk.Tk()
window.title("XO")
global turn, results, player_points
turn ="X"
results = ["", "", "", "", "", "", "", "", ""]
player_points = [0, 0]
def clicked(btn):
global turn
btn = int(btn)
if results[btn] == "":
if turn == "X":
results[btn] = "X"
buttons[btn]["bg"]="red"
buttons[btn]["fg"]="black"
buttons[btn]["text"]="X"
buttons[btn]["relief"]=tk.GROOVE
# buttons[btn]['state']=tk.DISABLED
turn = "O"
else:
results[btn] = "O"
buttons[btn]["bg"]="green"
buttons[btn]["fg"]="black"
buttons[btn]["text"]="O"
# buttons[btn]['state']=tk.DISABLED
turn = "X"
rule()
def rule():
if (results[0]==results[1]==results[2] and results[0]!=""):
show_winner(results[0])
elif(results[3]==results[4]==results[5] and results[3]!=""):
show_winner(3)
elif(results[6]==results[7]==results[8] and results[6]!=""):
show_winner(6)
elif(results[0]==results[3]==results[6] and results[0]!=""):
show_winner(0)
elif(results[1]==results[4]==results[7] and results[1]!=""):
show_winner(1)
elif(results[2]==results[5]==results[8] and results[2]!=""):
show_winner(2)
elif(results[0]==results[4]==results[8] and results[0]!=""):
show_winner(0)
elif(results[2]==results[4]==results[6] and results[2]!=""):
show_winner(2)
else:
check_draw()
def show_winner(winner):
if winner == "X":
player_points[0] += 1
showinfo("game end", "player 1 win")
reset()
else:
player_points[1] += 1
showinfo("game end", "player 2 win")
reset()
def reset():
global results, turn
results = ["", "", "", "", "", "", "", "", ""]
turn = "X"
points()
board()
def check_draw():
if "" not in results:
showinfo("game end","game equal")
reset()
def points():
board_frame = tk.Frame(window)
board_frame.grid(row=0)
label_player_one = tk.Label(board_frame, text="player1", font=("Aviny",16), padx=10)
label_player_two = tk.Label(board_frame, text="player2", font=("Aviny",16), padx=10)
label_player_one.grid(row=0, column=0)
label_player_two.grid(row=0, column=2)
point_frame = tk.Frame(window)
point_frame.grid(row=1)
point_player_one = tk.Label(point_frame, text=player_points[0], padx=20, font=("Lalezar", 18))
point_player_two = tk.Label(point_frame, text=player_points[1], padx=20, font=("Lalezar", 18))
point_player_one.grid(row=0, column=0)
point_player_two.grid(row=0, column=1)
def board():
global buttons
buttons = []
counter = 0
board_frame = tk.Frame(window)
board_frame.grid(row=2)
for row in range(1, 4):
for column in range(1, 4):
index = counter
buttons.append(index)
buttons[index] = tk.Button(board_frame, command=lambda x=f"{index}":clicked(x))
buttons[index].config(width=10, height=4, font=("None", 18, "bold"))
buttons[index].grid(row=row, column=column)
counter += 1
points()
board()
window.mainloop()