-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.sh
151 lines (131 loc) · 3.06 KB
/
generator.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
#!/bin/bash
# calculate matrix size row col
# where
# matrix an array, representing a 2D matrix.
# size the size of above (square) matrix.
# row the row of the element that should be calculated.
# col the column of the element.
calculate() {
size=$1; row=$2; col=$3
shift 3
matrix=("$@")
if [ "${matrix[$((row * size + col))]}" = "X" ]; then echo 9
else
count=0
for x in "-1" "0" "1"; do for y in "-1" "0" "1"; do
if [ "$x" -ne 0 ] || [ "$y" -ne 0 ]; then
if (( row + y >= 0 && row + y < size )); then
if (( col + x >= 0 && col + x < size )); then
if [[ "${matrix[$(( (row+y)*size + col + x ))]}" =~ [X9] ]]; then
((count++))
fi
fi
fi
fi
done; done
echo $count
fi
}
minefield=
outfile=
size=0
# Process command line arguments.
if [ $1 == "-r" ]; then
size=$2
shift
for (( i=0; i<size*size; i++ )); do
minefield[$i]=0;
if (( RANDOM % 5 == 0 )); then
minefield[$i]=X
fi
done
else
{
minestring=
while read line; do
minestring="$minestring$(echo "$line" | sed 's/./& /g')"
let "size = size + 1"
done
minefield=($minestring)
} <$1
fi
if [ -z $2 ]
then outfile="~temp.sed"
else outfile=$2
fi
{
for (( r=0; r<size; r++)); do for (( c=0; c<size; c++ )); do
minefield[$((r*size + c))]=$(calculate "$size" "$r" "$c" ${minefield[@]})
done; done
# Form dependend parts.
dots=
count='s/'
for (( i=0; i<size*size; i++ )); do
dots="$dots."
if [ "${minefield[$i]}" = "9" ]; then count="$count"'F'; fi
done
count="$count"'//'
regex=
for (( i=0; i<size-1; i++ )); do regex="$regex"'\.[\()]*'; done
substitutions=
empty_field=
process_input=
for (( r=0; r<size; r++ )); do
empty_field="$empty_field\n"
for (( c=0; c<size; c++ )); do
substitutions="$substitutions""s/$r,$c/&=${minefield[$((r*size+c))]}/;"
empty_field="$empty_field-"
process_input="$process_input"'s/^\('"$(echo "${dots:0:$((r*size + c))}"'\).\('"${dots:$((r*size + c + 1))}"'\)' | sed 's/[\()]*'$regex'\.\(\\(\)*/&\\n/g' | sed 's/\\n\\)$/\\)/')"'\n'"$r,$c="'\(.\)/\1\3\2/;t process_input;'
done
done
empty_field=${empty_field:2} # remove the leading \n
cleaner='s/.*\n\('"$(echo $empty_field | tr "-" ".")"'\)/\1/'
# Print completely.
cat <<HERE
#!/bin/sed -f
:parse
s/\\(.*\\)F/\1=F/
t flagged
$substitutions
:flagged
H
/=9/ b game_over
s/.*/$empty_field/
G
s/\\n\\n/\\n/
:process_input
$process_input
/-/ !b finished
:new_input
p
a\\
Please input new coordinats
d
:game_over
a\\
You lost.
q
:finished
H
s/[^F]//g
$count
/^..*$/t cheater
:won
a\\
Shit this guy is good.
q
:cheater
g
s/\\n[^\\n]*$//
x
$cleaner
b new_input
HERE
} >$outfile
if [ -z $2 ]; then
echo "Ready."
sed -f $outfile
rm $outfile
else
chmod a+x $outfile
fi