rem Sector hunt: find the bad sectors of a disk without reading one. A number tells how many bad sectors touch it.
rem Run: yabasic game.yab   Arrows move, space reads a sector, f marks it, n starts a new disk, q quits.
rem The same SEED gives the same disk. Keys come from inkey$, the screen goes to /dev/tty as escape codes.

cols = 12 : rows = 8 : bads = 14
dim bad(cols + 1, rows + 1), state(cols, rows), stack(cols * rows * 9)
seed = val(peek$("env", "SEED"))
if seed = 0 seed = val(mid$(time$, 4, 2)) * 60 + val(mid$(time$, 7, 2)) + 1
clear screen
repeat
  newdisk()
  repeat
    draw()
    k$ = inkey$
    if k$ = "left" and cx > 1 cx = cx - 1
    if k$ = "right" and cx < cols cx = cx + 1
    if k$ = "up" and cy > 1 cy = cy - 1
    if k$ = "down" and cy < rows cy = cy + 1
    if k$ = "f" and state(cx, cy) <> 1 state(cx, cy) = 2 - state(cx, cy)
    if k$ = " " and state(cx, cy) = 0 readsector(cx, cy)
    if goodread = cols * rows - bads over = 1
  until over or k$ = "q" or k$ = "n"
  if over then
    draw()
    repeat : k$ = inkey$ : until k$ = "q" or k$ = "n"
  endif
until k$ = "q"
send(e$("0") + chr$(27) + "[2J" + at$(1, 1))
exit

sub newdisk()
  local x, y
  for x = 0 to cols + 1 : for y = 0 to rows + 1 : bad(x, y) = 0 : next y : next x
  for x = 1 to cols : for y = 1 to rows : state(x, y) = 0 : next y : next x
  cx = 1 : cy = 1 : goodread = 0 : over = 0 : placed = 0
end sub

rem Bad sectors go down at the first read, never on that sector or next to it
sub place(sx, sy)
  local n, x, y
  while n < bads
    x = rnd(cols) + 1 : y = rnd(rows) + 1
    if not bad(x, y) and (abs(x - sx) > 1 or abs(y - sy) > 1) then bad(x, y) = 1 : n = n + 1 : endif
  wend
  placed = 1
end sub

sub readsector(x, y)
  local top, dx, dy
  if not placed place(x, y)
  if bad(x, y) then over = 2 : return : endif
  top = 1 : stack(1) = x * 100 + y
  while top > 0
    x = int(stack(top) / 100) : y = mod(stack(top), 100) : top = top - 1
    if state(x, y) = 0 then
      state(x, y) = 1 : goodread = goodread + 1
      if near(x, y) = 0 then
        for dx = -1 to 1 : for dy = -1 to 1
          if x + dx >= 1 and x + dx <= cols and y + dy >= 1 and y + dy <= rows then top = top + 1 : stack(top) = (x + dx) * 100 + y + dy : endif
        next dy : next dx
      endif
    endif
  wend
end sub

sub near(x, y)
  return bad(x-1, y-1) + bad(x, y-1) + bad(x+1, y-1) + bad(x-1, y) + bad(x+1, y) + bad(x-1, y+1) + bad(x, y+1) + bad(x+1, y+1)
end sub

sub draw()
  local s$, x, y, c$, marks
  s$ = e$("0") + chr$(27) + "[2J" + box$(2, 2, 54, 21, "Sector hunt") + box$(57, 2, 23, 7, "Legend") + box$(57, 10, 23, 6, "Disk")
  for x = 1 to cols : s$ = s$ + at$(4 + x * 4, 4) + e$("33") + str$(x, "%2.0f") : next x
  for y = 1 to rows
    s$ = s$ + at$(4, 4 + y * 2) + e$("33") + "T" + str$(y)
    for x = 1 to cols
      c$ = cell$(x, y)
      if state(x, y) = 2 marks = marks + 1
      if x = cx and y = cy and not over c$ = e$("30;43") + plain$(c$)
      s$ = s$ + at$(3 + x * 4, 4 + y * 2) + e$("0") + c$ + e$("0")
    next x
  next y
  s$ = s$ + at$(59, 4) + "░░░ not read" + at$(59, 5) + e$("32") + " 2 " + e$("0") + " bad next to it" + at$(59, 6) + e$("1;31") + " ■ " + e$("0") + " marked"
  s$ = s$ + at$(59, 7) + e$("1;37;41") + " X " + e$("0") + " bad sector"
  s$ = s$ + at$(59, 12) + "Bad      " + str$(bads) + at$(59, 13) + "Marked   " + str$(marks) + at$(59, 14) + "Read     " + str$(goodread) + " of " + str$(cols * rows - bads)
  if over = 1 s$ = s$ + at$(4, 21) + e$("1;32") + "Every good sector is read. The disk is safe."
  if over = 2 s$ = s$ + at$(4, 21) + e$("1;31") + "Track " + str$(cy) + ", sector " + str$(cx) + " was bad. The disk is lost."
  send(s$ + at$(3, 24) + e$("0;33") + "arrows move   space reads   f marks   n new disk   q quits" + e$("0"))
end sub

sub cell$(x, y)
  local n
  if over = 2 and bad(x, y) return e$("1;37;41") + " X "
  if state(x, y) = 0 return "░░░"
  if state(x, y) = 2 return e$("1;31") + " ■ "
  n = near(x, y)
  if n = 0 return " · "
  if n = 1 return e$("32") + " 1 "
  if n = 2 return e$("33") + " 2 "
  return e$("31") + " " + str$(n) + " "
end sub

rem The text of a cell without its colour code, for the cursor's own colours
sub plain$(c$)
  if left$(c$, 1) = chr$(27) return mid$(c$, instr(c$, "m") + 1)
  return c$
end sub

rem Random whole number from 0 to n - 1 (Park and Miller), the same row of numbers for the same seed
sub rnd(n)
  seed = mod(seed * 16807, 2147483647)
  return mod(seed, n)
end sub

sub box$(x, y, w, h, title$)
  local s$, i
  s$ = e$("33") + at$(x, y) + "╔" + rep$("═", w - 2) + "╗"
  for i = 1 to h - 2 : s$ = s$ + at$(x, y + i) + "║" + rep$(" ", w - 2) + "║" : next i
  s$ = s$ + at$(x, y + h - 1) + "╚" + rep$("═", w - 2) + "╝"
  return s$ + at$(x + 2, y) + e$("1;33") + " " + title$ + " " + e$("0")
end sub

sub rep$(s$, n) : local r$, i : for i = 1 to n : r$ = r$ + s$ : next i : return r$ : end sub
sub e$(code$) : return chr$(27) + "[" + code$ + "m" : end sub
sub at$(x, y) : return chr$(27) + "[" + str$(y) + ";" + str$(x) + "H" : end sub
sub send(s$) : open #1, "/dev/tty", "w" : print #1 s$; : close #1 : end sub
