2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "ucioption.h"
36 /// move_from_string() takes a position and a string as input, and attempts to
37 /// convert the string to a move, using simple coordinate notation (g1f3,
38 /// a7a8q, etc.). In order to correctly parse en passant captures and castling
39 /// moves, we need the position. This function is not robust, and expects that
40 /// the input move is legal and correctly formatted.
42 Move
move_from_string(const Position
&pos
, const std::string
&str
) {
45 Color us
= pos
.side_to_move();
47 if(str
.length() < 4) return MOVE_NONE
;
49 // Read the from and to squares:
50 from
= square_from_string(str
.substr(0, 2));
51 to
= square_from_string(str
.substr(2, 4));
53 // Find the moving piece:
54 piece
= pos
.piece_on(from
);
56 // If the string has more than 4 characters, try to interpret the 5th
57 // character as a promotion:
58 if(type_of_piece(piece
) == PAWN
&& str
.length() >= 5) {
61 return make_promotion_move(from
, to
, KNIGHT
);
63 return make_promotion_move(from
, to
, BISHOP
);
65 return make_promotion_move(from
, to
, ROOK
);
67 return make_promotion_move(from
, to
, QUEEN
);
71 if(piece
== king_of_color(us
)) {
72 // Is this a castling move? A king move is assumed to be a castling
73 // move if the destination square is occupied by a friendly rook, or
74 // if the distance between the source and destination squares is more
76 if(pos
.piece_on(to
) == rook_of_color(us
))
77 return make_castle_move(from
, to
);
78 else if(square_distance(from
, to
) > 1) {
79 // This is a castling move, but we have to translate it to the
80 // internal "king captures rook" representation.
81 SquareDelta delta
= (to
> from
)? DELTA_E
: DELTA_W
;
84 pawn_rank(us
, s
) == RANK_1
&& pos
.piece_on(s
) != rook_of_color(us
);
86 if(pawn_rank(us
, s
) == RANK_1
&& pos
.piece_on(s
) == rook_of_color(us
))
87 return make_castle_move(from
, s
);
90 else if(piece
== pawn_of_color(us
)) {
91 // En passant move? We assume that a pawn move is an en passant move
92 // without further testing if the destination square is epSquare.
93 if(to
== pos
.ep_square())
94 return make_ep_move(from
, to
);
97 return make_move(from
, to
);
101 /// move_to_string() converts a move to a string in coordinate notation
102 /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we
103 /// print in the e1g1 notation in normal chess mode, and in e1h1 notation in
106 const std::string
move_to_string(Move move
) {
109 if(move
== MOVE_NONE
)
111 else if(move
== MOVE_NULL
)
115 if(move_from(move
) == SQ_E1
&& move_is_short_castle(move
)) {
116 str
= "e1g1"; return str
;
118 else if(move_from(move
) == SQ_E1
&& move_is_long_castle(move
)) {
119 str
= "e1c1"; return str
;
121 if(move_from(move
) == SQ_E8
&& move_is_short_castle(move
)) {
122 str
= "e8g8"; return str
;
124 else if(move_from(move
) == SQ_E8
&& move_is_long_castle(move
)) {
125 str
= "e8c8"; return str
;
128 str
= square_to_string(move_from(move
)) + square_to_string(move_to(move
));
129 if(move_promotion(move
))
130 str
+= piece_type_to_char(move_promotion(move
), false);
136 /// Overload the << operator, to make it easier to print moves.
138 std::ostream
&operator << (std::ostream
&os
, Move m
) {
139 return os
<< move_to_string(m
);
143 /// move_is_ok(), for debugging.
145 bool move_is_ok(Move m
) {
146 return square_is_ok(move_from(m
)) && square_is_ok(move_to(m
));