-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdll_game
343 lines (303 loc) · 6.59 KB
/
dll_game
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#! /bin/bash
########################
OnFinish()
{
clear
if [ "$P2Ships_down" -eq 10 ]
then
TypeWord "P1_wins!" 5 3 cyan
else
TypeWord "P2_wins!" 5 3 magenta
fi
gotoXY 12 0
echo "P1 accuracy = $((P2Hits*100/$P2Shots)).$((P2Hits*100%$P2Shots*100/$P2Shots))%"
echo "P2 accuracy = $((P1Hits*100/$P1Shots)).$((P1Hits*100%$P1Shots*100/$P1Shots))%"
echo
echo -n "Press [Enter] to continue..."
read
tput me
clear
}
########################
Check()
{
# $1 - is shot, e.g. b7
case $(GetValue "P$Now_Shooting""$1") in
("1" | "2" | "3") echo 0;;
(*) echo 1;;
esac
}
########################
DoShot()
{
# $1 - is shot, e.g. b7
Inc "P$Now_Shooting"Shots
cell="P$Now_Shooting""$1"
if [ $(GetValue $cell) = "2" ]
then
Inc "P$Now_Shooting"Hits
echo -n "Shot done in [$1] - hit!"
SetValue $cell 5
return 0
else
echo -n "Shot done in [$1] - miss!"
SetValue $cell 4
return 1
fi
echo $(GetValue $cell)
}
########################
GetShot()
{
if [ "$Now_Shooting" = "1" ]
then Z=2
else Z=1
fi
flag=`eval echo "Player"$Z`
player=`eval echo "$"$flag`
if [ "$player" = "AI" ]
then sleep 1; AI_turn; Current="AI"
else Human_turn; Current="Human"
fi
}
########################
Human_turn()
{
OK=1
until [ "$OK" = "0" ]
do
echo -n "Next shot: "
read Shot
if [ $Shot = "rand" ]; then Human_random; fi
OK=$(Check $Shot)
if [ $Shot = "exit" ]; then OK=0; fi
done
if [ $Shot != "exit" ]; then DoShot $Shot; fi
hit=`echo $?`
}
########################
AI_turn()
{
# init last_ and prev_ shot variables.
temp=`eval echo "P"$Now_Shooting"Last_Shot"`
eval Last_Shot="$"$temp
temp=`eval echo "P"$Now_Shooting"Prev_Shot"`
eval Prev_Shot="$"$temp
if [ "$Last_Shot" = "a0" ]
then
AI_random
Prev_Shot="a0"
else
AI_kill_ship
fi
# init last_ and prev_ shot variables.
eval "P"$Now_Shooting"Last_Shot"="$Last_Shot"
eval "P"$Now_Shooting"Prev_Shot"="$Prev_Shot"
}
########################
AI_kill_ship()
{
str="abcdefghjk"
x=`echo $Last_Shot $str | awk '{print index($2,substr($0, 1, 1))}'`
y=`echo $Last_Shot | awk '{print substr($0, 2, 2)}'`
if [ "$Prev_Shot" = "a0" ]
then
OK=1
until [ "$OK" = "0" ]
do
FRandom 3; x_next=$(($RandNumber+$x-1))
y_next=$y
if [ "$x" = "$x_next" ]
then FRandom 3; y_next=$(($RandNumber+$y-1)); fi
position=`echo $str $x_next | awk '{print substr($1, $2, 1)}'`
Shot=`eval echo $position$y_next`
OK=$(Check $Shot)
done
DoShot $Shot
hit=`echo $?`
if [ "$hit" -eq 0 ]
then
Prev_Shot=$Last_Shot
Last_Shot=$Shot
fi
else
AI_find_next_cell
position=`echo $str $x_next | awk '{print substr($1, $2, 1)}'`
Shot=`eval echo $position$y_next`
DoShot $Shot
fi
}
########################
AI_find_next_cell()
{
str="abcdefghjk"
x=`echo "$Last_Shot" $str | awk '{print index($2,substr($0, 1, 1))}'`
y=`echo "$Last_Shot" | awk '{print substr($0, 2, 2)}'`
x_prev=`echo $Prev_Shot $str | awk '{print index($2,substr($0, 1, 1))}'`
y_prev=`echo $Prev_Shot | awk '{print substr($0, 2, 2)}'`
if [ "$x" = "$x_prev" ]
then
x_next=$x
y_next_m1=$(($y*2-$y_prev))
y_next_m2=$(($y*3-$y_prev*2))
y_next_p1=$(($y_prev*2-$y))
y_next_p2=$(($y_prev*3-$y*2))
flag1="minus"
flag2="plus"
position=`echo $str $x | awk '{print substr($1, $2, 1)}'`
temp=`eval echo "P"$Now_Shooting"$position"$y_next_m1`
value_1=`eval echo "$"$temp`
case $value_1 in
("1" | "2" | "3") y_next=$y_next_m1;;
("5") flag2="minus";;
(*) flag1="plus";;
esac
if [ "$flag2" = "minus" ]
then
temp=`eval echo "P"$Now_Shooting"$position"$y_next_m2`
value_2=`eval echo "$"$temp`
case $value_2 in
("1" | "2" | "3") y_next=$y_next_m2;;
(*) flag1="plus";;
esac
fi
if [ "$flag1" = "plus" ]
then
temp=`eval echo "P"$Now_Shooting"$position"$y_next_p1`
value_3=`eval echo "$"$temp`
case $value_3 in
("1" | "2" | "3") y_next="$y_next_p1" ;;
(*) y_next="$y_next_p2" ;;
esac
fi
# - hznt=0
else
y_next=$y
x_next_m1=$(($x*2-$x_prev))
x_next_m2=$(($x*3-$x_prev*2))
x_next_p1=$(($x_prev*2-$x))
x_next_p2=$(($x_prev*3-$x*2))
flag1="minus"
flag2="plus"
position1=`echo $str $x_next_m1 | awk '{print substr($1, $2, 1)}'`
position2=`echo $str $x_next_m2 | awk '{print substr($1, $2, 1)}'`
temp=`eval echo "P"$Now_Shooting$position1$y`
value_1=`eval echo "$"$temp`
case $value_1 in
("1" | "2" | "3") x_next=$x_next_m1;;
("5") flag2="minus";;
(*) flag1="plus";;
esac
if [ "$flag2" = "minus" ] ; then
temp=`eval echo "P"$Now_Shooting$position2$y`
value_2=`eval echo "$"$temp`
case $value_2 in
("1" | "2" | "3") x_next=$x_next_m2;;
(*) flag1="plus";;
esac; fi
if [ "$flag1" = "plus" ] ; then
position3=`echo $str $x_next_p1 | awk '{print substr($1, $2, 1)}'`
temp=`eval echo "P"$Now_Shooting$position3$y`
value_3=`eval echo "$"$temp`
case $value_3 in
("1" | "2" | "3") x_next=$x_next_p1;;
(*) x_next=$x_next_p2;;
esac; fi
fi
}
########################
AI_random()
{
eval AI_random_shots="$"P"$Now_Shooting"AI_random_shots
AI_random_shots=$(($AI_random_shots+1))
eval P"$Now_Shooting"AI_random_shots=$AI_random_shots
flag=$(($AI_random_shots%(12/$level) ))
OK=1
until [ "$OK" = "0" ]
do
if [ "$flag" = "0" ]
then
Shot=$(GetRandomShot)
Pos=`eval echo "$"P"$Now_Shooting"$Shot`
if [ "$Pos" = "2" ]; then OK=0; fi
else
Shot=$(GetRandomShot)
OK=$(Check $Shot)
fi
done
DoShot $Shot
hit=`echo $?`
if [ "$hit" -eq 0 ]
then
Last_Shot=$Shot
else
Last_Shot="a0"
fi
}
########################
Human_random()
{
OK=1
until [ "$OK" = "0" ]
do
Shot=$(GetRandomShot)
OK=$(Check $Shot)
done
}
########################
GetRandomShot()
{
# will return something like 'b8'
FRandom 10; index=$((RandNumber+1))
FRandom 10; number=$((RandNumber+1))
letter=$(SubString "abcdefghjk" $index 1)
echo $letter$number
}
########################
Game()
{
SetBackgroundColor;
SetColor;
clear
if [ "$Player1" = "Human" ]
then
SetShips P1
else
SetShipsRandom P1
fi
if [ "$Player2" = "Human" ]
then
SetShips P2
else
SetShipsRandom P2
fi
clear
ShowMatrix P1
ShowMatrix P2
until [ $(Or "$P1Ships_down = 10" "$P2Ships_down = 10") = $True ]
do
if [ "$First_Shot" = "false" ]
then
if [ "$hit" = "0" ]
then
eval ShowMatrix P"$Now_Shooting"
else
if [ "$Now_Shooting" = "1" ]
then ShowMatrix P2
else ShowMatrix P1
fi
fi
fi
GetShot
if [ "$hit" = "0" ]
then
CheckShip $Shot
else
Now_Shooting=$(Ternary "$Now_Shooting = 1" 2 1)
fi
sleep 1
First_Shot="false"
if [ $Shot = "exit" ] ; then P1Ships_down=10; fi
done
if [ $Shot != "exit" ] ; then OnFinish; fi
}