NASutils Knowledge BaseUnofficialPolski

Bash · projectBlink: a memory game with drive-bay LEDs

Blink is a small game for the SSH window. The four drive-bay LEDs of a drawn NAS light up one after another, and you press their numbers in the same order. Each round adds one LED to the sequence, and the game keeps your best round.

Programming on the NAS › Bash in 30 lessons

╔══════════════════════════════════════════════════════════════════════════════╗
║ BLINK   repeat the LEDs with the keys 1 to 4         round  0   best  0      ║
╚══════════════════════════════════════════════════════════════════════════════╝
   ┌───────────────────────────────────────────────────────────────────────┐
   │   ┌────────────┐   ┌────────────┐   ┌────────────┐   ┌────────────┐   │
   │   │   ██████   │   │   ██████   │   │   ██████   │   │   ██████   │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │     1      │   │     2      │   │     3      │   │     4      │   │
   │   └────────────┘   └────────────┘   └────────────┘   └────────────┘   │
   └───────────────────────────────────────────────────────────────────────┘

   Watch the LEDs, then press their numbers in the same order.
   Enter starts, q quits.
The start: four grey LEDs in the case of the NAS and the rules under them.
╔══════════════════════════════════════════════════════════════════════════════╗
║ BLINK   repeat the LEDs with the keys 1 to 4         round  2   best  2      ║
╚══════════════════════════════════════════════════════════════════════════════╝
   ┌───────────────────────────────────────────────────────────────────────┐
   │   ┌────────────┐   ┌────────────┐   ┌────────────┐   ┌────────────┐   │
   │   │   ██████   │   │   ██████   │   │   ██████   │   │   ██████   │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │     1      │   │     2      │   │     3      │   │     4      │   │
   │   └────────────┘   └────────────┘   └────────────┘   └────────────┘   │
   └───────────────────────────────────────────────────────────────────────┘

   Round 2 is right.
   Enter plays round 3, q quits.
After round 2: the sequence was 1, 1, and the best round is 2.
╔══════════════════════════════════════════════════════════════════════════════╗
║ BLINK   repeat the LEDs with the keys 1 to 4         round  3   best  2      ║
╚══════════════════════════════════════════════════════════════════════════════╝
   ┌───────────────────────────────────────────────────────────────────────┐
   │   ┌────────────┐   ┌────────────┐   ┌────────────┐   ┌────────────┐   │
   │   │   ██████   │   │   ██████   │   │   ██████   │   │   ██████   │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │ ▒▒▒▒▒▒▒▒▒▒ │   │
   │   │     1      │   │     2      │   │     3      │   │     4      │   │
   │   └────────────┘   └────────────┘   └────────────┘   └────────────┘   │
   └───────────────────────────────────────────────────────────────────────┘

   Wrong LED. The green one was next. Score: 2.
   Enter starts a new game, q quits.
Round 3 with a wrong key: bay 4 is red, bay 1 is green because the sequence starts with 1, and the score is 2.

Download it on the NAS

curl -fsSLO https://kb.nasutils.net/projects/bash/game.sh

game.sh Download game.sh

#!/usr/bin/env bash
# game.sh: Blink. The four drive-bay LEDs of a NAS light up in a sequence that grows by one each round; repeat it
# with the keys 1 to 4. Enter starts a round, q quits. The same SEED gives the same sequence.
# Run it: bash game.sh   (it needs Bash 4 or newer, such as TurboShell's bash)
set -uo pipefail

A=$'\e[38;5;214m' G=$'\e[1;38;5;46m' R=$'\e[1;38;5;196m' D=$'\e[38;5;240m' B=$'\e[1m' N=$'\e[0m'
RANDOM=${SEED:-$$} # the seed fixes every later $RANDOM
seq=() round=0 best=0

at() { printf '\e[%d;%dH' "$1" "$2"; }

bay() { # NUMBER COLOUR: one drive bay with its LED in the colour
  local c=$((8 + ($1 - 1) * 17)) i
  at 5 "$c" && printf '%s┌────────────┐%s' "$A" "$N"
  at 6 "$c" && printf '%s│%s   %s██████%s   %s│%s' "$A" "$N" "$2" "$N" "$A" "$N"
  for i in 7 8 9 10; do at "$i" "$c" && printf '%s│%s %s▒▒▒▒▒▒▒▒▒▒%s %s│%s' "$A" "$N" "$D" "$N" "$A" "$N"; done
  at 11 "$c" && printf '%s│%s     %s%d%s      %s│%s' "$A" "$N" "$B" "$1" "$N" "$A" "$N"
  at 12 "$c" && printf '%s└────────────┘%s' "$A" "$N"
}

bays() {
  local i
  for i in 1 2 3 4; do bay "$i" "$D"; done
}

say() { # LINE [LINE]: two lines of text under the bays
  at 15 1 && printf '\e[2K   %s' "$1"
  at 16 1 && printf '\e[2K   %s' "${2:-}"
}

score() {
  at 2 3 && printf '%sBLINK%s   repeat the LEDs with the keys 1 to 4' "$B$A" "$N"
  at 2 56 && printf 'round %2d   best %2d' "$round" "$best"
}

