-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.py
More file actions
160 lines (130 loc) · 4.99 KB
/
Board.py
File metadata and controls
160 lines (130 loc) · 4.99 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
154
155
156
157
158
159
160
from PIL import Image, ImageTk
import PIL
from tkinter import *
import tkinter
import math
import AStar
class chess_board:
startXY = None
endXY = None
to_green = AStar.Green.TO_GREEN
def __init__(self, board_size):
self.chess_board_arr = []
self.root = Tk()
self.default_size = board_size * 60
self.start_flag = False
self.finish_flag = False
s_img = PIL.Image.open("img/start.png")
s_img = s_img.resize((60, 60))
self.start_img = ImageTk.PhotoImage(s_img)
g_img = PIL.Image.open("img/finish.png")
g_img = g_img.resize((60, 60))
self.goal_img = ImageTk.PhotoImage(g_img)
img = PIL.Image.open("img/wall2.png")
img = img.resize((60, 60))
self.wall_image = ImageTk.PhotoImage(img)
p_img = PIL.Image.open("img/way.png")
p_img = p_img.resize((59, 59))
self.path_img = ImageTk.PhotoImage(p_img)
child_img = PIL.Image.open("img/child2.png")
child_img = child_img.resize((59, 59))
self.children_img = ImageTk.PhotoImage(child_img)
self.root.title("A* MAZE")
self.root.minsize(width=self.default_size +
150, height=self.default_size)
self.root.maxsize(width=self.default_size +
150, height=self.default_size)
self.canvas = Canvas(
self.root, width=self.default_size, height=self.default_size)
self.canvas.pack(expand=YES, fill=BOTH)
self.createCanvas(board_size)
# Find the path button
find_path_button = Button(
self.canvas, text="Find the path", command=self.showCalculatedWay) # then doo it
find_path_button.pack()
find_path_button.place(x=self.default_size + 50, y=10)
restart_button = Button(self.root, text="Restart",
command=self.restart_program)
restart_button.place(x=self.default_size + 50, y=50)
# Bind button clicks
self.canvas.bind("<Button-1>", self.mouse_click)
#self.canvas.bind("<B1-Motion>", self.mouse_click)
self.canvas.bind("<Button-2>", self.MidMouseClick)
self.canvas.bind("<Button-3>", self.RightMouseClick)
self.root.mainloop()
@staticmethod
def showChessArray(self):
print(self.chess_board_arr)
def returnChessArray(self):
return self.chess_board_arr
def restart_program(self):
AStar.CHILDREN_MOVE = []
self.root.destroy()
chess_board(10)
def mouse_click(self, event):
# Wall
x = math.ceil(event.x / 60 - 1)
y = math.ceil(event.y / 60 - 1)
if self.chess_board_arr[x][y] == 0:
self.putFigure(x, y)
self.chess_board_arr[x][y] = 1
else:
self.deleteFigure(x, y)
self.chess_board_arr[x][y] = 0
def createCanvas(self, n):
for i in range(n):
temp_arr = []
for j in range(n):
self.deleteFigure(i, j)
temp_arr.append(0)
self.chess_board_arr.append(temp_arr)
def putFigure(self, x, y):
self.canvas.create_image(
x * 60 + 30, y * 60 + 30, image=self.wall_image)
def deleteFigure(self, x, y):
self.canvas.create_rectangle(
x * 60, y * 60, x * 60 + 60, y * 60 + 60, fill="#F9F9F9")
def RightMouseClick(self, event):
# START
if self.start_flag:
return
x = math.ceil(event.x / 60 - 1)
y = math.ceil(event.y / 60 - 1)
if self.chess_board_arr[x][y] == 0:
self.putStart(x, y)
self.startXY = (y, x)
self.start_flag = True
else:
self.deleteFigure(x, y)
self.start_flag = False
def putStart(self, x, y):
self.canvas.create_image(
x * 60 + 30, y * 60 + 30, image=self.start_img)
def MidMouseClick(self, event):
# END
if self.finish_flag:
return
x = math.ceil(event.x / 60 - 1)
y = math.ceil(event.y / 60 - 1)
if self.chess_board_arr[x][y] == 0:
self.putEnd(x, y)
self.endXY = (y, x)
self.finish_flag = True
else:
self.finish_flag = False
self.deleteFigure(x, y)
def putEnd(self, x, y):
self.canvas.create_image(
x * 60 + 30, y * 60 + 30, image=self.goal_img)
def showCalculatedWay(self):
AStar.solve(self)
path_array = AStar.solve(self)
# MIGHT NOT WORK PROPERLY
for each in AStar.CHILDREN_MOVE: #[:-1:]
if each != AStar.END_COORDINATE and each != AStar.START_COORDINATE and self.chess_board_arr[each[1]][each[0]] != 1:
self.canvas.create_image(
each[1] * 60 + 30, each[0] * 60 + 30, image=self.children_img)
# SHOW THE WAY OF ALGORITHM
for each in path_array[1:-1:]:
self.canvas.create_image(
each[1] * 60 + 30, each[0] * 60 + 30, image=self.path_img)