Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions ExtraWidgits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import pygame
import time
from threading import *
import SUDOKU
import N_Queen
import Knight_Tour


class Clock(Thread):
def __init__(self,screen,CordinateX,CordinateY,font,*args):
Thread.__init__(self)
self.screen = screen
self.CordinateX=CordinateX
self.CordinateY=CordinateY
self.ClockComplete=False
self.font=font
self.running=True
self.Clock=[0,0]
self.printTime= "00:00"

def run(self):
try:
while(N_Queen.RunClock and Knight_Tour.RunClock and SUDOKU.RunClock):
pygame.font.init()

myfont = pygame.font.SysFont('Comic Sans MS', self.font)
self.textsurface = myfont.render(self.printTime, False, (255, 255, 255))
self.screen.blit(self.textsurface, (self.CordinateX-50, self.CordinateY))

myfont = pygame.font.SysFont('Comic Sans MS', self.font)
self.textsurface = myfont.render(" Time Escaped:", False, (255, 255, 255))
self.screen.blit(self.textsurface, (self.CordinateX-120, self.CordinateY-30))


time.sleep(1)
if not N_Queen.RunClock or not Knight_Tour.RunClock or not SUDOKU.RunClock:
break
textsurface = myfont.render(self.printTime, False, (0, 0, 0))
self.screen.blit(textsurface, (self.CordinateX-50, self.CordinateY))


if not self.ClockComplete:

if self.Clock[1]==59:

self.Clock[0]+=1
self.Clock[1]=0
if len(str(self.Clock[0])) == 1:
self.printTime = '0' + str(self.Clock[0])
else:
self.printTime = str(self.Clock[0])
self.printTime=str(self.Clock[0])+':'+ '0' + str(self.Clock[1])

else:

self.Clock[1]+=1
if len(str(self.Clock[0])) == 1 and len(str(self.Clock[1]))==1:
self.printTime='0'+str(self.Clock[0])+':' +'0' + str(self.Clock[1])
elif len(str(self.Clock[1]))==1:
self.printTime=str(self.Clock[0])+':'+ '0' + str(self.Clock[1])
elif len(str(self.Clock[0])) == 1:
self.printTime = '0'+str(self.Clock[0]) + ':' + str(self.Clock[1])
else:
self.printTime = str(self.Clock[0])+ ':' + str(self.Clock[1])

myfont = pygame.font.SysFont('Comic Sans MS', self.font)
textsurface = myfont.render(self.printTime, False, (255, 255, 255))
self.screen.blit(textsurface, (self.CordinateX-50, self.CordinateY))
except:
pass




class MainMenuButton(Thread):
def __init__(self,screen,CordinateX,CordinateY,*args):
Thread.__init__(self)
self.X=CordinateX
self.Y=CordinateY
self.screen = screen
self.CordinateX=CordinateX
self.CordinateY=CordinateY
self.ClockComplete=False

def run(self):
try:
while(True):
self.pos=pygame.mouse.get_pos()

self.OnMainMenuButton=False
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:
self.OnMainMenuButton=True

pygame.font.init()

if self.OnMainMenuButton == False:
pygame.draw.rect(self.screen,(255,255,255),(self.X,self.Y,240,35))
myfont = pygame.font.SysFont('Comic Sans MS', 20)
textsurface = myfont.render("Go Back to Main Menu", False, (0, 0, 0))
self.screen.blit(textsurface, (self.X+17, self.Y))
else:
pygame.draw.rect(self.screen,(0,0,0),(self.X,self.Y,240,35))
pygame.draw.rect(self.screen,(255,255,255),(self.X,self.Y,240,35),3)
myfont = pygame.font.SysFont('Comic Sans MS', 20)
textsurface = myfont.render("Go Back to Main Menu", False, (255, 255, 255))
self.screen.blit(textsurface, (self.X+17, self.Y))
pygame.event.get()
pygame.display.update()
except:
pass



class ExitText(Thread):
def __init__(self,screen,CordinateX,CordinateY,*args):
Thread.__init__(self)
self.X=CordinateX
self.Y=CordinateY
self.screen = screen
self.CordinateX=CordinateX
self.CordinateY=CordinateY
self.ClockComplete=False

def run(self):
try:
while(True):
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 20)
textsurface = myfont.render("Press Esc to Exit.", False, (255, 255, 255))
self.screen.blit(textsurface, (self.X+17, self.Y))
except:
pass



class OperationsDone:
def __init__(self,NoOfOperations,screen,X,Y,String):
try:
self.NoOfOperations=NoOfOperations
self.screen=screen
self.X=X
self.Y=Y
pygame.font.init()

myfont = pygame.font.SysFont('Comic Sans MS', 20)
textsurface = myfont.render(String, False, (255, 255, 255))
self.screen.blit(textsurface, (self.X + 50, self.Y))
myfont = pygame.font.SysFont('Comic Sans MS', 20)
textsurface = myfont.render("Operations Performed: "+str(self.NoOfOperations), False, (255, 255, 255))
self.screen.blit(textsurface, (self.X, self.Y+40))
except:
pass


class OperationsDoneKnight:
def __init__(self,NoOfOperations,screen,X,Y,String):
try:
self.NoOfOperations=NoOfOperations
self.screen=screen
self.X=X
self.Y=Y

pygame.font.init()

myfont = pygame.font.SysFont('Comic Sans MS', 20)
textsurface = myfont.render(String, False, (255, 255, 255))
self.screen.blit(textsurface, (self.X+10, self.Y))
myfont = pygame.font.SysFont('Comic Sans MS', 20)
textsurface = myfont.render("Operations Performed: "+str(self.NoOfOperations), False, (255, 255, 255))
self.screen.blit(textsurface, (self.X, self.Y+40))
except:
pass
File renamed without changes.
Binary file added Images/knight2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/queen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
161 changes: 161 additions & 0 deletions Knight_Tour.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import pygame
from heapq import *
import random
import time
import ExtraWidgits
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()
AddMainMenuButton = ExtraWidgits.MainMenuButton(self.win,700,300)
AddMainMenuButton.start()
AddExitText = ExtraWidgits.ExitText(self.win,725,250)
AddExitText.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))



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
Loading