[go: up one dir, main page]

Menu

[0ec8fa]: / doc / chess / dce.sh  Maximize  Restore  History

Download this file

280 lines (251 with data), 5.2 kB

  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
#!/bin/bash
# This script is a simple chess engine for dbacl and XBoard.
# It is based on the essay
#
# "Can spam filters play chess?" (c) 2005 Laird A. Breyer
# http://www.lbreyer.com/spam_chess.html
#
# The essay explains the construction of this engine by
# successive modifications of the dce-basic.sh script.
#
# THE dce-*.sh SCRIPTS ARE INCOMPLETE ENGINES AND INTENDED
# ONLY FOR ILLUSTRATION OF POINTS RAISED IN THE ESSAY.
# IF YOU WANT TO TRY OUT YOUR OWN MODIFICATIONS OF THE
# CHESS ENGINE, YOU SHOULD MAKE A COPY OF THE FINAL (dce.sh)
# FILE AND CHANGE THAT COPY.
#
# Revision history:
#
# 28/06/2005 (Laird Breyer) Initial release.
#
DBACL=./dbacl/src/dbacl
SAN=./SAN/SAN_SRC/san
TMP=.
DCE=dce.$$
SANOK="no"
PGN="$TMP/$DCE.current.pgn"
GAMELINE="$TMP/$DCE.current.gameline"
SCORES="$TMP/$DCE.current.scores"
CAPTURES="$SCORES.cap"
ENGINEMOVE="$TMP/$DCE.current.emove"
SANOUT="$TMP/$DCE.current.stdout"
SANERR="$TMP/$DCE.current.stderr"
RANPLAY="no"
SIDE="black"
MOVENOW="no"
FORCEMODE="no"
CATFILE="./BlackWinDraw"
trap "" SIGTERM
trap "" SIGINT
function exec_san() {
rm -rf $PGN.new $SANOUT $SANERR
echo -ne "svop namd nabd napr\nlfer $PGN\n$1\nsfer $PGN.new" \
| "$SAN" > "$SANOUT" 2> "$SANERR"
if grep Error "$SANOUT" > /dev/null; then
echo "Error (illegal move): $cmd"
return 1
else
mv $PGN.new $PGN
fi
return 0
}
function do_reset() {
rm -f $PGN
touch $PGN
exec_san ""
SIDE="black"
FORCEMODE="no"
MOVENOW="no"
}
function do_move() {
if [ "$SANOK" != "yes" ]; then
echo "Illegal move (you must use SAN): $1"
echo "Error (cannot play): $1"
fi
if exec_san "$1"; then
MOVENOW="yes"
fi
}
function echo_gameline() {
cat "$PGN" \
| sed -e 's/\r//g' \
| sed -e :a -e '$!N;s/\n\([a-hKQNBRO0-9]\)/ \1/;ta' -e 'P;D' \
| sed -e 's/^ *//' \
| grep '^1\.' \
| sed 's/[0-9]*\.[ ]*//g' \
| sed 's/ \*.*$//'
}
function echo_completions() {
# legal moves are in $SANOUT
cat "$SANOUT" \
| grep '.* : 1$' \
| cut -f 1 -d ' ' \
| while read move; do
echo "`cat $GAMELINE` $move"
done
}
function randomizer() {
# assume stdin is sorted and $2 is the score
awk '
{
if( cf == 0 ) {
cf = $2;
}
score[NR] = $2 - cf;
line[NR] = $0;
}
END{
srand();
while(1) {
x = int(rand() * NR) + 1;
t = -log(rand());
if( log(2) * score[x] < t ) {
print line[x];
break;
}
}
}
'
}
function echo_best_move() {
# assume $1 is sorted already
if [ "$RANPLAY" = "yes" ]; then
cat $1 \
| randomizer \
| sed -e 's/^.* //'
else
cat "$1" \
| head -1 \
| sed -e 's/^.* //'
fi
}
function combine_halfmoves() {
sed -e 's/$/ Z/' -e 's/ \([^ ]* *[^ ]\)/_\1/g' -e 's/[ ]*Z$//'
}
function split_fullmove() {
sed -e 's/.*_//'
}
function get_captures() {
cat "$SCORES" \
| grep '^.* [^ ]*x[^ _]*$' \
> "$CAPTURES"
# we rearrange the captures in order of least important piece to
# most important piece
grep -v '[NBRQK][^_ ]*$' "$CAPTURES" > "$CAPTURES.new"
grep 'N[^_ ]*$' "$CAPTURES" >> "$CAPTURES.new"
grep 'B[^_ ]*$' "$CAPTURES" >> "$CAPTURES.new"
grep 'R[^_ ]*$' "$CAPTURES" >> "$CAPTURES.new"
grep 'Q[^_ ]*$' "$CAPTURES" >> "$CAPTURES.new"
# king is too valuable to force captures
# grep 'K[^_ ]*$' "$CAPTURES" >> "$CAPTURES.new"
mv "$CAPTURES.new" "$CAPTURES"
N=`cat $CAPTURES| wc -l`
if [ "$N" = "0" -o "$N" = "1" ]; then
# don't recommend capture
return 1
fi
# tell engine to make a capturing move
return 0
}
function do_engine_move() {
if exec_san "enum 1"; then
echo_gameline > "$GAMELINE"
# make all completions and let dbacl decide
echo_completions \
| combine_halfmoves \
| "$DBACL" -n -c "$CATFILE" -f 1 \
| sort -k 2 -n \
> "$SCORES"
if [ "`cat $SCORES| wc -l`" = "0" ]; then
# no moves left, game over!
# the gameline contains the result
echo "`cat $GAMELINE | sed 's/^.* //'` {Play again?}"
return 0
else
# pick best scoring move
echo_best_move "$SCORES" \
| split_fullmove \
> "$ENGINEMOVE"
if get_captures; then
echo_best_move "$CAPTURES" \
| split_fullmove \
> "$ENGINEMOVE"
fi
if exec_san "`cat $ENGINEMOVE`"; then
echo "move `cat $ENGINEMOVE`"
return 0
fi
fi
fi
return 1
}
function get_side_to_play() {
N=`echo_gameline | wc -w`
if [ "`expr $N % 2`" = "0" ]; then
SIDE="white"
else
SIDE="black"
fi
}
while read cmd; do
case "$cmd" in
xboard)
echo "feature san=1 done=1"
;;
"accepted san")
SANOK="yes"
;;
random)
RANPLAY="yes"
;;
quit)
exit 0
;;
new)
do_reset
;;
variant*)
echo "Error (only standard chess please): $cmd"
;;
force)
FORCEMODE="yes"
;;
white)
SIDE="black"
;;
black)
SIDE="white"
;;
go)
get_side_to_play
FORCEMODE="no"
MOVENOW="yes"
;;
[a-h][x1-8]*)
do_move "$cmd"
;;
[KQBNRP][xa-h]*)
do_move "$cmd"
;;
O-O*)
do_move "$cmd"
;;
analyze)
echo "Error (unknown command): $cmd"
;;
*)
# ignore other commands
echo "Error (command not implemented): $cmd"
;;
esac
if [ "${FORCEMODE}${MOVENOW}" = "noyes" ]; then
if do_engine_move; then
MOVENOW="no"
else
echo "Error (engine blew up): kaboom"
exit 1
fi
fi
done
# don't forget to clean up
rm -f $DCE.current.*