forked from RodolfoFerro/KnightsTour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Knights.py
executable file
·155 lines (140 loc) · 3.8 KB
/
Knights.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pygame, sys
from pygame.locals import *
import numpy as np
def inRangeAndEmpty(posx,posy,board,N):
return (posx < N and posx >= 0 and posy < N and posy >= 0 and board[posx][posy] == 0)
def getAccessibility(x,y,moves,board,N):
accessibility = 0
for i in range(8):
if inRangeAndEmpty(x+moves[i][0],y+moves[i][1],board,N):
accessibility += 1
return accessibility
def getNextMoves(move,moves,board,N):
positionx = move[0]
positiony = move[1]
accessibility = 8
for i in range(8):
newx = positionx + moves[i][0]
newy = positiony + moves[i][1]
newacc = getAccessibility(newx,newy,moves,board,N)
if inRangeAndEmpty(newx,newy,board,N) and newacc < accessibility:
move[0] = newx
move[1] = newy
accessibility = newacc
return
def graphicTour(N,L_coor):
horse = pygame.image.load("knight.png")
# Initialize window size and title:
pygame.init()
window = pygame.display.set_mode((32*N,32*N))
pygame.display.set_caption("Knight's Tour")
background = pygame.image.load("chess.png")
index = 0
# Text:
font = pygame.font.SysFont("Ubuntu",16)
text = []
surface = []
while True:
# Fill background:
window.blit(background,(0,0))
if index < N*N:
window.blit(horse,(L_coor[index][0]*32,L_coor[index][1]*32))
text.append(font.render(str(index+1),True,(255,255,255)))
surface.append(text[index].get_rect())
surface[index].center = (L_coor[index][0]*32+16,L_coor[index][1]*32+16)
index += 1
else:
window.blit(horse,(L_coor[index-1][0]*32,L_coor[index-1][1]*32))
for x in range(10000000):
pass
# Check events on window:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == 27:
pygame.quit()
sys.exit()
for i in range(index):
window.blit(text[i],surface[i])
# Update window:
pygame.display.update()
def ifSolution(Board,N):
for i in range(N):
for j in range(N):
if Board[i][j] == 0:
return False
return True
# Inicializamos las variables:
N = int(input("Enter N, size of the board (NxN): "))
positionx = int(input("Enter initial x position: "))%N
positiony = int(input("Enter initial y position: "))%N
x = positionx
y = positiony
moveNumber = 2
move = [positionx,positiony]
moves = [[2,1],[2,-1],[1,2],[1,-2],[-1,2],[-1,-2],[-2,1],[-2,-1]]
Board = np.zeros([N,N])
Board[positionx][positiony] = 1
L = []
# Buscamos la solución y aplicamos Wansdorff:
for i in range(N*N):
move[0] = positionx
move[1] = positiony
getNextMoves(move,moves,Board,N)
positionx = move[0]
positiony = move[1]
Board[positionx][positiony] = moveNumber
moveNumber += 1
Board[positionx][positiony] -= 1
# Revisamos si encontramos solución:
sol = ifSolution(Board,N)
if sol:
# Añadimos las posiciones a la lista de coordenadas L:
k = 1
while k <= N*N:
for i in range(N):
for j in range(N):
if Board[i][j] == k:
L.append([i,j])
k += 1
print(Board)
else:
moves = [[2,1],[-2,1],[2,-1],[-2,-1],[1,2],[-1,2],[1,-2],[-1,-2]]
Board = np.zeros([N,N])
positionx = x
positiony = y
Board[positionx][positiony] = 1
L = []
moveNumber = 2
move = [positionx,positiony]
# Buscamos la solución y aplicamos Wansdorff:
for i in range(N*N):
move[0] = positionx
move[1] = positiony
getNextMoves(move,moves,Board,N)
positionx = move[0]
positiony = move[1]
Board[positionx][positiony] = moveNumber
moveNumber += 1
Board[positionx][positiony] -= 1
# Revisamos si encontramos solución:
sol = ifSolution(Board,N)
if sol:
# Añadimos las posiciones a la lista de coordenadas L:
k = 1
while k <= N*N:
for i in range(N):
for j in range(N):
if Board[i][j] == k:
L.append([i,j])
k += 1
print(Board)
if len(L) == 0:
print("Didn't find a solution.")
print("Knights' positions: ", L)
if N <= 32 and sol:
graphicTour(N,L)