random bash scripts
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.

144 lines
3.7 KiB

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