#!/bin/bash

## matches.sh
## version 1.0
## Matches is based on a two player game from the trenches of World War One.

## Author: Ze'ev Schurmann
## License: GPL3.0 or later
## Git Repo: https://git.zaks.web.za/thisiszeev/matches

## How to play
## 1. The game is two player.
## 2. You have four sets of matches. Set A has 4 matches, Set B has 3 matches, Set C has 2 matches and Set D hs 1 match.
## 3. During your turn, you first select the Set you want to burn matches from by inputing A, B, C or D.
## 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?
## Proof of concept. I wanted to learn how to work with manipulating cursor positions, how to work with colours and also how to do simple text animations in Bash. So this game seemed like the challenge to help me make that happen. It took me less than two hours from start to working version 1.0

## What next?
## 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
declare -A fgCol
  fgCol[black]="30"
  fgCol[red]="31"
  fgCol[green]="32"
  fgCol[orange]="33"
  fgCol[blue]="34"
  fgCol[magenta]="35"
  fgCol[cyan]="36"
  fgCol[white]="37"

#BACKGROUND COLOURS
declare -A bgCol
  bgCol[black]=";40"
  bgCol[red]=";41"
  bgCol[green]=";42"
  bgCol[orange]=";43"
  bgCol[blue]=";44"
  bgCol[magenta]=";45"
  bgCol[cyan]=";46"
  bgCol[white]=";47"

#ATTRIBUTES
attBright="1;"
attUnderline="4;"
attBlink="5;"
attReverse="7;"

#FLAME POSITIONS
f=(5 4 3 2)

#MATCH POSITIONS
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)

function drawBoard {
#DRAW BOARD
  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"
}

function drawFlame {
  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
}

function burnMatches {
  if [[ $1 = "A" ]]; then
    local q=${#setA[@]}
    local setQ=($(echo ${setA[@]}))
  elif [[ $1 = "B" ]]; then
    local q=${#setB[@]}
    local setQ=($(echo ${setB[@]}))
  elif [[ $1 = "C" ]]; then
    local q=${#setC[@]}
    local setQ=($(echo ${setC[@]}))
  elif [[ $1 = "D" ]]; then
    local q=${#setD[@]}
    local setQ=($(echo ${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
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"
  fi
  echo -e "\e[14;3H\e[4m${player}\e[0m"
  echo -n -e "\e[15;5HChoose a set (A/B/C/D) : \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
done

echo -e "\e[14;3H${player} WINS!!!\e[0m"