From d2f5bc8d0ea684603defec46690a3806ad879ed9 Mon Sep 17 00:00:00 2001 From: Ze'ev Schurmann Date: Thu, 19 Dec 2024 23:27:10 +0200 Subject: [PATCH] v1.20 Added a very basic computer logic using random selection for single player mode --- matches.sh | 217 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 147 insertions(+), 70 deletions(-) diff --git a/matches.sh b/matches.sh index 22438d7..c49a8cf 100644 --- a/matches.sh +++ b/matches.sh @@ -1,7 +1,7 @@ #!/bin/bash ## matches.sh -## version 1.10 +## version 1.20 ## Matches is based on a two player game from the trenches of World War One. ## Author: Ze'ev Schurmann @@ -15,7 +15,9 @@ ## 4. Then you input how many matches in that set you want to burn. You can burn all the matches in the set or even just 1 match. It's upto you. ## 5. Watch the matches burn and let your opponent have their turn. ## 6. The secret of the game is to make sure you get to burn the last match. He he burns the last match wins. + ## Note: In the trenches of World War One, matches were valuable, so they didn't burn the matches, they simply removed the desired of matches from the set. + ## When you develop a good strategy to the game, you will be unbeatable by novice players. ## Why did I make this game? @@ -67,6 +69,112 @@ declare -a setD=(9) declare player declare sets +declare -a names + +function burnMatches { +# 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[K\e[0m" + go="false" + fi + if [[ ${go} == "true" ]]; then + played="false" + if [[ ${q} -gt 1 ]]; then + range="1-${q}" + else + range="1" + fi + while [[ ${played} == "false" ]]; do + echo -en "\e[15;5HNumber of matches to burn (${range}): \e[0m" + if [[ ${player} == "COMPUTER" ]]; then + input=$(computerPlayer ${q}) + echo ${input} + sleep 1s + else + read -r input + fi + echo -en "\e[15;5H\e[K\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[K\e[0m" + fi + done + 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 + fi +} + +function computerPlayer { + local r + local maj + local min + local -a setarray + + maj=$((RANDOM%5)) + min=$((RANDOM%10)) + if [[ ${maj} == 0 ]] && [[ ${min} == 0 ]]; then + sleep 5.0s + else + sleep ${maj}.${min}s + fi + if [[ ${1} == "SET" ]]; then + if [[ ${#setA[@]} -gt 0 ]]; then + setarray+=("A") + fi + if [[ ${#setB[@]} -gt 0 ]]; then + setarray+=("B") + fi + if [[ ${#setC[@]} -gt 0 ]]; then + setarray+=("C") + fi + if [[ ${#setD[@]} -gt 0 ]]; then + setarray+=("D") + fi + r=$((RANDOM%${#setarray[@]})) + echo ${setarray[${r}]} + elif [[ ${1} = "1" ]] || [[ ${1} == "2" ]] || [[ ${1} == "3" ]] || [[ ${1} == "4" ]]; then + r=$(((RANDOM%${1})+1)) + echo ${r} + fi +} function drawBoard { # DRAW BOARD @@ -125,78 +233,36 @@ function drawFlame { done } -function burnMatches { -# LET'S BURN SOME MATCHES - local n - local q - local go - local played - local range - local regex - local -a setQ +function initGame { + local r + local players - 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[@]}") + cat $0 | head -n 21 | tail -n 19 | sed 's/## //g' | fold -s + echo + until [[ ${players} == "1" ]] || [[ ${players} == "2" ]]; do + echo -n "Select number of human players (1/2) : " + read -r players + if [[ ${players} != "1" ]] && [[ ${players} != "2" ]]; then + echo -e "\e[1AInvalid selection\e[K\e[0m" + sleep 3s + echo -e "\e[1A\e[K\e[0m" + echo -en "\e[1A\e[0m" + fi + done + if [[ ${players} == 1 ]]; then + names=("PLAYER" "PLAYER") + r=$((RANDOM%2)) + names[${r}]="COMPUTER" else - echo -en "\e[15;5HInvalid set selection\e[0m" - sleep 3s - echo -en "\e[15;5H \e[0m" - go="false" - fi - if [[ ${go} == "true" ]]; then - played="false" - if [[ ${q} -gt 1 ]]; then - range="1-${q}" - else - 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 + names=("PLAYER ONE" "PLAYER TWO") fi + echo "Thank you" + sleep 3s } +initGame drawBoard -player="PLAYER ONE" +player=${names[0]} while [[ ${#setA[@]} -gt 0 ]] || [[ ${#setB[@]} -gt 0 ]] || [[ ${#setC[@]} -gt 0 ]] || [[ ${#setD[@]} -gt 0 ]]; do sets="(" if [[ ${#setA[@]} -gt 0 ]]; then @@ -212,10 +278,21 @@ while [[ ${#setA[@]} -gt 0 ]] || [[ ${#setB[@]} -gt 0 ]] || [[ ${#setC[@]} -gt 0 sets="${sets}D/" fi sets="${sets:0:-1})" - echo -e "\e[14;3H\e[4m${player}\e[0m" + if [[ ${player} == ${names[0]} ]]; then + player="${names[1]}" + else + player="${names[0]}" + fi + echo -e "\e[14;3H\e[4m${player}\e[K\e[0m" echo -en "\e[15;5HChoose a set ${sets} : \e[0m" - read -r input - echo -en "\e[15;5H \e[0m" + if [[ ${player} == "COMPUTER" ]]; then + input=$(computerPlayer SET) + echo ${input} + sleep 1s + else + read -r input + fi + echo -en "\e[15;5H\e[K\e[0m" burnMatches ${input:0:1} done