-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathminesweeper.sh
executable file
·163 lines (112 loc) · 3.73 KB
/
minesweeper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
# Mine Sweeper version 1.1 august 2008 written by Feherke
# the classic game in text mode
shopt -s extglob
IFS=''
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' )
size=( 'S ' 10 10 15 'M ' 15 15 33 'L ' 20 20 60 'XL' 30 20 90 )
function drawboard()
{
[[ "$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 " ${piece[board[j*mx+i]]}"; done; echo ' '; done
echo -n $'\e[0m'
dxt=''
}
function newgame()
{
# n="$( expr index 'nNmM' "$1" )" # line kept as human readable version :(
n='nNmM'; n="${n%$1*}"; n=${#n}
mx=${size[n*4+1]}; my=${size[n*4+2]}; mb=${size[n*4+3]}; mf=0
echo -n $'\e[0m'
clear
echo 'Mine Sweeper version 1.1 august 2008 written by Feherke'
echo "board : ${size[n*4]} size : $mx*$my mine : $mb flag : $mf "$'\e[43;30m:)\e[0m'
for ((i=0;i<mx*my;i++)); do bomb[i]=0; board[i]=9; done
for ((i=0;i<mb;i++)); do while :; do r=$(( RANDOM%(mx*my) )); (( bomb[r] )) || break; done; bomb[r]=1; done
drawboard
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'
cx=0; cy=0
status=1
}
function gameover()
{
for ((i=0;i<mx;i++)); do for ((j=0;j<my;j++)); do
(( bomb[j*mx+i]==1 && board[j*mx+i]==9 )) && board[j*mx+i]=12
(( bomb[j*mx+i]==0 && board[j*mx+i]==10 )) && board[j*mx+i]=13
done; done
drawboard
tput 'cup' 1 52
echo -n $'\e[43;30m:(\e[0m'
status=0
}
function makestep()
{
local i j
local sx=${1:-$cx} sy=${2:-$cy}
[[ "${board[sy*mx+sx]}" != @(9|10|11) ]] && return
(( bomb[cy*mx+cx]==1 )) && { gameover; 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
board[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 )) && makestep $(( sx+i )) $(( sy+j ))
done; done
}
[[ "$1" ]] || {
drawboard
tput 'cup' 1 52
echo -n $'\e[43;30m:)\e[0m'
}
}
function putflag()
{
[[ ${board[cy*mx+cx]} != @(9|10|11) ]] && return
board[cy*mx+cx]=$(( (board[cy*mx+cx]-9+1)%3+9 ))
(( board[cy*mx+cx]==10 )) && (( mf++ ))
(( board[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 && board[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 "
}
# |\/| /\ | |\|
newgame 'n'
while :; do
tput 'cup' $(( cy+2 )) $(( cx*2 ))
echo -en "\e[1;40;37m[${piece[board[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 ${piece[board[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'|' '|'') makestep ;;
'f'|'0') putflag ;;
'n'|'N'|'m'|'M') newgame "$a" ;;
'r') drawboard ;;
'q') break ;;
esac
done
echo -n $'\e[0m'
clear