NASutils Knowledge BaseUnofficialPolski

BASIC · projectSector hunt: a puzzle game in yabasic

Sector hunt is a puzzle for the SSH window. The disk has 8 tracks of 12 sectors, and 14 of them are bad. Each good sector you read shows how many bad sectors touch it, and you read every good one without touching a bad one.

Programming on the NAS › BASIC in 30 lessons

 ╔═ Sector hunt ══════════════════════════════════════╗ ╔═ Legend ════════════╗
 ║                                                    ║ ║                     ║
 ║      1   2   3   4   5   6   7   8   9  10  11  12 ║ ║ ░░░ not read        ║
 ║                                                    ║ ║  2  bad next to it  ║
 ║ T1 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║ ║  ■  marked          ║
 ║                                                    ║ ║  X  bad sector      ║
 ║ T2 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║ ╚═════════════════════╝
 ║                                                    ║
 ║ T3 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║ ╔═ Disk ══════════════╗
 ║                                                    ║ ║                     ║
 ║ T4 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║ ║ Bad      14         ║
 ║                                                    ║ ║ Marked   0          ║
 ║ T5 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║ ║ Read     0 of 82    ║
 ║                                                    ║ ╚═════════════════════╝
 ║ T6 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ║ T7 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ║ T8 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ╚════════════════════════════════════════════════════╝

  arrows move   space reads   f marks   n new disk   q quits
A new disk: 96 sectors not read, the cursor on track 1, sector 1.
 ╔═ Sector hunt ══════════════════════════════════════╗ ╔═ Legend ════════════╗
 ║                                                    ║ ║                     ║
 ║      1   2   3   4   5   6   7   8   9  10  11  12 ║ ║ ░░░ not read        ║
 ║                                                    ║ ║  2  bad next to it  ║
 ║ T1 ░░░ ░░░  1   ·   ·   ·   ·   ·   ·   ·   1  ░░░ ║ ║  ■  marked          ║
 ║                                                    ║ ║  X  bad sector      ║
 ║ T2 ░░░ ░░░  1   ·   ·   ·   ·   ·   ·   ·   1  ░░░ ║ ╚═════════════════════╝
 ║                                                    ║
 ║ T3 ░░░ ░░░  1   ·   ·   ·   1   1   1   ·   1  ░░░ ║ ╔═ Disk ══════════════╗
 ║                                                    ║ ║                     ║
 ║ T4 ░░░ ░░░  1   1   ·   ·   1  ░░░  2   1   1  ░░░ ║ ║ Bad      14         ║
 ║                                                    ║ ║ Marked   0          ║
 ║ T5 ░░░ ░░░ ░░░  1   ·   1   2  ░░░ ░░░ ░░░ ░░░ ░░░ ║ ║ Read     42 of 82   ║
 ║                                                    ║ ╚═════════════════════╝
 ║ T6 ░░░ ░░░ ░░░  1   1   2  ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ║ T7 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ║ T8 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ╚════════════════════════════════════════════════════╝

  arrows move   space reads   f marks   n new disk   q quits
After the first read at track 4, sector 6: 42 sectors open at once, with their numbers.
 ╔═ Sector hunt ══════════════════════════════════════╗ ╔═ Legend ════════════╗
 ║                                                    ║ ║                     ║
 ║      1   2   3   4   5   6   7   8   9  10  11  12 ║ ║ ░░░ not read        ║
 ║                                                    ║ ║  2  bad next to it  ║
 ║ T1 ░░░ ░░░  1   ·   ·   ·   ·   ·   ·   ·   1  ░░░ ║ ║  ■  marked          ║
 ║                                                    ║ ║  X  bad sector      ║
 ║ T2 ░░░ ░░░  1   ·   ·   ·   ·   ·   ·   ·   1  ░░░ ║ ╚═════════════════════╝
 ║                                                    ║
 ║ T3 ░░░ ░░░  1   ·   ·   ·   1   1   1   ·   1  ░░░ ║ ╔═ Disk ══════════════╗
 ║                                                    ║ ║                     ║
 ║ T4 ░░░ ░░░  1   1   ·   ·   1   ■   2   1   1  ░░░ ║ ║ Bad      14         ║
 ║                                                    ║ ║ Marked   2          ║
 ║ T5 ░░░ ░░░ ░░░  1   ·   1   2   2  ░░░ ░░░ ░░░ ░░░ ║ ║ Read     43 of 82   ║
 ║                                                    ║ ╚═════════════════════╝
 ║ T6 ░░░ ░░░ ░░░  1   1   2   ■  ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ║ T7 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ║ T8 ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ╚════════════════════════════════════════════════════╝

  arrows move   space reads   f marks   n new disk   q quits