flash() { # NUMBER COLOUR SECONDS: light one LED, then dim it again
  bay "$1" "$2"
  sleep "$3"
  bay "$1" "$D"
}

show() { # the sequence of this round, one LED after another
  local k
  say "Watch the LEDs."
  sleep 0.6
  for k in "${seq[@]:0:round}"; do
    flash "$k" "$G" 0.5
    sleep 0.2
  done
  while IFS= read -rsn1 -t 0.01 _; do :; done # keys typed during the show do not count
}

turn() { # the player repeats the sequence; ends with status 1 at the first wrong key
  local i=0 key
  while ((i < round)); do
    say "Your turn: $i of $round."
    IFS= read -rsn1 key
    [[ $key == q ]] && exit 0
    [[ $key == [1-4] ]] || continue
    if ((key != seq[i])); then
      bay "$key" "$R"
      bay "${seq[i]}" "$G"
      return 1
    fi
    flash "$key" "$A" 0.15
    i=$((i + 1))
  done
}

restore() { printf '\e[0m\e[?25h\e[?1049l'; }
trap restore EXIT
trap 'exit 0' INT TERM
printf '\e[?1049h\e[?25l\e[2J'
at 1 1 && printf '%s╔%s╗%s' "$A" "$(printf '═%.0s' {1..78})" "$N"
at 3 1 && printf '%s╚%s╝%s' "$A" "$(printf '═%.0s' {1..78})" "$N"
at 2 1 && printf '%s║%s' "$A" "$N" && at 2 80 && printf '%s║%s' "$A" "$N"
at 4 4 && printf '%s┌%s┐%s' "$D" "$(printf '─%.0s' {1..71})" "$N" # the case of the NAS around the bays
for i in {5..12}; do at "$i" 4 && printf '%s│%s' "$D" "$N" && at "$i" 76 && printf '%s│%s' "$D" "$N"; done
at 13 4 && printf '%s└%s┘%s' "$D" "$(printf '─%.0s' {1..71})" "$N"
bays
score
say "Watch the LEDs, then press their numbers in the same order." "Enter starts, q quits."
while true; do
  IFS= read -rsn1 key
  [[ $key == q ]] && exit 0
  [[ -z $key ]] || continue
  if ((round == 0)); then # a new game: a new sequence of 99 LEDs
    seq=()
    for _ in {1..99}; do seq+=($((RANDOM % 4 + 1))); done
  fi
  round=$((round + 1))
  score
  show
  if turn; then
    ((round > best)) && best=$round
    score
    say "${G}Round $round is right.$N" "Enter plays round $((round + 1)), q quits."
  else
    say "${R}Wrong LED.$N The green one was next. Score: $((round - 1))." "Enter starts a new game, q quits."
    round=0
  fi
done

What you learn

  • RANDOM=7 seeds Bash's random numbers, so the same seed gives the same sequence every time.
  • "${seq[@]:0:round}" takes the first round entries of an array, the part of the sequence this round plays.
  • sleep 0.5 between two drawings of the same bay makes an LED blink.
  • while read -rsn1 -t 0.01 _; do :; done empties the keys typed while the LEDs were blinking.
  • [[ $key == [1-4] ]] accepts only the four digits and ignores every other key.

How it works

The function bay draws one drive bay at its column: a frame, the LED in the colour it gets, four rows of the drive tray and the number. The LED is grey when it is off, green when the game shows it, amber when you press its key and red when you pressed the wrong one.

The game makes a sequence of 99 random LEDs when a game starts. Round 1 shows the first LED, round 2 the first two, and so on. show lights each of them for half a second with a short pause between them. Then it reads away every key that arrived in the meantime, so a key pressed too early does not count as an answer.

turn reads one key per LED and compares it with the sequence. A right key blinks amber and moves on. A wrong key turns its LED red and the expected LED green, and turn ends with status 1. The main loop then shows the score and waits for Enter to start a new game, or q.

Notes to the lines

  • 8Assigning a number to RANDOM seeds the generator. Without SEED the process number $$ seeds it, so each game differs.
  • 13–20bay computes the column of bay 1 to 4 (8, 25, 42, 59) and draws it line by line.
  • 37–41flash draws a bay in a colour, waits, and draws it grey again.
  • 47–50The LEDs of this round, half a second each.
  • 51read -t 0.01 returns at once when no key waits, which ends the loop.
  • 58–60One key; q quits at any time; anything except 1 to 4 is skipped.
  • 61–65The wrong key shows red, the right LED green, and the round ends.
  • 75–80The title frame and the case of the NAS, drawn once at the start.
  • 88–91A new game draws 99 numbers from 1 to 4 with RANDOM % 4 + 1.

Run it

bash game.sh

Enter starts a round. Watch the LEDs, then press 1, 2, 3 or 4 in the same order. q quits. The script needs Bash 4 or newer; in TurboShell, bash in an SSH window is Bash 5.3.

Try it yourself

Change flash "$k" "$G" 0.5 to 0.3 to make the game faster. Keep the best round in a file such as ~/.blink-best: read it into best at the start and write it each time best grows.

More projects in Bash