v1.20 Added a very basic computer logic using random selection for single player mode
This commit is contained in:
parent
32220fa7c3
commit
d2f5bc8d0e
209
matches.sh
209
matches.sh
|
@ -1,7 +1,7 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
## matches.sh
|
## matches.sh
|
||||||
## version 1.10
|
## version 1.20
|
||||||
## 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
|
||||||
|
@ -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.
|
## 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.
|
## 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.
|
## 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.
|
## 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.
|
## When you develop a good strategy to the game, you will be unbeatable by novice players.
|
||||||
|
|
||||||
## Why did I make this game?
|
## Why did I make this game?
|
||||||
|
@ -67,6 +69,112 @@ declare -a setD=(9)
|
||||||
|
|
||||||
declare player
|
declare player
|
||||||
declare sets
|
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 {
|
function drawBoard {
|
||||||
# DRAW BOARD
|
# DRAW BOARD
|
||||||
|
@ -125,78 +233,36 @@ function drawFlame {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
function burnMatches {
|
function initGame {
|
||||||
# LET'S BURN SOME MATCHES
|
local r
|
||||||
local n
|
local players
|
||||||
local q
|
|
||||||
local go
|
|
||||||
local played
|
|
||||||
local range
|
|
||||||
local regex
|
|
||||||
local -a setQ
|
|
||||||
|
|
||||||
go="true"
|
cat $0 | head -n 21 | tail -n 19 | sed 's/## //g' | fold -s
|
||||||
if [[ ${1^^} = "A" ]] && [[ ${#setA[@]} -gt 0 ]]; then
|
echo
|
||||||
q=${#setA[@]}
|
until [[ ${players} == "1" ]] || [[ ${players} == "2" ]]; do
|
||||||
setQ=("${setA[@]}")
|
echo -n "Select number of human players (1/2) : "
|
||||||
elif [[ ${1^^} = "B" ]] && [[ ${#setB[@]} -gt 0 ]]; then
|
read -r players
|
||||||
q=${#setB[@]}
|
if [[ ${players} != "1" ]] && [[ ${players} != "2" ]]; then
|
||||||
setQ=("${setB[@]}")
|
echo -e "\e[1AInvalid selection\e[K\e[0m"
|
||||||
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
|
sleep 3s
|
||||||
echo -en "\e[15;5H \e[0m"
|
echo -e "\e[1A\e[K\e[0m"
|
||||||
go="false"
|
echo -en "\e[1A\e[0m"
|
||||||
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
|
fi
|
||||||
done
|
done
|
||||||
if [[ ${player} == "PLAYER ONE" ]]; then
|
if [[ ${players} == 1 ]]; then
|
||||||
player="PLAYER TWO"
|
names=("PLAYER" "PLAYER")
|
||||||
|
r=$((RANDOM%2))
|
||||||
|
names[${r}]="COMPUTER"
|
||||||
else
|
else
|
||||||
player="PLAYER ONE"
|
names=("PLAYER ONE" "PLAYER TWO")
|
||||||
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
|
|
||||||
fi
|
fi
|
||||||
|
echo "Thank you"
|
||||||
|
sleep 3s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initGame
|
||||||
drawBoard
|
drawBoard
|
||||||
player="PLAYER ONE"
|
player=${names[0]}
|
||||||
while [[ ${#setA[@]} -gt 0 ]] || [[ ${#setB[@]} -gt 0 ]] || [[ ${#setC[@]} -gt 0 ]] || [[ ${#setD[@]} -gt 0 ]]; do
|
while [[ ${#setA[@]} -gt 0 ]] || [[ ${#setB[@]} -gt 0 ]] || [[ ${#setC[@]} -gt 0 ]] || [[ ${#setD[@]} -gt 0 ]]; do
|
||||||
sets="("
|
sets="("
|
||||||
if [[ ${#setA[@]} -gt 0 ]]; then
|
if [[ ${#setA[@]} -gt 0 ]]; then
|
||||||
|
@ -212,10 +278,21 @@ while [[ ${#setA[@]} -gt 0 ]] || [[ ${#setB[@]} -gt 0 ]] || [[ ${#setC[@]} -gt 0
|
||||||
sets="${sets}D/"
|
sets="${sets}D/"
|
||||||
fi
|
fi
|
||||||
sets="${sets:0:-1})"
|
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"
|
echo -en "\e[15;5HChoose a set ${sets} : \e[0m"
|
||||||
|
if [[ ${player} == "COMPUTER" ]]; then
|
||||||
|
input=$(computerPlayer SET)
|
||||||
|
echo ${input}
|
||||||
|
sleep 1s
|
||||||
|
else
|
||||||
read -r input
|
read -r input
|
||||||
echo -en "\e[15;5H \e[0m"
|
fi
|
||||||
|
echo -en "\e[15;5H\e[K\e[0m"
|
||||||
burnMatches ${input:0:1}
|
burnMatches ${input:0:1}
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue