hello to the round :-)
the "knight's tour" study in minimicro is a bit further.. the graphic works and the field selection with mouse (via activezone) also.
how to restart the program (after a run) with click on new field i can't find in the documentation.
in general i would like to say that the language "miniscript" is a pleasant experience. i wish it would be integrated in such an environment like "easylang-online". for (and surely not only) my modest needs an ideal solution.
the editor is in any case (like the language itself) more mature. the resulting PWA (at the push of a button!) would be generally and comfortably usable on any system.
here is the code mentioned:
// my-cb-00-03a (start field selection with mouse)
// 20210526 - 15:00
n = 8 // no of fields
cbx = n-1 // width and
cby = n-1 // height of the chessboard
bs = 36 // block size
cb = [] // make chessboard cb
for j in range(0, n-1) // fill cb with zeros
cb.push([0, 0, 0, 0, 0, 0, 0, 0]) // 8 fields
end for
clear // clear screen
print "Click on the desired Field.."
// show chessboard (botom left)
for x in range(0, cbx)
for y in range(0, cby)
if (x+y) % 2 == 0 then
gfx.color = color.red
else
gfx.color = color.yellow
end if
gfx.fillRect x*bs, y*bs, bs, bs
end for
end for
// --------------------------------------------------- functions
// show cb[] content ( debug )
prnt = function(cb)
for x in range(0, n-1)
print cb[x]
end for
end function
//between inclusive
between = function( x, min, max )
between = (x >= min) and (x <= max);
return between
end function
while true // ------------------- definition of the active zone
k = 0
doJump = function( kx, ky ) // -------------------- JUMP
// -- possible moves in all directions
// the Knight can move on the chessboard
dx = [-2, -1, 1, 2, -2, -1, 1, 2]
dy = [1, 2, 2, 1, -1, -2, -2, -1]
while k != 64
k = k+1
cb[ky][kx] = k // write first jump in cb[]
pq = [] // empty priority queue of available neighbors
// first jump start field (grafics)
x = (kx * bs) - (bs / 2) + (bs)
y = (ky * bs) - (bs / 2) + (bs)
for i in range(0, 7) // Check thatone for usability (n=8)
nx = kx + dx[i]
ny = ky + dy[i]
// first between
if between( nx, 0, cbx ) and between( ny, 0, cby ) then
if cb[ny][nx] == 0 then
// count the available neighbors of the neighbor
ctr = 0
for j in range(0, 7) // Check for usability
ex = nx + dx[j]
ey = ny + dy[j]
// second between
if between( ex, 0, cbx ) and between( ey, 0, cby ) then
// increment the counter ctr (pq)
if cb[ey][ex] == 0 then ctr = ctr + 1
end if //between-2
end for //j
pq.push [ctr, i] // counter & loop no.
end if //cb[ny][nx] == 0
end if // between-1
end for // i
// ----------------------------------------- priority queue
// move to the neighbor with min number of avail. neighbors
if pq then
minVal = 7
minD = -1
for dd in pq.indexes
p = pq[dd][0]
m = pq[dd][1]
rdm = floor(rnd * 9) // coin toss; random
if p == minVal and rdm < 5 then // take it.. or not ;-)
minVal = p
minD = m
end if
if p < minVal then
minVal = p
minD = m
end if
end for //dd
m = minD
// new field (logics):
kx = kx + dx[m]
ky = ky + dy[m]
// new field (grafics):
nx = (kx * bs) + bs/2
ny = (ky * bs) + bs/2
if k == 1 then // mark startfield: white circle
gfx.color = color.white // startfield
gfx.fillEllipse x-5, y-5, 10, 10
gfx.color = color.blue // colorswitch: back to blue
gfx.line x, y, nx, ny // show the jump in right color
else
gfx.color = color.blue // color of normal case
gfx.fillEllipse x-5, y-5, 10, 10 // not necessary
end if
gfx.line x, y, nx, ny // show the jump in right color
x = nx // switch back
y = ny // switch back
wait(0.1) // wait a lttle moment
end if // pq
end while // end jump-loop
gfx.color = color.aqua // mark endfield
gfx.fillEllipse x-5, y-5, 10, 10 // "
end function
//prnt(cb) // show content of cb[]
// ---------------------------------------------------- end of jumps
// --------------------------------------- definition of active zone
b = new Bounds
b.x = 142
b.y = 142
b.width = 287
b.height = 287
if b.contains(mouse) then
gfx.drawPoly b.corners, color.yellow // zone active
text.row = 1 // debug
if mouse.button(0) then
key.clear
kx =floor(mouse.x/bs)
ky =floor(mouse.y/bs)
doJump(kx, ky)
end if
else
gfx.drawPoly b.corners, color.gray // zone inactive
end if
yield
end while
// ----------------------------------------------- the end of pgm