File: attacks.c

package info (click to toggle)
gnushogi 1.5~git20140725-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,900 kB
  • sloc: ansic: 12,731; sh: 4,104; makefile: 128
file content (259 lines) | stat: -rw-r--r-- 5,556 bytes parent folder | download | duplicates (4)
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
/*
 * FILE: attacks.c
 *
 * ----------------------------------------------------------------------
 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
 *
 * GNU SHOGI is based on GNU CHESS
 *
 * Copyright (c) 1988, 1989, 1990 John Stanback
 * Copyright (c) 1992 Free Software Foundation
 *
 * This file is part of GNU SHOGI.
 *
 * GNU Shogi is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with GNU Shogi; see the file COPYING. If not, see
 * <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------
 *
 */

#include "gnushogi.h"


#define CHECK_DISTANCE

/*
 * See if any piece with color 'side' attacks sq.
 * *blockable == attack could be blocked by drop
 */

int
SqAttacked(short square, short side, short *blockable)
{
#ifdef SAVE_NEXTPOS
    short d;
#else
    unsigned char *ppos, *pdir;
#endif

    short u, ptyp;

    if (MatchSignature(threats_signature[side]))
    {
        *blockable = true; /* don't know */
        return Anyattack(side, square);
    }

    /*
     * First check neighbouring squares,
     * then check Knights.
     * then check Bishops,
     * then (last) check Rooks,
     */

    *blockable = false;

    /* try a capture from direct neighboured squares */

    ptyp = ptype[black][king];

#ifdef SAVE_NEXTPOS
    u = first_direction(ptyp, &d, square);
#else
    pdir = (*nextdir[ptyp])[square];
    u = pdir[square];
#endif

    do
    {
        if (color[u] == side) /* can piece reach square in one step ? */
#ifdef CHECK_DISTANCE
        {
            if (piece_distance(side, board[u], u, square) == 1)
                return true;
        }
#else
        {
            short v;
            short ptypv = ptype[side][board[u]];
#ifdef SAVE_NEXTPOS
            short dv;
            v = first_direction(ptypv, &dv, u);
#else
            unsigned char *qdir;
            qdir = (*nextdir[ptypv])[u];
            v = qdir[u];
#endif
            do
            {
                if (v == square)
                    return true;
#ifdef SAVE_NEXTPOS
                v = next_direction(ptypv, &dv, u);
#else
                v = qdir[v];
#endif
            }
            while (v != u);
        }
#endif

#ifdef SAVE_NEXTPOS
        u = next_direction(ptyp, &d, square);
#else
        u = pdir[u];
#endif
    }
    while (u != square);

#ifndef MINISHOGI
    /* try a knight capture (using xside's knight moves) */

    ptyp = ptype[side ^ 1][knight];

#ifdef SAVE_NEXTPOS
    u = first_direction(ptyp, &d, square);
#else
    pdir = (*nextdir[ptyp])[square];
    u = pdir[square];
#endif

    do
    {
        if (color[u] == side && board[u] == knight)
            return true;

#ifdef SAVE_NEXTPOS
        u = next_direction(ptyp, &d, square);
#else
        u = pdir[u];
#endif
    }
    while (u != square);
#endif	/* MINISHOGI */

    *blockable = true;

    /* try a (promoted) bishop capture */

    ptyp = ptype[black][bishop];

#ifdef SAVE_NEXTPOS
    u = first_direction(ptyp, &d, square);
#else
    ppos = (*nextpos[ptyp])[square];
    pdir = (*nextdir[ptyp])[square];
    u = ppos[square];
#endif

    do
    {
        if (color[u] == neutral)
#ifdef SAVE_NEXTPOS
            u = next_position(ptyp, &d, square, u);
#else
        u = ppos[u];
#endif
        else
        {
            if (color[u] == side && (unpromoted[board[u]] == bishop))
                return true;

#ifdef SAVE_NEXTPOS
            u = next_direction(ptyp, &d, square);
#else
            u = pdir[u];
#endif
        }
    }
    while (u != square);

    /* try a (promoted) rook capture */

    ptyp = ptype[black][rook];

#ifdef SAVE_NEXTPOS
    u = first_direction(ptyp, &d, square);
#else
    ppos = (*nextpos[ptyp])[square];
    pdir = (*nextdir[ptyp])[square];
    u = ppos[square];
#endif

    do
    {
        if (color[u] == neutral)
#ifdef SAVE_NEXTPOS
        {
            u = next_position(ptyp, &d, square, u);
        }
#else
        {
            u = ppos[u];
        }
#endif
        else
        {
            if (color[u] == side && (unpromoted[board[u]] == rook))
                return true;

#ifdef SAVE_NEXTPOS
            u = next_direction(ptyp, &d, square);
#else
            u = pdir[u];
#endif
        }
    }
    while (u != square);

#ifndef MINISHOGI
    /* try a lance capture (using xside's lance moves) */

    ptyp = ptype[side ^ 1][lance];

#ifdef SAVE_NEXTPOS
    u = first_direction(ptyp, &d, square);
#else
    ppos = (*nextpos[ptyp])[square];
    u = ppos[square];
#endif

    do
    {
        if (color[u] == neutral)
#ifdef SAVE_NEXTPOS
        {
            u = next_position(ptyp, &d, square, u);
        }
#else
        {
            u = ppos[u];
        }
#endif
        else
        {
            if ((color[u] == side) && (board[u] == lance))
                return true;

            u = square;
        }
    }
    while (u != square);
#endif	/* MINISHOGI */

    return false;
}