89 lines
1.8 KiB
Plaintext
89 lines
1.8 KiB
Plaintext
clear
|
|
N = 8
|
|
SOLUTIONCOUNT = 0
|
|
|
|
getTileDisplay = function
|
|
gfx.clear
|
|
queen = file.loadImage("/sys/pics/gamePieces/blackQueen.png")
|
|
gfx.color = color.white
|
|
gfx.fillRect 0, 0, 80, 80
|
|
gfx.fillRect 160, 0, 80, 80
|
|
gfx.color = color.brown
|
|
gfx.fillRect 80, 0, 80, 80
|
|
gfx.fillRect 240, 0, 80, 80
|
|
gfx.drawImage queen, 172, 14
|
|
gfx.drawImage queen, 252, 14
|
|
tiles = gfx.getImage(0,0, 320, 80)
|
|
gfx.clear
|
|
display(4).mode = displayMode.tile
|
|
td = display(4)
|
|
td.cellSize = 640 / N
|
|
td.extent = [N, N]
|
|
td.overlap = 0
|
|
td.tileSet = tiles
|
|
td.tileSetTileSize = 80
|
|
td.scrollX = -160
|
|
td.clear
|
|
return td
|
|
|
|
end function
|
|
|
|
updateBoard = function(td, arr)
|
|
for y in range(0, N - 1)
|
|
ix = y % 2
|
|
for x in range(0, N - 1)
|
|
td.setCell x, y, ix
|
|
ix += 1
|
|
ix %= 2
|
|
end for
|
|
end for
|
|
|
|
y = 0
|
|
for x in arr
|
|
td.setCell x, y, td.cell(x, y) + 2
|
|
y += 1
|
|
end for
|
|
yield
|
|
end function
|
|
|
|
list.has = function(n)
|
|
return self.indexOf(n) != null
|
|
end function
|
|
|
|
queens = function(n, i, a, b, c, td)
|
|
solutions = []
|
|
updateBoard(td, a)
|
|
if i < n then
|
|
for j in range(0, n - 1)
|
|
if not a.has(j) and not b.has(i + j) and not c.has(i - j) then
|
|
solution = queens(n, i + 1, a + [j], b + [i + j], c + [i - j], td)
|
|
if solution != null then solutions += solution
|
|
end if
|
|
end for
|
|
else
|
|
globals.SOLUTIONCOUNT += 1
|
|
text.row = 25
|
|
text.print "SOLUTIONS"
|
|
text.print globals.SOLUTIONCOUNT
|
|
solutions.push(a)
|
|
end if
|
|
return solutions
|
|
end function
|
|
|
|
td = getTileDisplay
|
|
solutions = queens(N, 0, [], [], [], td)
|
|
ix = 0
|
|
while true
|
|
text.row = 25
|
|
text.print "SOLUTION # "
|
|
text.print (ix + 1) + (" " * 10)
|
|
text.print
|
|
text.print char(17) + "/" + char(18) + " keys"
|
|
updateBoard(td, solutions[ix])
|
|
k = key.get
|
|
kcode = code(k)
|
|
if kcode == 27 then break
|
|
ix = ix - (kcode == 17) + (kcode == 18) + solutions.len
|
|
ix %= solutions.len
|
|
end while
|