v1.10 Made many code improvements
This commit is contained in:
parent
6327c8444f
commit
32220fa7c3
293
matches.sh
293
matches.sh
|
@ -1,7 +1,7 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
## matches.sh
|
## matches.sh
|
||||||
## version 1.01
|
## version 1.10
|
||||||
## Matches is based on a two player game from the trenches of World War One.
|
## Matches is based on a two player game from the trenches of World War One.
|
||||||
|
|
||||||
## Author: Ze'ev Schurmann
|
## 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.)
|
## 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.
|
## 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
|
declare -A fgCol
|
||||||
fgCol[black]="30"
|
fgCol[black]="30"
|
||||||
fgCol[red]="31"
|
fgCol[red]="31"
|
||||||
|
@ -36,7 +36,7 @@ declare -A fgCol
|
||||||
fgCol[cyan]="36"
|
fgCol[cyan]="36"
|
||||||
fgCol[white]="37"
|
fgCol[white]="37"
|
||||||
|
|
||||||
#BACKGROUND COLOURS
|
# BACKGROUND COLOURS
|
||||||
declare -A bgCol
|
declare -A bgCol
|
||||||
bgCol[black]=";40"
|
bgCol[black]=";40"
|
||||||
bgCol[red]=";41"
|
bgCol[red]=";41"
|
||||||
|
@ -47,179 +47,176 @@ declare -A bgCol
|
||||||
bgCol[cyan]=";46"
|
bgCol[cyan]=";46"
|
||||||
bgCol[white]=";47"
|
bgCol[white]=";47"
|
||||||
|
|
||||||
#ATTRIBUTES
|
# ATTRIBUTES
|
||||||
attBright="1;"
|
declare attBright="1;"
|
||||||
attUnderline="4;"
|
declare attUnderline="4;"
|
||||||
attBlink="5;"
|
declare attBlink="5;"
|
||||||
attReverse="7;"
|
declare attReverse="7;"
|
||||||
|
|
||||||
#FLAME POSITIONS
|
# FLAME POSITIONS
|
||||||
f=(5 4 3 2)
|
declare -a f=(5 4 3 2)
|
||||||
|
|
||||||
#MATCH POSITIONS
|
# MATCH POSITIONS
|
||||||
match=(5 8 11 14 23 26 29 38 41 50)
|
declare -a match=(5 8 11 14 23 26 29 38 41 50)
|
||||||
|
|
||||||
#MATCH INDEXES
|
# MATCH INDEXES
|
||||||
setA=(3 2 1 0)
|
declare -a setA=(3 2 1 0)
|
||||||
setB=(6 5 4)
|
declare -a setB=(6 5 4)
|
||||||
setC=(8 7)
|
declare -a setC=(8 7)
|
||||||
setD=(9)
|
declare -a setD=(9)
|
||||||
|
|
||||||
|
declare player
|
||||||
|
declare sets
|
||||||
|
|
||||||
function drawBoard {
|
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
|
clear
|
||||||
echo -e "\e[0${bgCol[white]}m \e[0m"
|
for ((n=0; n<${#board[@]}; n++)); do
|
||||||
echo -e "\e[0${bgCol[white]}m \e[0m"
|
echo -e "${board[${n}]}"
|
||||||
echo -e "\e[0${bgCol[white]}m \e[0m"
|
done
|
||||||
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"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawFlame {
|
function drawFlame {
|
||||||
|
# ANIMATE FLAME
|
||||||
local m=${match[$1]}
|
local m=${match[$1]}
|
||||||
echo -n -e "\e[s\e[5;${m}H\e[${attBright}${fgCol[red]}${bgCol[white]}m▓\e[u\e[0m"
|
local n
|
||||||
sleep 0.1s
|
local -a flame=(
|
||||||
echo -n -e "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m▒\e[u\e[0m"
|
"\e[s\e[5;$((${m}-1))H\e[${attBright}${fgCol[red]}${bgCol[white]}m▒▓▒\e[u\e[0m"
|
||||||
sleep 0.1s
|
"\e[s\e[4;$((${m}-1))H\e[${fgCol[orange]}${bgCol[white]}m░▒\e[u\e[0m"
|
||||||
echo -n -e "\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"
|
||||||
sleep 0.1s
|
"\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m \e[u\e[0m"
|
||||||
echo -n -e "\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"
|
||||||
sleep 0.1s
|
"\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m▒░\e[u\e[0m"
|
||||||
echo -n -e "\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"
|
||||||
sleep 0.1s
|
"\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m \e[u\e[0m"
|
||||||
echo -n -e "\e[s\e[4;${m}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"
|
||||||
sleep 0.1s
|
"\e[s\e[4;$((${m}-1))H\e[${fgCol[orange]}${bgCol[white]}m░▒\e[u\e[0m"
|
||||||
echo -n -e "\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"
|
||||||
sleep 0.1s
|
"\e[s\e[3;${m}H\e[${attBright}${fgCol[orange]}${bgCol[white]}m \e[u\e[0m"
|
||||||
echo -n -e "\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"
|
||||||
sleep 0.1s
|
"\e[s\e[5;$((${m}-1))H\e[${fgCol[black]}${bgCol[white]}m ▓ \e[u\e[0m"
|
||||||
echo -n -e "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m \e[u\e[0m"
|
"\e[s\e[4;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m≈\e[u\e[0m"
|
||||||
sleep 0.1s
|
"\e[s\e[3;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m≈\e[u\e[0m"
|
||||||
echo -n -e "\e[s\e[4;${m}H\e[${fgCol[orange]}${bgCol[white]}m▒\e[u\e[0m"
|
"\e[s\e[2;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m≈\e[u\e[0m"
|
||||||
sleep 0.1s
|
"\e[s\e[4;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m \e[u\e[0m"
|
||||||
echo -n -e "\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[black]}${bgCol[white]}m \e[u\e[0m"
|
||||||
sleep 0.1s
|
"\e[s\e[2;${m}H\e[${attBright}${fgCol[black]}${bgCol[white]}m \e[u\e[0m"
|
||||||
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"
|
for ((n=0; n<${#flame[@]}; n++)); do
|
||||||
sleep 0.1s
|
echo -en "${flame[$n]}"
|
||||||
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
|
sleep 0.1s
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
function burnMatches {
|
function burnMatches {
|
||||||
if [[ $1 = "A" ]]; then
|
# LET'S BURN SOME MATCHES
|
||||||
local q=${#setA[@]}
|
local n
|
||||||
#local setQ=($(echo ${setA[@]}))
|
local q
|
||||||
local setQ=( "${setA[@]}" )
|
local go
|
||||||
elif [[ $1 = "B" ]]; then
|
local played
|
||||||
local q=${#setB[@]}
|
local range
|
||||||
#local setQ=($(echo ${setB[@]}))
|
local regex
|
||||||
local setQ=( "${setB[@]}" )
|
local -a setQ
|
||||||
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[@]}" )
|
|
||||||
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"
|
|
||||||
else
|
|
||||||
echo -n -e "\e[15;5HInvalid number of matches\e[0m"
|
|
||||||
sleep 3s
|
|
||||||
echo -n -e "\e[15;5H \e[0m"
|
|
||||||
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
|
go="true"
|
||||||
while [[ ${#setA[@]} -gt 0 ]] || [[ ${#setB[@]} -gt 0 ]] || [[ ${#setC[@]} -gt 0 ]] || [[ ${#setD[@]} -gt 0 ]]; do
|
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
|
||||||
|
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
|
if [[ ${player} == "PLAYER ONE" ]]; then
|
||||||
player="PLAYER TWO"
|
player="PLAYER TWO"
|
||||||
else
|
else
|
||||||
player="PLAYER ONE"
|
player="PLAYER ONE"
|
||||||
fi
|
fi
|
||||||
echo -e "\e[14;3H\e[4m${player}\e[0m"
|
if [[ ${1^^} = "A" ]]; then
|
||||||
echo -n -e "\e[15;5HChoose a set (A/B/C/D) : \e[0m"
|
setA=("${setQ[@]}")
|
||||||
read -r input
|
elif [[ ${1^^} = "B" ]]; then
|
||||||
echo -n -e "\e[15;5H \e[0m"
|
setB=("${setQ[@]}")
|
||||||
if [[ ${input^^} == "A" ]]; then
|
elif [[ ${1^^} = "C" ]]; then
|
||||||
|
setC=("${setQ[@]}")
|
||||||
|
elif [[ ${1^^} = "D" ]]; then
|
||||||
|
setD=("${setQ[@]}")
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
drawBoard
|
||||||
|
player="PLAYER ONE"
|
||||||
|
while [[ ${#setA[@]} -gt 0 ]] || [[ ${#setB[@]} -gt 0 ]] || [[ ${#setC[@]} -gt 0 ]] || [[ ${#setD[@]} -gt 0 ]]; do
|
||||||
|
sets="("
|
||||||
if [[ ${#setA[@]} -gt 0 ]]; then
|
if [[ ${#setA[@]} -gt 0 ]]; then
|
||||||
burnMatches A
|
sets="${sets}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
|
fi
|
||||||
elif [[ ${input^^} == "B" ]]; then
|
|
||||||
if [[ ${#setB[@]} -gt 0 ]]; then
|
if [[ ${#setB[@]} -gt 0 ]]; then
|
||||||
burnMatches B
|
sets="${sets}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
|
fi
|
||||||
elif [[ ${input^^} == "C" ]]; then
|
|
||||||
if [[ ${#setC[@]} -gt 0 ]]; then
|
if [[ ${#setC[@]} -gt 0 ]]; then
|
||||||
burnMatches C
|
sets="${sets}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
|
fi
|
||||||
elif [[ ${input^^} == "D" ]]; then
|
|
||||||
if [[ ${#setD[@]} -gt 0 ]]; then
|
if [[ ${#setD[@]} -gt 0 ]]; then
|
||||||
burnMatches D
|
sets="${sets}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
|
fi
|
||||||
|
sets="${sets:0:-1})"
|
||||||
|
echo -e "\e[14;3H\e[4m${player}\e[0m"
|
||||||
|
echo -en "\e[15;5HChoose a set ${sets} : \e[0m"
|
||||||
|
read -r input
|
||||||
|
echo -en "\e[15;5H \e[0m"
|
||||||
|
burnMatches ${input:0:1}
|
||||||
done
|
done
|
||||||
|
|
||||||
echo -e "\e[14;3H${player} WINS!!!\e[0m"
|
echo -e "\e[14;3H${player} WINS!!!\e[0m"
|
||||||
|
|
Loading…
Reference in New Issue