#!/usr/local/bin/bash
# Buscaminash   version 1.1   traducido por perón
# El clásico juego de no morir reventado
 
 
shopt -s extglob
IFS=''
 
pieza=( $'\e[1;30m.' $'\e[1;34m1' $'\e[1;32m2' $'\e[1;35m3' $'\e[1;36m4' $'\e[1;31m5' $'\e[33m6' $'\e[1;37m7' $'\e[0;40;37m8' $'\e[0;40;37m#' $'\e[0;40;31mF' $'\e[0;40;33m?' $'\e[1;31m*' $'\e[0;40;31mx' )
tamano=( 'S ' 10 10 15   'M ' 15 15 33   'L ' 20 20 60   'XL' 30 20 90 )
 
function dibujarcampo()
{
 
  [[ "$dxt" ]] || { dxt=$mx; dyf=0; dyt=$my; }
 
  tput 'cup' $(( dyf+2 )) 0
  echo -n $'\e[40m'
  for ((j=dyf;j<dyt;j++)); do for ((i=0;i<dxt;i++)); do echo -n " ${pieza[tablero[j*mx+i]]}"; done; echo ' '; done
  echo -n $'\e[0m'
 
  dxt=''
 
}
 
function juegonuevo()
{
 
#  n="$( expr index 'nNmM' "$1" )" # line kept as human readable version :(
  n='nNmM'; n="${n%$1*}"; n=${#n}
 
  mx=${tamano[n*4+1]}; my=${tamano[n*4+2]}; mb=${tamano[n*4+3]}; mf=0
 
  echo -n $'\e[0m'
  clear
  echo 'Buscaminash   versión 1.1   Traducido por ~peron'
  echo "Campo : ${tamano[n*4]}   Tamaño : $mx*$my   Minas : $mb   Banderas : $mf    "$'\e[43;30m:)\e[0m'
 
  for ((i=0;i<mx*my;i++)); do bomb[i]=0; tablero[i]=9; done
  for ((i=0;i<mb;i++)); do while :; do r=$(( RANDOM%(mx*my) )); (( bomb[r] )) || break; done; bomb[r]=1; done
 
  dibujarcampo
  echo $'<\e[1mh\e[0m/\e[1mj\e[0m/\e[1mk\e[0m/\e[1ml\e[0m> Mover <\e[1mg\e[0m> Pisar <\e[1mf\e[0m> Bandera <\e[1mn\e[0m/\e[1mN\e[0m/\e[1mm\e[0m/\e[1mM\e[0m> Nuevo <\e[1mq\e[0m> Salir'
 
  cx=0; cy=0
  status=1
 
}
 
function juegotermino()
{
 
  for ((i=0;i<mx;i++)); do for ((j=0;j<my;j++)); do
    (( bomb[j*mx+i]==1 && tablero[j*mx+i]==9 )) && tablero[j*mx+i]=12
    (( bomb[j*mx+i]==0 && tablero[j*mx+i]==10 )) && tablero[j*mx+i]=13
  done; done
 
  dibujarcampo
  tput 'cup' 1 52
  echo -n $'\e[43;30m:(\e[0m'
 
  status=0
 
}
 
function darpaso()
{
  local i j
  local sx=${1:-$cx} sy=${2:-$cy}
 
  [[ "${tablero[sy*mx+sx]}" != @(9|10|11) ]] && return
  (( bomb[cy*mx+cx]==1 )) && { juegotermino; return; }
 
  [[ "$1" ]] || {
    dxt=$sx; dyf=$sy; dyt=$sy
    tput 'cup' 1 52
    echo -n $'\e[43;30m:o\e[0m'
  }
 
  (( dxt=dxt>sx?dxt:sx+1 )); (( dyf=dyf<sy?dyf:sy )); (( dyt=dyt>sy?dyt:sy+1 ))
 
  n=0
  for ((i=-1;i<=1;i++)); do for ((j=-1;j<=1;j++)); do
    (( (i!=0 || j!=0) && sx+i>=0 && sx+i<mx && sy+j>=0 && sy+j<my )) && (( bomb[(sy+j)*mx+(sx+i)]==1 )) && (( n++ ))
  done; done
  tablero[sy*mx+sx]=$n
 
  (( n )) || {
    for ((i=-1;i<=1;i++)); do for ((j=-1;j<=1;j++)); do
      (( (i!=0 || j!=0) && sx+i>=0 && sx+i<mx && sy+j>=0 && sy+j<my )) && darpaso $(( sx+i )) $(( sy+j ))
    done; done
  }
 
  [[ "$1" ]] || {
    dibujarcampo
    tput 'cup' 1 52
    echo -n $'\e[43;30m:)\e[0m'
  }
 
}
 
function ponerbandera()
{
 
  [[ ${tablero[cy*mx+cx]} != @(9|10|11) ]] && return
 
  tablero[cy*mx+cx]=$(( (tablero[cy*mx+cx]-9+1)%3+9 ))
 
  (( tablero[cy*mx+cx]==10 )) && (( mf++ ))
  (( tablero[cy*mx+cx]==11 )) && (( mf-- ))
 
  (( mf==mb )) && {
    n=0
    for ((i=0;i<mx;i++)); do for ((j=0;j<my;j++)); do
      (( bomb[j*mx+i]==1 && tablero[j*mx+i]==10 )) && (( n++ ))
    done; done
 
    tput 'cup' 1 52
    echo -n $'\e[43;30mB)\e[0m'
 
    status=0
  }
 
  tput 'cup' 1 47
  echo -en "\e[0m$mf  "
 
}
 
 
 
# |\/| /\ | |\|
 
juegonuevo 'n'
 
while :; do
 
  tput 'cup' $(( cy+2 )) $(( cx*2 ))
  echo -en "\e[1;40;37m[${pieza[tablero[cy*mx+cx]]}\e[1;37m]\b\b"
 
  read -s -n 1 a
  [[ "$a" == '␛' ]] && { read -s -n 1 a; [[ "$a" == '[' ]] && read -s -n 1 a; }
 
  echo -en "\b ${pieza[tablero[cy*mx+cx]]} \b\b"
 
  (( status!=1 )) && [[ "$a" != [nNmMrq] ]] && continue
 
  case "$a" in
    'h'|'a'|'D'|'4') (( cx>0?cx--:0 )) ;;
    'j'|'s'|'B'|'2') (( cy<my-1?cy++:0 )) ;;
    'k'|'w'|'A'|'8') (( cy>0?cy--:0 )) ;;
    'l'|'d'|'C'|'6') (( cx<mx-1?cx++:0 )) ;;
    'g'|' '|'') darpaso ;;
    'f'|'0') ponerbandera ;;
    'n'|'N'|'m'|'M') juegonuevo "$a" ;;
    'r') dibujarcampo ;;
    'q') break ;;
  esac
 
done
 
echo -n $'\e[0m'
clear