Here’s another one of my useless programs. I normally write code while I’m watching TV for something else to do. On Saturday and Sunday I spent both days watching the football playoffs all day. This code tries to solve solutions for the knights tour. That’s the chess piece (knight) moving on a checker board so it makes a valid move and lands on each square without landing on a previously occupied square. There are a lot of solutions and even more paths that aren’t. So here’s a visual of the knights moves. A knight can make 8 valid moves from a square. I try a move and if it’s lands on a square already occupied, I try the next move going in a counter clockwise direction. If after the 8 tries, I’ll backup to the previous square and try the next move. The program will pause when a solution is found or you can tap the screen to pause it. The first one is at 54,482,161 tries. When the program starts, it makes 1 try per draw cycle. Opening up the print pane, you can move the slider to increase it to 1,000,000 moves per draw cycle. I show different counts as the program runs. As I said, I wrote this while watching TV, so it might not display correctly on every device. This only starts at the lower left square. If you have questions, just ask.
viewer.mode=FULLSCREEN
function setup()
sx,sy=1,1
parameter.integer("tries",0,6)
piecePos=1
pieceMove={vec2(-1,2),vec2(-2,1),vec2(-2,-1),vec2(-1,-2),
vec2(1,-2),vec2(2,-1),vec2(2,1),vec2(1,2)}
rectMode(CENTER)
tab={}
board={}
for x=1,8 do
board[x]={}
for y=1,8 do
board[x][y]=0
end
end
for z=1,64 do
table.insert(tab,piece(vec2(0,0),z))
end
size=80
tab[1].x=sx
tab[1].y=sy
board[sx][sy]=1
count=0
high=0
solutions=0
end
function draw()
background(0)
drawBoard()
for z=1,10^tries do
tab[piecePos]:next()
end
if piecePos>high then
high=piecePos
end
fontSize(20)
fill(255)
text("Rotations",WIDTH/2,HEIGHT-20)
for a,b in pairs(tab) do
text(string.format("%d",b.rot),a*12+20,HEIGHT-50)
end
text("Nbr of Tries "..count,WIDTH-150,HEIGHT-80)
text("Current Pos "..piecePos,WIDTH-150,HEIGHT-110)
text("Highest Pos "..high,WIDTH-150,HEIGHT-140)
text("Solutions "..solutions,WIDTH-150,HEIGHT-170)
text("+ 1 + 8 +",WIDTH-150,HEIGHT-200)
text("2 + + + 7",WIDTH-150,HEIGHT-220)
text("+ + O + +",WIDTH-150,HEIGHT-240)
text("3 + + + 6",WIDTH-150,HEIGHT-260)
text("+ 4 + 5 +",WIDTH-150,HEIGHT-280)
text("Tries per draw-cycle "..10^tries,WIDTH/2,HEIGHT-100)
if wait then
text("PAUSED",WIDTH/2,HEIGHT-130)
end
text("3 solutions at ",WIDTH-150,HEIGHT-310)
text("54,482,161",WIDTH-150,HEIGHT-330)
text("57,053,315",WIDTH-150,HEIGHT-350)
text("57,059,627",WIDTH-150,HEIGHT-370)
end
function touched(t)
if t.state==BEGAN then
wait=not wait
if high==64 then
high=0
end
end
end
function drawBoard()
strokeWidth(1)
fill(255)
if wait then
fill(0, 201, 255)
end
for x=1,8 do
for y=1,8 do
rect(x*size,y*size,size,size)
end
end
for z=1,piecePos do
tab[z]:draw()
end
end
piece = class()
function piece:init(pos,val)
self.x=pos.x
self.y=pos.y
self.rot=0
self.val=val
end
function piece:draw()
stroke(0)
strokeWidth(4)
for z=2,piecePos do
line(tab[z].x*size,tab[z].y*size,tab[z-1].x*size,tab[z-1].y*size)
end
fontSize(60)
fill(255,0,0)
if self.x>0 then
text(self.val,self.x*size,self.y*size)
end
end
function piece:next()
if wait then
return
end
count=count+1
while true do
self.rot=self.rot+1
if self.rot>8 then
self.rot=0
board[self.x][self.y]=0
piecePos=piecePos-1
return
else
tx=tab[piecePos].x+pieceMove[self.rot].x
ty=tab[piecePos].y+pieceMove[self.rot].y
if tx<1 or ty<1 or tx>8 or ty>8 or board[tx][ty]~=0 then
--
else
tab[piecePos+1].x=tx
tab[piecePos+1].y=ty
board[tx][ty]=piecePos+1
piecePos=piecePos+1
if piecePos==64 then
wait=true
solutions=solutions+1
end
return
end
end
end
end