Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
99 | pmbaty | 1 | /* |
2 | Texel - A UCI chess engine. |
||
3 | Copyright (C) 2012-2013 Peter Ă–sterlund, peterosterlund2@gmail.com |
||
4 | |||
5 | This program 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 | This program 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 | * textio.hpp |
||
21 | * |
||
22 | * Created on: Feb 25, 2012 |
||
23 | * Author: petero |
||
24 | */ |
||
25 | |||
26 | #ifndef TEXTIO_HPP_ |
||
27 | #define TEXTIO_HPP_ |
||
28 | |||
29 | #include <string> |
||
30 | #include <vector> |
||
31 | |||
32 | #include "chessParseError.hpp" |
||
33 | #include "position.hpp" |
||
34 | #include "util/util.hpp" |
||
35 | |||
36 | |||
37 | class MoveGen; |
||
38 | |||
39 | /** |
||
40 | * Conversion between text and binary formats. |
||
41 | */ |
||
42 | class TextIO { |
||
43 | public: |
||
44 | static const std::string startPosFEN; |
||
45 | |||
46 | /** Parse a FEN string and return a chess Position object. */ |
||
47 | static Position readFEN(const std::string& fen); |
||
48 | |||
49 | /** Remove pseudo-legal EP square if it is not legal, ie would leave king in check. */ |
||
50 | static void fixupEPSquare(Position& pos); |
||
51 | |||
52 | /** Return a FEN string corresponding to a chess Position object. */ |
||
53 | static std::string toFEN(const Position& pos); |
||
54 | |||
55 | /** |
||
56 | * Convert a chess move to human readable form. |
||
57 | * @param pos The chess position. |
||
58 | * @param move The executed move. |
||
59 | * @param longForm If true, use long notation, eg Ng1-f3. |
||
60 | * Otherwise, use short notation, eg Nf3 |
||
61 | */ |
||
62 | static std::string moveToString(const Position& pos, const Move& move, bool longForm); |
||
63 | |||
64 | /** Convert a move object to UCI string format. */ |
||
65 | static std::string moveToUCIString(const Move& m); |
||
66 | |||
67 | /** |
||
68 | * Convert a string to a Move object. |
||
69 | * @return A move object, or empty move if move has invalid syntax. |
||
70 | */ |
||
71 | static Move uciStringToMove(const std::string& move); |
||
72 | |||
73 | /** |
||
74 | * Convert a chess move string to a Move object. |
||
75 | * The string may specify any combination of piece/source/target/promotion |
||
76 | * information as long as it matches exactly one valid move. |
||
77 | */ |
||
78 | static Move stringToMove(Position& pos, const std::string& strMove); |
||
79 | |||
80 | /** |
||
81 | * Convert a string, such as "e4" to a square number. |
||
82 | * @return The square number, or -1 if not a legal square. |
||
83 | */ |
||
84 | static int getSquare(const std::string& s); |
||
85 | |||
86 | /** |
||
87 | * Convert a square number to a string, such as "e4". |
||
88 | */ |
||
89 | static std::string squareToString(int square); |
||
90 | |||
91 | /** |
||
92 | * Create an ascii representation of a position. |
||
93 | */ |
||
94 | static std::string asciiBoard(const Position& pos); |
||
95 | |||
96 | /** Create an ascii representation of a bitmask. */ |
||
97 | static std::string asciiBoard(U64 mask); |
||
98 | |||
99 | private: |
||
100 | static void safeSetPiece(Position& pos, int col, int row, int p); |
||
101 | |||
102 | static int charToPiece(bool white, char c); |
||
103 | }; |
||
104 | |||
105 | inline int |
||
106 | TextIO::getSquare(const std::string& s) |
||
107 | { |
||
108 | int x = s[0] - 'a'; |
||
109 | int y = s[1] - '1'; |
||
110 | if ((x < 0) || (x > 7) || (y < 0) || (y > 7)) |
||
111 | return -1; |
||
112 | |||
113 | return Position::getSquare(x, y); |
||
114 | } |
||
115 | |||
116 | inline std::string |
||
117 | TextIO::squareToString(int square) |
||
118 | { |
||
119 | std::string ret; |
||
120 | int x = Position::getX(square); |
||
121 | int y = Position::getY(square); |
||
122 | ret += (char)(((x + 'a'))); |
||
123 | ret += (char)(((y + '1'))); |
||
124 | return ret; |
||
125 | } |
||
126 | |||
127 | inline void |
||
128 | TextIO::safeSetPiece(Position& pos, int col, int row, int p) { |
||
129 | if (col > 7) throw ChessParseError("Too many columns"); |
||
130 | if ((p == Piece::WPAWN) || (p == Piece::BPAWN)) |
||
131 | if ((row == 0) || (row == 7)) |
||
132 | throw ChessParseError("Pawn on first/last rank"); |
||
133 | pos.setPiece(Position::getSquare(col, row), p); |
||
134 | } |
||
135 | |||
136 | inline int |
||
137 | TextIO::charToPiece(bool white, char c) { |
||
138 | switch (c) { |
||
139 | case 'Q': case 'q': return white ? Piece::WQUEEN : Piece::BQUEEN; |
||
140 | case 'R': case 'r': return white ? Piece::WROOK : Piece::BROOK; |
||
141 | case 'B': return white ? Piece::WBISHOP : Piece::BBISHOP; |
||
142 | case 'N': case 'n': return white ? Piece::WKNIGHT : Piece::BKNIGHT; |
||
143 | case 'K': case 'k': return white ? Piece::WKING : Piece::BKING; |
||
144 | case 'P': case 'p': return white ? Piece::WPAWN : Piece::BPAWN; |
||
145 | } |
||
146 | return -1; |
||
147 | } |
||
148 | |||
149 | inline |
||
150 | std::ostream& operator<<(std::ostream& os, const Move& m) { |
||
151 | os << TextIO::moveToUCIString(m); |
||
152 | return os; |
||
153 | } |
||
154 | |||
155 | #endif /* TEXTIO_HPP_ */ |