Basic (experimental) lazy evaluation
[owl.git] / san.c
blob3a3e7443b765197c94ec0d80a35b5ecf2c941c28
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 #include "common.h"
17 #include "attacks.h"
18 #include "board.h"
19 #include "move.h"
21 /* move in SAN notation */
22 char *
23 san_move(int move, char *buf)
25 int i;
26 int from, to;
27 char tmp[MAX_STRING];
28 int piece, cap_piece, promote_piece, type;
29 int move_legal, move_check, move_mate;
30 struct moves_t moves;
32 assert(move_is_legal(move));
34 buf[0] = '\0';
35 tmp[0] = '\0';
37 from = GET_FROM(move);
38 to = GET_TO(move);
39 piece = PIECE(move);
40 cap_piece = CAP_PIECE(move);
41 promote_piece = GET_PROMOTE(move);
42 type = GET_TYPE(move);
44 move_legal = move_check = move_mate = FALSE;
46 gen_legal_moves(&moves);
48 for (i = 0; i < moves.count; i++) {
49 if (make_move(moves.move[i], FALSE)) {
50 if (move == moves.move[i]) {
51 move_legal = TRUE;
53 if (IS_CHECKED(brd.wtm)) {
54 move_check = TRUE;
56 if (!gen_legal_moves(&moves))
57 move_mate = TRUE;
59 takeback();
60 break;
62 takeback();
66 if (!move_legal)
67 return NULL;
69 if (type & TYPE_CASTLE) {
70 if (to == G1 || to == G8)
71 sprintf(buf, "%s", "O-O");
72 else
73 sprintf(buf, "%s", "O-O-O");
74 } else {
75 if (piece == PAWN) {
76 if (cap_piece || (type & TYPE_ENPASSANT))
77 sprintf(buf, "%cx%s", (char) (_FILE(from) + 'a'), char_board[to]);
78 else
79 sprintf(buf, "%s", char_board[to]);
80 if (promote_piece) {
81 sprintf(tmp, "=%c", char_pieces[promote_piece]);
82 strcat(buf, tmp);
84 } else {
85 sprintf(buf, "%c", char_pieces[piece]);
87 gen_legal_moves(&moves);
89 for (i = 0; i < moves.count; i++) {
90 if (move != moves.move[i]) {
91 if (piece == PIECE(moves.move[i]) && to == GET_TO(moves.move[i])) {
92 /* move is ambiguous */
93 if (_FILE(from) == _FILE(GET_FROM(moves.move[i])))
94 sprintf(tmp, "%c", (char) (_RANK(from) + '1'));
95 else
96 sprintf(tmp, "%c", (char) (_FILE(from) + 'a'));
97 strcat(buf, tmp);
98 break;
103 if (cap_piece)
104 sprintf(tmp, "x%s", char_board[to]);
105 else
106 sprintf(tmp, "%s", char_board[to]);
107 strcat(buf, tmp);
111 if (move_mate)
112 strcat(buf, "#");
113 else if (move_check)
114 strcat(buf, "+");
116 return buf;