Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/krestiki-noliki.py
5918 views
1
# ВНИМАНИЕ! Это код для учеников - тут есть 5 мест, где делаем оптимизацию кода и меняем X на Y :)
2
# ATTENTION! This is code for students - there are 5 places where we optimize the code and change X to Y :)
3
4
from tkinter import *
5
from tkinter import messagebox
6
import time
7
import random
8
9
tk = Tk()
10
app_running = True
11
12
size_canvas_x = 768
13
size_canvas_y = 768
14
15
def on_closing():
16
global app_running
17
if messagebox.askokcancel("Выход из игры", "Хотите выйти из игры?"):
18
app_running = False
19
tk.destroy()
20
21
22
tk.protocol("WM_DELETE_WINDOW", on_closing)
23
24
tk.title("Игра крестики-нолики")
25
tk.resizable(0, 0)
26
tk.wm_attributes("-topmost", 1)
27
canvas = Canvas(tk, width=size_canvas_x, height=size_canvas_y, bd=0, highlightthickness=0)
28
canvas.create_rectangle(0,0,size_canvas_x, size_canvas_y,fill="white")
29
canvas.pack()
30
tk.update()
31
32
33
s_x = 3
34
s_y = s_x
35
step_x = size_canvas_x // s_x
36
step_y = size_canvas_y // s_y
37
38
def draw_table():
39
for i in range(0, s_x + 1):
40
canvas.create_line(0, i * step_y, size_canvas_x, i * step_y)
41
for i in range(0,s_y+1):
42
canvas.create_line(i*step_y,0,i*step_y,size_canvas_y)
43
44
#points = [[-1,-1,-1],[-1,-1,-1],[-1,-1,-1]]
45
points = [[-1 for i in range(s_x)] for i in range(s_x)]
46
print(points)
47
list_ids = []
48
draw_table()
49
50
class Point:
51
def __init__(self, x, y, type):
52
self.x = x
53
self.y = y
54
self.type = type
55
56
def __str__(self):
57
return str(self.__class__) + ": " + str(self.__dict__)
58
59
def draw_point(x, y, type):
60
size = 25
61
color = "black"
62
id = 0
63
if type == 0:
64
color = "red"
65
id = canvas.create_oval(x * step_x, y * step_y, x * step_x + step_x, y * step_y + step_y, fill=color)
66
id2 = canvas.create_oval(x * step_x + size, y * step_y + size, x * step_x + step_x - size, y * step_y + step_y - size, fill="white")
67
list_ids.append(id)
68
list_ids.append(id2)
69
if type == 1:
70
color = "blue"
71
id = canvas.create_rectangle(x * step_x, y * step_y+ step_y//2-step_y//10, x * step_x+step_x, y * step_y + step_y//2+step_y//10, fill=color)
72
id2 = canvas.create_rectangle(x * step_x+ step_x // 2 - step_x // 10, y * step_y, x * step_x+ step_x // 2 + step_x // 10, y * step_y + step_y, fill=color)
73
list_ids.append(id)
74
list_ids.append(id2)
75
76
print(type)
77
#id = canvas.create_oval(x*step_x, y*step_y, x*step_x+step_x, y*step_y+step_y, fill=color)
78
79
def add_to_points(event):
80
#print(event.num, event.x, event.y)
81
global points
82
type = 0
83
if event.num == 3:
84
type = 1
85
if points[event.x // step_x][event.y // step_y] == -1:
86
points[event.x // step_x][event.y // step_y] = type
87
draw_point(event.x // step_x, event.y // step_y, type)
88
if check_winner(type):
89
print("Победитель", type)
90
points = [[10 for i in range(s_x)] for i in range(s_x)]
91
#print(" ".join(map(str, points)))
92
93
canvas.bind_all("<Button-1>", add_to_points) # ЛКМ
94
canvas.bind_all("<Button-3>", add_to_points) # ПКМ
95
96
def button_press():
97
global list_ids
98
global points
99
print(list_ids)
100
for i in list_ids:
101
canvas.delete(i)
102
list_ids = []
103
print(list_ids)
104
points = [[-1 for i in range(s_x)] for i in range(s_x)]
105
106
b1 = Button(tk, text="Начать заново!", command=button_press)
107
b1.pack()
108
109
def check_winner(who):
110
for j in range(0,s_y):
111
win = True
112
for i in range(0,s_x):
113
if points[j][i] != who:
114
win = False
115
if win:
116
return True
117
for j in range(0,s_y):
118
win = True
119
for i in range(0,s_x):
120
if points[i][j] != who:
121
win = False
122
if win:
123
return True
124
125
win = True
126
for i in range(0,s_y):
127
print(points[i][i])
128
if points[i][i] != who:
129
win = False
130
if win:
131
return True
132
133
while app_running:
134
if app_running:
135
tk.update_idletasks()
136
tk.update()
137
time.sleep(0.005)
138
139