helpdesk talks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.7 KiB

  1. #!/bin/bash
  2. # Mine Sweeper version 1.1 august 2008 written by Feherke
  3. # the classic game in text mode
  4. shopt -s extglob
  5. IFS=''
  6. piece=( $'\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' )
  7. size=( 'S ' 10 10 15 'M ' 15 15 33 'L ' 20 20 60 'XL' 30 20 90 )
  8. function drawboard()
  9. {
  10. [[ "$dxt" ]] || { dxt=$mx; dyf=0; dyt=$my; }
  11. tput 'cup' $(( dyf+2 )) 0
  12. echo -n $'\e[40m'
  13. for ((j=dyf;j<dyt;j++)); do for ((i=0;i<dxt;i++)); do echo -n " ${piece[board[j*mx+i]]}"; done; echo ' '; done
  14. echo -n $'\e[0m'
  15. dxt=''
  16. }
  17. function newgame()
  18. {
  19. # n="$( expr index 'nNmM' "$1" )" # line kept as human readable version :(
  20. n='nNmM'; n="${n%$1*}"; n=${#n}
  21. mx=${size[n*4+1]}; my=${size[n*4+2]}; mb=${size[n*4+3]}; mf=0
  22. echo -n $'\e[0m'
  23. clear
  24. echo 'Mine Sweeper version 1.1 august 2008 written by Feherke'
  25. echo "board : ${size[n*4]} size : $mx*$my mine : $mb flag : $mf "$'\e[43;30m:)\e[0m'
  26. for ((i=0;i<mx*my;i++)); do bomb[i]=0; board[i]=9; done
  27. for ((i=0;i<mb;i++)); do while :; do r=$(( RANDOM%(mx*my) )); (( bomb[r] )) || break; done; bomb[r]=1; done
  28. drawboard
  29. echo $'<\e[1mh\e[0m/\e[1mj\e[0m/\e[1mk\e[0m/\e[1ml\e[0m> Move <\e[1mg\e[0m> Step <\e[1mf\e[0m> Flag <\e[1mn\e[0m/\e[1mN\e[0m/\e[1mm\e[0m/\e[1mM\e[0m> New <\e[1mq\e[0m> Quit'
  30. cx=0; cy=0
  31. status=1
  32. }
  33. function gameover()
  34. {
  35. for ((i=0;i<mx;i++)); do for ((j=0;j<my;j++)); do
  36. (( bomb[j*mx+i]==1 && board[j*mx+i]==9 )) && board[j*mx+i]=12
  37. (( bomb[j*mx+i]==0 && board[j*mx+i]==10 )) && board[j*mx+i]=13
  38. done; done
  39. drawboard
  40. tput 'cup' 1 52
  41. echo -n $'\e[43;30m:(\e[0m'
  42. status=0
  43. }
  44. function makestep()
  45. {
  46. local i j
  47. local sx=${1:-$cx} sy=${2:-$cy}
  48. [[ "${board[sy*mx+sx]}" != @(9|10|11) ]] && return
  49. (( bomb[cy*mx+cx]==1 )) && { gameover; return; }
  50. [[ "$1" ]] || {
  51. dxt=$sx; dyf=$sy; dyt=$sy
  52. tput 'cup' 1 52
  53. echo -n $'\e[43;30m:o\e[0m'
  54. }
  55. (( dxt=dxt>sx?dxt:sx+1 )); (( dyf=dyf<sy?dyf:sy )); (( dyt=dyt>sy?dyt:sy+1 ))
  56. n=0
  57. for ((i=-1;i<=1;i++)); do for ((j=-1;j<=1;j++)); do
  58. (( (i!=0 || j!=0) && sx+i>=0 && sx+i<mx && sy+j>=0 && sy+j<my )) && (( bomb[(sy+j)*mx+(sx+i)]==1 )) && (( n++ ))
  59. done; done
  60. board[sy*mx+sx]=$n
  61. (( n )) || {
  62. for ((i=-1;i<=1;i++)); do for ((j=-1;j<=1;j++)); do
  63. (( (i!=0 || j!=0) && sx+i>=0 && sx+i<mx && sy+j>=0 && sy+j<my )) && makestep $(( sx+i )) $(( sy+j ))
  64. done; done
  65. }
  66. [[ "$1" ]] || {
  67. drawboard
  68. tput 'cup' 1 52
  69. echo -n $'\e[43;30m:)\e[0m'
  70. }
  71. }
  72. function putflag()
  73. {
  74. [[ ${board[cy*mx+cx]} != @(9|10|11) ]] && return
  75. board[cy*mx+cx]=$(( (board[cy*mx+cx]-9+1)%3+9 ))
  76. (( board[cy*mx+cx]==10 )) && (( mf++ ))
  77. (( board[cy*mx+cx]==11 )) && (( mf-- ))
  78. (( mf==mb )) && {
  79. n=0
  80. for ((i=0;i<mx;i++)); do for ((j=0;j<my;j++)); do
  81. (( bomb[j*mx+i]==1 && board[j*mx+i]==10 )) && (( n++ ))
  82. done; done
  83. tput 'cup' 1 52
  84. echo -n $'\e[43;30mB)\e[0m'
  85. status=0
  86. }
  87. tput 'cup' 1 47
  88. echo -en "\e[0m$mf "
  89. }
  90. # |\/| /\ | |\|
  91. newgame 'n'
  92. while :; do
  93. tput 'cup' $(( cy+2 )) $(( cx*2 ))
  94. echo -en "\e[1;40;37m[${piece[board[cy*mx+cx]]}\e[1;37m]\b\b"
  95. read -s -n 1 a
  96. [[ "$a" == '' ]] && { read -s -n 1 a; [[ "$a" == '[' ]] && read -s -n 1 a; }
  97. echo -en "\b ${piece[board[cy*mx+cx]]} \b\b"
  98. (( status!=1 )) && [[ "$a" != [nNmMrq] ]] && continue
  99. case "$a" in
  100. 'h'|'a'|'D'|'4') (( cx>0?cx--:0 )) ;;
  101. 'j'|'s'|'B'|'2') (( cy<my-1?cy++:0 )) ;;
  102. 'k'|'w'|'A'|'8') (( cy>0?cy--:0 )) ;;
  103. 'l'|'d'|'C'|'6') (( cx<mx-1?cx++:0 )) ;;
  104. 'g'|' '|'') makestep ;;
  105. 'f'|'0') putflag ;;
  106. 'n'|'N'|'m'|'M') newgame "$a" ;;
  107. 'r') drawboard ;;
  108. 'q') break ;;
  109. esac
  110. done
  111. echo -n $'\e[0m'
  112. clear