]> git.sesse.net Git - stockfish/blob - src/move.cpp
Add new superlinear interpolator
[stockfish] / src / move.cpp
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
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.
9   
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.
14   
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/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #include <cassert>
25
26 #include "move.h"
27 #include "piece.h"
28 #include "position.h"
29 #include "ucioption.h"
30
31
32 ////
33 //// Functions
34 ////
35
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.
41
42 Move move_from_string(const Position &pos, const std::string &str) {
43   Square from, to;
44   Piece piece;
45   Color us = pos.side_to_move();
46
47   if(str.length() < 4) return MOVE_NONE;
48
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));
52
53   // Find the moving piece:
54   piece = pos.piece_on(from);
55
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) {
59     switch(str[4]) {
60     case 'n': case 'N': 
61       return make_promotion_move(from, to, KNIGHT);
62     case 'b': case 'B':
63       return make_promotion_move(from, to, BISHOP);
64     case 'r': case 'R':
65       return make_promotion_move(from, to, ROOK);
66     case 'q': case 'Q':
67       return make_promotion_move(from, to, QUEEN);
68     }
69   }
70
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
75     // than 1.
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;
82       Square s;
83       for(s = from + delta;
84           pawn_rank(us, s) == RANK_1 && pos.piece_on(s) != rook_of_color(us);
85           s += delta);
86       if(pawn_rank(us, s) == RANK_1 && pos.piece_on(s) == rook_of_color(us))
87         return make_castle_move(from, s);
88     }
89   }
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);
95   }
96
97   return make_move(from, to);
98 }
99
100
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
104 /// Chess960 mode.
105
106 const std::string move_to_string(Move move) {
107   std::string str;
108
109   if(move == MOVE_NONE)
110     str = "(none)";
111   else if(move == MOVE_NULL)
112     str = "0000";
113   else {
114     if(!Chess960) {
115       if(move_from(move) == SQ_E1 && move_is_short_castle(move)) {
116         str = "e1g1"; return str;
117       }
118       else if(move_from(move) == SQ_E1 && move_is_long_castle(move)) {
119         str = "e1c1"; return str;
120       }
121       if(move_from(move) == SQ_E8 && move_is_short_castle(move)) {
122         str = "e8g8"; return str;
123       }
124       else if(move_from(move) == SQ_E8 && move_is_long_castle(move)) {
125         str = "e8c8"; return str;
126       }
127     }
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);
131   }
132   return str;
133 }
134
135
136 /// Overload the << operator, to make it easier to print moves.
137
138 std::ostream &operator << (std::ostream &os, Move m) {
139   return os << move_to_string(m);
140 }
141
142
143 /// move_is_ok(), for debugging.
144
145 bool move_is_ok(Move m) {
146   return square_is_ok(move_from(m)) && square_is_ok(move_to(m));
147 }