-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtik_tak_toe.py
More file actions
148 lines (125 loc) · 3.58 KB
/
Copy pathtik_tak_toe.py
File metadata and controls
148 lines (125 loc) · 3.58 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
import tkinter as tk
xo = False
values = [""] * 9
ai_mode = False
game_over = False
root = tk.Tk()
root.title("tik tak toe")
class AI:
def __init__(self):
self.values = [""] * 9
def test_for_2(self, board, symbol):
wins = [
(0, 1, 2),
(3, 4, 5),
(6, 7, 8),
(0, 3, 6),
(1, 4, 7),
(2, 5, 8),
(0, 4, 8),
(2, 4, 6)
]
for a, b, c in wins:
line = [board[a], board[b], board[c]]
if line[0] == line[1] == symbol and line[2] == "":
return c
if line[1] == line[2] == symbol and line[0] == "":
return a
if line[0] == line[2] == symbol and line[1] == "":
return b
return None
def test(self, board):
move = self.test_for_2(board, "O")
if move is not None:
return move
move = self.test_for_2(board, "X")
if move is not None:
return move
if board[4] == "":
return 4
for i in [0, 2, 6, 8]:
if board[i] == "":
return i
for i in [1, 3, 5, 7]:
if board[i] == "":
return i
return None
def move(self, value):
global values, xo
if value is None:
return
values[value] = "O"
text(value, values[value])
def test_win(X = ""):
global game_over
global xo
if game_over:
return
if values[0] == values[1] == values[2] == X != "" or values[3] == values[4] == values[5] == X != "" or values[6] == values[7] == values[8] == X != "":
game_over = True
root.destroy()
print(f"{X} win")
return
if values[0] == values[3] == values[6] == X != "" or values[1] == values[4] == values[7] == X != "" or values[2] == values[5] == values[8] == X != "":
game_over = True
root.destroy()
print(f"{X} win")
return
if values[0] == values[4] == values[8] == X != "" or values[2] == values[4] == values[6] == X != "":
game_over = True
root.destroy()
print(f"{X} win")
return
if all(v != "" for v in values):
game_over = True
root.destroy()
print("keiner gewinnt!")
def text(index, text_):
buttons[index]["text"] = text_
def on_click(index):
global xo, game_over
if game_over:
return
if values[index] != "":
return
if ai_mode:
values[index] = "X"
text(index, values[index])
if game_over:
return
xo = not xo
move = ai_obj.test(values)
ai_obj.move(move)
test_win("X")
test_win("O")
else:
values[index] = "X" if xo else "O"
xo = not xo
text(index, values[index])
test_win("X")
test_win("O")
for r in range(3):
root.rowconfigure(r, weight=1)
for c in range(3):
root.columnconfigure(c, weight=1)
buttons = []
for i in range(9):
btn = tk.Button(root,
text=" ",
font=("Arial", 32),
command=lambda idx=i: on_click(idx))
btn.grid(row=i//3, column=i%3, padx=5, pady=5, sticky="nsew")
buttons.append(btn)
ai_mode = False if input("ai mode [y/n]?: ") == "n" else True
ai_obj = AI() if ai_mode else None
if ai_mode:
print("ai mode")
else:
print("no ai, 2 player mode")
def main():
root.lift()
root.attributes("-topmost", True)
root.after(100, lambda: root.attributes("-topmost", False))
root.mainloop()
if __name__ == "__main__":
main()