Wednesday, 23 November 2022

Wednesday, 2 March 2022

bashword : A Wordle-like game in BASH

bashword

 

#!/bin/bash
clear
shopt -s lastpipe
RED="\e[1;31m"
GREEN="\e[1;37;42m"
BROWN="\e[33m"
CYAN="\e[36m"
GRAY="\e[37m"
YELLOW="\e[1;43;30m"
COLOUR=( $GRAY $GREEN $YELLOW )
EOC="\e[0m"
z=$RANDOM
COUNT=0
guessNum=1
CONGRATS=("Extraordinary !" "Superb !" "Well Done" "Not bad" "Just OK... " "Just in time !")
 
# build a WORD array
grep "^.....$"  /usr/share/dict/words|grep -v "[é'[:upper:]]"|
while read word ;do
   WORDS[$COUNT]+=$word
   ((COUNT++))
done  

# select a random 5 letter word
word=${WORDS[$(($RANDOM % ${#WORDS[@]}))]}
echo -e $CYAN"Guess the 5 letter word"
echo -e "The letter clues are colour coded :"$EOC
echo -e "           "$GREEN     "  Correct position   "$EOC
echo -e "           "$YELLOW   "  Incorrect position "$EOC
if [ "$1" == "DEBUG" ] ; then echo "The word is:" $word ;fi

while [ $guessNum -lt  7 ]   
do
  found=0
 while [ $found -eq 0 ] ; do
  echo -e $CYAN"Enter guess no." $guessNum $EOC
  read guess
  while [ ${#guess} -lt 5 ] ;do
    echo "Enter a 5 letter word"
    read guess
  done  
  #check guess is real word
  n=${#WORDS[@]}
  ((n--))
  while [ $n -ge 0 ] ;do
    if [ ${WORDS[$n]} == $guess ] ; then
     found=1
     break
    fi
    ((n--))
  done
  if [ $found -eq 0 ];then
    echo -e $RED"Word not found in list, Try again"$EOC
  fi
 done
 
 # check for correct letter in correct space
 unset testWord
 match=(0 0 0 0 0 )
 for letter in {0..4} ;do
     testWord[$letter]+=${word:$letter:1}  
     if [ ${guess:$letter:1} == ${testWord[$letter]} ]; then
      set $((match[$letter] = 1))
      testWord[$letter]="_"
     fi
 done

# check for correct letters anywhere...  
 for gw in {0..4} ;do
    for tw in {0..4} ;do
     if [ ${testWord[$tw]} == ${guess:$gw:1} ]; then
      if [ ${match[$gw]} -eq 0 ] ;then
         set $((match[$gw] = 2))
         testWord[$tw]="_"
      fi
     fi
  done
 done  

# display matches
 echo " "
 for letter in {0..4} ;do
     echo -en ${COLOUR[${match[$letter]}]}" "${guess:$letter:1} $EOC
 done  
 echo
 echo

 if [ "$guess" ==  "$word" ] ; then
   echo ${CONGRATS[$guessNum-1]}
   echo
   break
 fi
  ((guessNum++))
done

if [ $guessNum -eq 7 ]; then
 echo -e "You ran out of guesses\nThe word was "$word
fi

Wednesday, 16 February 2022

M5StickC (RF) Remote Control decoder

When tinkering with 433 Mhz RF appliance switches it's handy to easily check what codes they respond to. This is a simple decoder using the RCswitch library

 
#include <M5StickC.h>
#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

int x=0;
int y=4; 
void setup() {
   M5.begin();
   M5.Lcd.setRotation(3);
   Serial.begin(115200); 
   mySwitch.enableReceive(0); 
    //labelled G0 on the "HAT" end of the M5StickC 
}

void loop() {
  if (mySwitch.available()) {
   
    Serial.print("Code ");
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(" / ");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println(mySwitch.getReceivedProtocol() );
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(",");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print(",");
    Serial.println( mySwitch.getReceivedProtocol() );
 
    M5.Lcd.fillScreen(TFT_BLACK);
    M5.Lcd.setTextColor(TFT_GREEN, TFT_BLACK);   
    M5.Lcd.setCursor(x, y, 2);
    M5.Lcd.setTextSize(1);
    M5.Lcd.print("Code ");
    M5.Lcd.print( String(mySwitch.getReceivedValue()) );
    M5.Lcd.print(" / ");
    M5.Lcd.print( String(mySwitch.getReceivedBitlength()) );
    M5.Lcd.println("bit ");
    M5.Lcd.print("Protocol: ");
    M5.Lcd.println( String(mySwitch.getReceivedProtocol()) );
    mySwitch.resetAvailable();
     
   } //if mySwitch
}//loop 
 

 

Tuesday, 1 February 2022

Internet Time (in C this time)

The plain C version: 

 
#include <stdio.h>
#include <time.h>
#include <math.h>

Monday, 31 January 2022

Internet Time (Processing)

In the glorious utopian (comparatively) early internet Swatch thought a universal time system would be a good idea. Wikipedia has an article on it : Swatch Internet Time .

Here is a "@beats" clock in Processing :

import java.util.*;  
int oldbeats=0;   	
Date trialTime;
TimeZone cet;
Calendar calendar ;

void setup() {
  size(120, 120);  
  cet=TimeZone.getTimeZone("Europe/Stockholm");
  calendar = new GregorianCalendar(cet);
}

void draw(){
 
  trialTime = new Date();
  calendar.setTime(trialTime);
  
  int beats =  floor( (calendar.get(Calendar.SECOND) + 
                      (calendar.get(Calendar.MINUTE) * 60) + 
                      (calendar.get(Calendar.HOUR_OF_DAY)  * 3600))
                      / 86.4005529635 );

  if (beats > oldbeats) { 
    oldbeats = beats;
    background(255);
    textSize(40);
    fill(0, 102, 153, 204);
    text("@"+beats, 3, 60); 
  }
  delay(1000); 
}

void mousePressed (){
  exit();
}