Two sectors marked with ■ and one more read: 43 of 82.
 ╔═ Sector hunt ══════════════════════════════════════╗ ╔═ Legend ════════════╗
 ║                                                    ║ ║                     ║
 ║      1   2   3   4   5   6   7   8   9  10  11  12 ║ ║ ░░░ not read        ║
 ║                                                    ║ ║  2  bad next to it  ║
 ║ T1 ░░░ ░░░  1   ·   ·   ·   ·   ·   ·   ·   1  ░░░ ║ ║  ■  marked          ║
 ║                                                    ║ ║  X  bad sector      ║
 ║ T2  X   X   1   ·   ·   ·   ·   ·   ·   ·   1   X  ║ ╚═════════════════════╝
 ║                                                    ║
 ║ T3 ░░░ ░░░  1   ·   ·   ·   1   1   1   ·   1  ░░░ ║ ╔═ Disk ══════════════╗
 ║                                                    ║ ║                     ║
 ║ T4 ░░░ ░░░  1   1   ·   ·   1   X   2   1   1  ░░░ ║ ║ Bad      14         ║
 ║                                                    ║ ║ Marked   2          ║
 ║ T5 ░░░ ░░░  X   1   ·   1   2   2  ░░░  X  ░░░ ░░░ ║ ║ Read     43 of 82   ║
 ║                                                    ║ ╚═════════════════════╝
 ║ T6 ░░░ ░░░ ░░░  1   1   2   X  ░░░ ░░░ ░░░ ░░░ ░░░ ║
 ║                                                    ║
 ║ T7  X   X  ░░░ ░░░ ░░░  X  ░░░ ░░░ ░░░  X   X  ░░░ ║
 ║                                                    ║
 ║ T8 ░░░  X  ░░░ ░░░ ░░░ ░░░ ░░░ ░░░ ░░░  X  ░░░ ░░░ ║
 ║ Track 5, sector 3 was bad. The disk is lost.       ║
 ╚════════════════════════════════════════════════════╝

  arrows move   space reads   f marks   n new disk   q quits
A read of the bad sector at track 5, sector 3: every bad sector shows as X and the disk is lost.

Download it on the NAS

curl -fsSLO https://kb.nasutils.net/projects/basic/game.yab

game.yab Download game.yab

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

What you learn

  • dim bad(13, 9) makes a two-dimensional array; a border of empty cells round the 12 x 8 disk saves the edge checks when the program counts neighbours.
  • A stack in an array replaces recursion: reading an empty sector opens its neighbours, and theirs, until the numbers stop it.
  • rnd() with the Park and Miller formula gives the same row of numbers for the same SEED, so a test sees the same disk every time.
  • One draw() builds the whole screen as a string and sends it in one write, so a key press never shows half a picture.

How it works

Each sector has two values: bad(x, y) is 1 for a bad sector, and state(x, y) is 0 for not read, 1 for read and 2 for marked. The bad sectors are placed at the first read, never on that sector or next to it, so the first read always opens a part of the disk.

readsector puts the sector on the stack. While the stack holds something, it takes the top sector, marks it read and counts its bad neighbours with near. If the count is 0, all eight neighbours go on the stack. That is how one key opens the whole calm area at once.

draw paints the disk, the legend and the counters. cell$ decides how a sector looks: ░░░ not read, a number in green, amber or red, · for no bad neighbour, ■ for a mark. The cursor sector gets black on amber; plain$ strips the colour code of the cell first. After a bad read the game shows every bad sector as X and waits for n or q.

Notes to the lines

  • 5The size of the disk and the number of bad sectors: change them here and the whole game follows.
  • 6bad has a border of one empty cell on each side; stack has room for nine entries per sector, the most a fill can push.
  • 7–8SEED starts the random row; without it, the minute and the second of the clock do.
  • 15–18The arrows move the cursor and stop at the edge.
  • 19f turns a mark on and off: 2 - 0 is 2 and 2 - 2 is 0. A read sector cannot be marked.
  • 21The game is won when the count of read sectors equals all sectors minus the bad ones.
  • 41–44The loop draws random places until 14 different ones lie outside the 3 x 3 square of the first read.
  • 52–63The fill: one number x * 100 + y keeps both coordinates of a sector on the stack.
  • 67The sum of the eight neighbours is the number shown on the sector.
  • 79The cursor sector keeps its text and gets black on amber in place of its own colour.
  • 86–87The message of a won or a lost disk on the last line of the frame.
  • 110–113Each call multiplies the seed by 16807 and keeps the remainder of the division by 2147483647.

Run it

yabasic game.yab

The arrow keys move, space reads the sector under the cursor, f marks or unmarks it, n starts a new disk and q ends the game.

Try it yourself

Set bads = 20 on line 5 and play a harder disk. Make the numbers 3 and higher bold red: change e$("31") on line 100 to e$("1;31").

More projects in BASIC