Steeph's Web Site

Go To Navigation
Show/Hide Navigation
Backmatch - A Simple Dual-N-Back-Inspired Performance Task Trainer For Bash

Here is a script that I wrote as a short side project because I wanted my own n-back trainer. I'm aware that the practical memory improments of n-back training, even when using a proper audiovisual dual n-back trainer, is not as great as it's often said to be. I just wanted to try it and see whether I like the training.

Usage

Execute the script and pass it a number that resembles the difficulty level. For example bash backmatch 3 starts the script with a 3-back task. On your keyboard press the key of the letter that was displayed n letters ago (in this example 3 letters ago). When you press a key the next letter is presented immedietly. If you don't press a key for 3 seconds (the time value can be changed by changing the variable sec) the next letter is presented and you don't get a point for this letter. When you exit the script by pressing ctrl+c your keypresses get compared to what was presented and you get your score.


#!/bin/bash

if [[ ${#} -ge 1 ]] && [[ ${1} =~ ^[0-9]*$ ]]
then
  n=${1}
else
  echo "'${@}' is not a number, is it?"
  exit 1
fi

echo "Ctrl+C pressing is for quitters."
npo=$((n+1))
sec=3

end() {
  echo -en "           \n"
  if [[ ${#str} -ge ${npo} ]]							# If enough characters had been generated
  then
    for i in $(seq $n); do echo -n "-"; done					# offset by $n dashes
    echo ${str}
    echo ${you}
    got=0
    for i in $(seq ${#str})
    do
      [[ ${str:$i:1} == ${you:$((i+n)):1} ]] && got=$((got+1))
    done
    echo -e "\n${got} out of $((${#str}-npo)) correct"
  else
    echo "Not enough data to judge you."
  fi
}

trap end EXIT

while true
do
  str+=$(cat /dev/urandom | tr -dc 'A-Z' | head -c 1)				# Get a random new letter.
  echo -en "\r       \r${str: -1}"						# Print the last character in the string (the new letter).
    read -n 1 -t ${sec} key							# Get a single character input, timeout $sec seconds.
    [[ -n ${key} ]] && you+=${key^^} || you+="-"
    [[ ${key^^} == ${str: -$npo:1} ]] \
      && echo -en "\r       good" \
      || echo -en "\r        bad"						# Check if uppercase input char is the same as the nth char from the back.
done

end

exit 1