-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathKnight_Tour.py
167 lines (147 loc) · 5.7 KB
/
Knight_Tour.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
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
161
162
163
164
165
166
167
import pygame
from heapq import *
import random
import time
import ExtraWidgits
import ExtraWidgits_for_Pathfinders
from threading import *
import StartProcess
import Knight_Tour
RunClock=True
class Knight():
def __init__(self, v, speed):
self.n = v
self.cb = [[0 for x in range(v)] for y in range(v)]
self.ans = []
self.speed = speed
self.WaitForEndProcess=True
self.operations=0
self.running=True
pygame.init()
self.SIDE = 600
self.block = self.SIDE // self.n
self.win = pygame.display.set_mode((self.SIDE+450, self.SIDE))
self.K_SIDE = (self.block * 3) // 4
self.knight_img = pygame.image.load('Images/knight2.png')
self.knight_img = pygame.transform.scale(self.knight_img, (self.K_SIDE, self.K_SIDE))
self.WHITE = (255, 255, 255)
self.BLACK = (0, 0, 0)
self.RED = (255, 0, 0)
pygame.display.set_caption("KNIGHT'S TOUR")
self.x = self.block // 2
self.y = self.block // 2
self.x1 = (self.block - self.K_SIDE) // 2
self.line_w = -int(-70 // self.n)
self.StartVisualization()
def StartVisualization(self):
self.grid()
AddClock = ExtraWidgits.Clock(self.win, 850, 100, 25)
AddClock.start()
AddExitText = ExtraWidgits.ExitText(self.win,725,250)
AddExitText.start()
AddMainMenuButton = ExtraWidgits_for_Pathfinders.MainMenuButton(self.win,700,300)
AddMainMenuButton.start()
StartSolving=Thread(target=self.solve)
StartSolving.start()
self.CheckActions()
def CheckActions(self):
self.X = 700
self.Y = 300
while (self.running):
try:
self.pos = pygame.mouse.get_pos()
except:
pass
for event in pygame.event.get():
if event.type==pygame.QUIT:
self.running=False
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
pygame.quit()
if self.pos[0] > self.X and self.pos[0] < self.X + 240 and self.pos[1] > self.Y and self.pos[
1] < self.Y + 35:
if event.type == pygame.MOUSEBUTTONDOWN:
try:
self.running=False
pygame.quit()
while(self.WaitForEndProcess):
pass
Process = StartProcess.START()
Process.start()
Knight_Tour.RunClock=True
except:
pass
def grid(self):
for i in range(self.n):
for j in range(self.n):
if not self.running:
break
if ((i + j) % 2 == 0):
color = self.WHITE
else:
color = self.BLACK
pygame.draw.rect(self.win, color, (i * self.block, j * self.block, self.block, self.block))
if ([i, j] in self.ans):
color = (0, 180, 0)
pygame.draw.rect(self.win, (120, 255, 0),
(self.x1 + i * self.block, self.x1 + j * self.block, self.K_SIDE, self.K_SIDE))
def show(self):
self.grid()
xx, yy = self.ans[0]
for i in range(1, len(self.ans)):
if not self.running:
break
tx, ty = self.ans[i]
pygame.draw.line(self.win, (255, 0, 0), (self.x + xx * self.block, self.x + yy * self.block),
(self.x + tx * self.block, self.x + ty * self.block), self.line_w)
xx, yy = self.ans[i]
self.win.blit(self.knight_img, (self.x1 + xx * self.block, self.x1 + yy * self.block))
update_display=pygame.Rect(0,0,self.SIDE,self.SIDE)
pygame.display.update(update_display)
def solve(self):
kx = random.randint(0, self.n - 1)
ky = random.randint(0, self.n - 1)
dx = [-2, -1, 1, 2, -2, -1, 1, 2]
dy = [1, 2, 2, 1, -1, -2, -2, -1]
for k in range(self.n ** 2):
if not self.running:
break
self.operations+=1
self.cb[ky][kx] = k + 1
self.ans.append([kx, ky])
self.show()
if not self.running:
break
time.sleep(1/self.speed)
pq = []
for i in range(8):
self.operations+=1
if not self.running:
break
nx = kx + dx[i]
ny = ky + dy[i]
if 0 <= nx < self.n and 0 <= ny < self.n:
if self.cb[ny][nx] == 0:
ctr = 0
for j in range(8):
self.operations+=1
if not self.running:
break
ex = nx + dx[j]
ey = ny + dy[j]
if 0 <= ex < self.n and 0 <= ey < self.n:
if self.cb[ey][ex] == 0: ctr += 1
heappush(pq, (ctr, i))
if len(pq) > 0:
(p, m) = heappop(pq)
kx += dx[m]
ky += dy[m]
else:
break
if self.running:
ExtraWidgits.OperationsDoneKnight(self.operations, self.win, 700, 400, "Knight's Tour Completed.")
Knight_Tour.RunClock=False
self.WaitForEndProcess=False
return True