From 32220fa7c34e312d36d17127bee62a381f9aacf9 Mon Sep 17 00:00:00 2001 From: Ze'ev Schurmann Date: Thu, 19 Dec 2024 22:02:54 +0200 Subject: [PATCH] v1.10 Made many code improvements --- matches.sh | 301 ++++++++++++++++++++++++++--------------------------- 1 file changed, 149 insertions(+), 152 deletions(-) diff --git a/matches.sh b/matches.sh index 178b95e..22438d7 100644 --- a/matches.sh +++ b/matches.sh @@ -1,7 +1,7 @@ #!/bin/bash ## matches.sh -## version 1.01 +## version 1.10 ## Matches is based on a two player game from the trenches of World War One. ## Author: Ze'ev Schurmann @@ -25,7 +25,7 @@ ## I will like to develop the logic so you can play against the computer. I will make several levels of difficulty from Easy to Super Hard (using all my own strategies - I am virtually unbeaten.) ## My grandfather taught me this game when I was about 5 or 6. I am now 46 and I have been teaching friends and partners my entire life. I have developed some strong strategies, and you may not think it, but the game can be come mentally challenging when you play a good opponent. -#FOREGROUND COLOURS +# FOREGROUND COLOURS declare -A fgCol fgCol[black]="30" fgCol[red]="31" @@ -36,7 +36,7 @@ declare -A fgCol fgCol[cyan]="36" fgCol[white]="37" -#BACKGROUND COLOURS +# BACKGROUND COLOURS declare -A bgCol bgCol[black]=";40" bgCol[red]=";41" @@ -47,179 +47,176 @@ declare -A bgCol bgCol[cyan]=";46" bgCol[white]=";47" -#ATTRIBUTES -attBright="1;" -attUnderline="4;" -attBlink="5;" -attReverse="7;" +# ATTRIBUTES +declare attBright="1;" +declare attUnderline="4;" +declare attBlink="5;" +declare attReverse="7;" -#FLAME POSITIONS -f=(5 4 3 2) +# FLAME POSITIONS +declare -a f=(5 4 3 2) -#MATCH POSITIONS -match=(5 8 11 14 23 26 29 38 41 50) +# MATCH POSITIONS +declare -a match=(5 8 11 14 23 26 29 38 41 50) -#MATCH INDEXES -setA=(3 2 1 0) -setB=(6 5 4) -setC=(8 7) -setD=(9) +# MATCH INDEXES +declare -a setA=(3 2 1 0) +declare -a setB=(6 5 4) +declare -a setC=(8 7) +declare -a setD=(9) + +declare player +declare sets function drawBoard { -#DRAW BOARD +# DRAW BOARD + local n + local -a board=( + "\e[0${bgCol[white]}m \e[0m" + "\e[0${bgCol[white]}m \e[0m" + "\e[0${bgCol[white]}m \e[0m" + "\e[0${bgCol[white]}m \e[0m" + "\e[${fgCol[green]}${bgCol[white]}m ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▓ \e[0m" + "\e[${attBright}${fgCol[orange]}${bgCol[white]}m █ █ █ █ █ █ █ █ █ █ \e[0m" + "\e[${attBright}${fgCol[orange]}${bgCol[white]}m █ █ █ █ █ █ █ █ █ █ \e[0m" + "\e[${attBright}${fgCol[orange]}${bgCol[white]}m █ █ █ █ █ █ █ █ █ █ \e[0m" + "\e[${attBright}${fgCol[orange]}${bgCol[white]}m █ █ █ █ █ █ █ █ █ █ \e[0m" + "\e[0${bgCol[white]}m \e[0m" + "\e[${attBright}${fgCol[blue]}${bgCol[white]}m [A] [B] [C] [D] \e[0m" + "\e[0${bgCol[white]}m \e[0m" + ) + clear - echo -e "\e[0${bgCol[white]}m \e[0m" - echo -e "\e[0${bgCol[white]}m \e[0m" - echo -e "\e[0${bgCol[white]}m \e[0m" - echo -e "\e[0${bgCol[white]}m \e[0m" - echo -e "\e[${fgCol[green]}${bgCol[white]}m ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▓ \e[0m" - echo -e "\e[${attBright}${fgCol[orange]}${bgCol[white]}m █ █ █ █ █ █ █ █ █ █ \e[0m" - echo -e "\e[${attBright}${fgCol[orange]}${bgCol[white]}m █ █ █ █ █ █ █ █ █ █ \e[0m" - echo -e "\e[${attBright}${fgCol[orange]}${bgCol[white]}m █ █ █ █ █ █ █ █ █ █ \e[0m" - echo -e "\e[${attBright}${fgCol[orange]}${bgCol[white]}m █ █ █ █ █ █ █ █ █ █ \e[0m" - echo -e "\e[0${bgCol[white]}m \e[0m" - echo -e "\e[${attBright}${fgCol[blue]}${bgCol[white]}m [A] [B] [C] [D] \e[0m" - echo -e "\e[0${bgCol[white]}m \e[0m" + for ((n=0; n<${#board[@]}; n++)); do + echo -e "${board[${n}]}" + done } function drawFlame { +# ANIMATE FLAME local m=${match[$1]} - echo -n -e "\e[s\e[5;${m}H\e[${attBright}${fgCol[red]}${bgCol[white]}m▓\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m▒\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m░\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m▒\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m░\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m▒\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m░\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[5;${m}H\e[${fgCol[black]}${bgCol[white]}m▓\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[4;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m≈\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[3;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m≈\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[2;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m≈\e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[4;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m \e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[3;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m \e[u\e[0m" - sleep 0.1s - echo -n -e "\e[s\e[2;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m \e[u\e[0m" - sleep 0.1s + local n + local -a flame=( + "\e[s\e[5;$((${m}-1))H\e[${attBright}${fgCol[red]}${bgCol[white]}m▒▓▒\e[u\e[0m" + "\e[s\e[4;$((${m}-1))H\e[${fgCol[orange]}${bgCol[white]}m░▒\e[u\e[0m" + "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m░\e[u\e[0m" + "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" + "\e[s\e[4;$((${m}-1))H\e[${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" + "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m▒░\e[u\e[0m" + "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m░\e[u\e[0m" + "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" + "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" + "\e[s\e[4;$((${m}-1))H\e[${fgCol[orange]}${bgCol[white]}m░▒\e[u\e[0m" + "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m░\e[u\e[0m" + "\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" + "\e[s\e[4;$((${m}-1))H\e[${fgCol[orange]}${bgCol[white]}m \e[u\e[0m" + "\e[s\e[5;$((${m}-1))H\e[${fgCol[black]}${bgCol[white]}m ▓ \e[u\e[0m" + "\e[s\e[4;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m≈\e[u\e[0m" + "\e[s\e[3;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m≈\e[u\e[0m" + "\e[s\e[2;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m≈\e[u\e[0m" + "\e[s\e[4;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m \e[u\e[0m" + "\e[s\e[3;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m \e[u\e[0m" + "\e[s\e[2;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m \e[u\e[0m" + ) + + for ((n=0; n<${#flame[@]}; n++)); do + echo -en "${flame[$n]}" + sleep 0.1s + done } function burnMatches { - if [[ $1 = "A" ]]; then - local q=${#setA[@]} - #local setQ=($(echo ${setA[@]})) - local setQ=( "${setA[@]}" ) - elif [[ $1 = "B" ]]; then - local q=${#setB[@]} - #local setQ=($(echo ${setB[@]})) - local setQ=( "${setB[@]}" ) - elif [[ $1 = "C" ]]; then - local q=${#setC[@]} - #local setQ=($(echo ${setC[@]})) - local setQ=( "${setC[@]}" ) - elif [[ $1 = "D" ]]; then - local q=${#setD[@]} - #local setQ=($(echo ${setD[@]})) - local setQ=( "${setD[@]}" ) +# LET'S BURN SOME MATCHES + local n + local q + local go + local played + local range + local regex + local -a setQ + + go="true" + if [[ ${1^^} = "A" ]] && [[ ${#setA[@]} -gt 0 ]]; then + q=${#setA[@]} + setQ=("${setA[@]}") + elif [[ ${1^^} = "B" ]] && [[ ${#setB[@]} -gt 0 ]]; then + q=${#setB[@]} + setQ=("${setB[@]}") + elif [[ ${1^^} = "C" ]] && [[ ${#setC[@]} -gt 0 ]]; then + q=${#setC[@]} + setQ=("${setC[@]}") + elif [[ ${1^^} = "D" ]] && [[ ${#setD[@]} -gt 0 ]]; then + q=${#setD[@]} + setQ=("${setD[@]}") + else + echo -en "\e[15;5HInvalid set selection\e[0m" + sleep 3s + echo -en "\e[15;5H \e[0m" + go="false" fi - local played="false" - while [[ ${played} == "false" ]]; do - echo -n -e "\e[15;5HNumber of matches to burn (1-${q}): \e[0m" - read -r input - echo -n -e "\e[15;5H \e[0m" - if [[ ${input} -gt 0 ]] && [[ ${input} -le ${q} ]]; then - for ((n=0; n<${input}; n++)); do - drawFlame ${setQ[${n}]} - setQ[${n}]="" - done - local played="true" + if [[ ${go} == "true" ]]; then + played="false" + if [[ ${q} -gt 1 ]]; then + range="1-${q}" else - echo -n -e "\e[15;5HInvalid number of matches\e[0m" - sleep 3s - echo -n -e "\e[15;5H \e[0m" + range="1" + fi + while [[ ${played} == "false" ]]; do + echo -en "\e[15;5HNumber of matches to burn (${range}): \e[0m" + read -r input + echo -en "\e[15;5H \e[0m" + regex="^[${range}]$" + if [[ ${input} =~ ${regex} ]]; then + for ((n=0; n<${input}; n++)); do + drawFlame ${setQ[0]} + setQ=("${setQ[@]:1}") + done + played="true" + else + echo -en "\e[15;5HInvalid number of matches\e[0m" + sleep 3s + echo -en "\e[15;5H \e[0m" + fi + done + if [[ ${player} == "PLAYER ONE" ]]; then + player="PLAYER TWO" + else + player="PLAYER ONE" + fi + if [[ ${1^^} = "A" ]]; then + setA=("${setQ[@]}") + elif [[ ${1^^} = "B" ]]; then + setB=("${setQ[@]}") + elif [[ ${1^^} = "C" ]]; then + setC=("${setQ[@]}") + elif [[ ${1^^} = "D" ]]; then + setD=("${setQ[@]}") fi - done - if [[ $1 = "A" ]]; then - setA=($(echo ${setQ[@]})) - elif [[ $1 = "B" ]]; then - setB=($(echo ${setQ[@]})) - elif [[ $1 = "C" ]]; then - setC=($(echo ${setQ[@]})) - elif [[ $1 = "D" ]]; then - setD=($(echo ${setQ[@]})) fi } drawBoard +player="PLAYER ONE" while [[ ${#setA[@]} -gt 0 ]] || [[ ${#setB[@]} -gt 0 ]] || [[ ${#setC[@]} -gt 0 ]] || [[ ${#setD[@]} -gt 0 ]]; do - if [[ ${player} == "PLAYER ONE" ]]; then - player="PLAYER TWO" - else - player="PLAYER ONE" + sets="(" + if [[ ${#setA[@]} -gt 0 ]]; then + sets="${sets}A/" fi + if [[ ${#setB[@]} -gt 0 ]]; then + sets="${sets}B/" + fi + if [[ ${#setC[@]} -gt 0 ]]; then + sets="${sets}C/" + fi + if [[ ${#setD[@]} -gt 0 ]]; then + sets="${sets}D/" + fi + sets="${sets:0:-1})" echo -e "\e[14;3H\e[4m${player}\e[0m" - echo -n -e "\e[15;5HChoose a set (A/B/C/D) : \e[0m" + echo -en "\e[15;5HChoose a set ${sets} : \e[0m" read -r input - echo -n -e "\e[15;5H \e[0m" - if [[ ${input^^} == "A" ]]; then - if [[ ${#setA[@]} -gt 0 ]]; then - burnMatches A - else - echo -n -e "\e[15;5HNo unlit matches in Set A\e[0m" - sleep 3s - echo -n -e "\e[15;5H \e[0m" - fi - elif [[ ${input^^} == "B" ]]; then - if [[ ${#setB[@]} -gt 0 ]]; then - burnMatches B - else - echo -n -e "\e[15;5HNo unlit matches in Set B\e[0m" - sleep 3s - echo -n -e "\e[15;5H \e[0m" - fi - elif [[ ${input^^} == "C" ]]; then - if [[ ${#setC[@]} -gt 0 ]]; then - burnMatches C - else - echo -n -e "\e[15;5HNo unlit matches in Set C\e[0m" - sleep 3s - echo -n -e "\e[15;5H \e[0m" - fi - elif [[ ${input^^} == "D" ]]; then - if [[ ${#setD[@]} -gt 0 ]]; then - burnMatches D - else - echo -n -e "\e[15;5HNo unlit matches in Set D\e[0m" - sleep 3s - echo -n -e "\e[15;5H \e[0m" - fi - else - echo -n -e "\e[15;5HInvalid selection\e[0m" - sleep 3s - echo -n -e "\e[15;5H \e[0m" - fi + echo -en "\e[15;5H \e[0m" + burnMatches ${input:0:1} done echo -e "\e[14;3H${player} WINS!!!\e[0m"