Add a move counter
This commit is contained in:
parent
ba2c7aedab
commit
25f08b612f
1 changed files with 11 additions and 7 deletions
18
bash/bucket
18
bash/bucket
|
@ -40,9 +40,9 @@ build_bucket() {
|
|||
}
|
||||
|
||||
render() {
|
||||
local bkt_idx=$3 pour_idx=$4 bkt_{one,two,three} {bkt_,}row meters
|
||||
local bkt_idx=$3 pour_idx=$4 moves=$5
|
||||
local bkt_{one,two,three} {bkt_,}row meters
|
||||
declare -n bkt_sizes=$1 bkt_amts=$2
|
||||
local bkt_{one,two,three}
|
||||
|
||||
build_bucket bkt_one "${bkt_sizes[0]}" "${bkt_amts[0]}"
|
||||
build_bucket bkt_two "${bkt_sizes[1]}" "${bkt_amts[1]}"
|
||||
|
@ -71,6 +71,8 @@ render() {
|
|||
|
||||
meters[$bkt_idx]=$(red "${meters[$bkt_idx]}")
|
||||
printf " %s %s %s\n" "${meters[0]}" "${meters[1]}" "${meters[2]}"
|
||||
printf "\n"
|
||||
printf "Move #%d\n" "$moves"
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
|
@ -80,24 +82,25 @@ cleanup() {
|
|||
trap cleanup EXIT
|
||||
tput civis
|
||||
|
||||
win=0 bkt_idx=0 pour_idx=
|
||||
sizes=(8 5 3)
|
||||
amts=(0 0 0)
|
||||
moves=0 win=0 bkt_idx=0 pour_idx=
|
||||
sizes=(8 5 3) amts=(0 0 0)
|
||||
|
||||
while (( ! win )); do
|
||||
clear
|
||||
render sizes amts "$bkt_idx" "$pour_idx"
|
||||
render sizes amts "$bkt_idx" "$pour_idx" "$moves"
|
||||
read -rsn 1 key
|
||||
|
||||
case $key in
|
||||
f)
|
||||
if [[ -z $pour_idx ]]; then
|
||||
amts[$bkt_idx]=${sizes[$bkt_idx]}
|
||||
(( moves += 1 ))
|
||||
fi
|
||||
;;
|
||||
e)
|
||||
if [[ -z $pour_idx ]]; then
|
||||
amts[$bkt_idx]=0
|
||||
(( moves += 1 ))
|
||||
fi
|
||||
;;
|
||||
p)
|
||||
|
@ -105,6 +108,7 @@ while (( ! win )); do
|
|||
pour_idx=$bkt_idx
|
||||
else
|
||||
if (( pour_idx != bkt_idx )); then
|
||||
(( moves += 1 ))
|
||||
(( space = sizes[$pour_idx] - amts[$pour_idx] ))
|
||||
|
||||
if (( amts[$bkt_idx] <= space )); then
|
||||
|
@ -147,5 +151,5 @@ while (( ! win )); do
|
|||
done
|
||||
|
||||
clear
|
||||
render sizes amts "$bkt_idx" "$pour_idx"
|
||||
render sizes amts "$bkt_idx" "$pour_idx" "$moves"
|
||||
read -rsn 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue