UCILoader 1.1.2
Small C++ library that allows user to connect to a chess engines via UCI protocol.
Loading...
Searching...
No Matches
StandardChess.h
1#pragma once
2#include "Parser.h"
3#include "EngineConnection.h"
4#include <cstring> // for memcpy
5
25namespace StandardChess {
26
30 enum PromotionTarget {
31 None,
32 Rook,
33 Bishop,
34 Knight,
35 Queen
36 };
37
63 unsigned char from;
64 unsigned char to;
65 PromotionTarget promoteTo;
66
73 bool operator == (const StandardChessMove& other) const {
74 return this->from == other.from &&
75 this->to == other.to &&
76 this->promoteTo == other.promoteTo;
77 }
78
86 this->from = other.from;
87 this->to = other.to;
88 this->promoteTo = other.promoteTo;
89 return *this;
90 };
91 };
92
104 static unsigned char parseCoords(char column, char file) {
105 return 8 * ('8' - file) + column - 'a';
106 }
107
114 static StandardChess::PromotionTarget parsePromotion(char c){
115 switch (c)
116 {
117 case 'q' : return StandardChess::Queen;
118 case 'r' : return StandardChess::Rook;
119 case 'b' : return StandardChess::Bishop;
120 case 'n' : return StandardChess::Knight;
121 default: return StandardChess::None;
122 }
123 };
124
125
140 static StandardChessMove createMove(const char from[], const char to[], char promoteTo = '\0') {
141 StandardChessMove result;
142 result.from = parseCoords(from[0], from[1]);
143 result.to = parseCoords(to[0], to[1]);
144 result.promoteTo = parsePromotion(promoteTo);
145 return result;
146 }
147
162 class StandardChessMoveMarschaler : public UCILoader::Marschaler<StandardChessMove> {
163
164
165 public:
166
173 void loadInto(const std::string& token, StandardChessMove& target) const override {
174 target.from = parseCoords(token[0], token[1]);
175 target.to = parseCoords(token[2], token[3]);
176
177 if(token.size() >= 5 )
178 target.promoteTo = parsePromotion(token[4]);
179 else
180 target.promoteTo = StandardChess::None;
181 }
182
183 };
184
197 static StandardChessMove moveValueOf(const char c_style_string[]) {
198 static StandardChessMoveMarschaler marshaler;
199 return marshaler.marshal(c_style_string);
200 }
201
217 static std::string stringValueOf(const StandardChessMove& m) {
218 std::string result = "a1a1";
219
220 result[0] += m.from%8;
221 result[1] += 7 - m.from/8;
222 result[2] += m.to % 8;
223 result[3] += 7 - m.to/8;
224
225 if (m.promoteTo != StandardChess::None) {
226 result += "qrbn"[m.promoteTo];
227 }
228
229 return result;
230 }
231
232
251 public:
252
258 std::string toFen() const override { return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; };
264 std::string toPositionString() const override { return "startpos"; };
265 };
266
286 std::string fen;
287 public:
293 FenPos(const std::string& fen) : fen(fen) {};
299 std::string toFen() const override{ return fen; };
305 std::string toPositionString() const override{ return "fen " + fen; };
306 };
307
328 extern std::shared_ptr<UCILoader::EngineInstanceBuilder<StandardChessMove>> ChessEngineInstanceBuilder;
329}
Position formatter for arbitrary FEN positions.
Definition: StandardChess.h:285
std::string toFen() const override
Get the FEN string.
Definition: StandardChess.h:299
FenPos(const std::string &fen)
Constructor that stores the FEN string.
Definition: StandardChess.h:293
std::string toPositionString() const override
Get the UCI position string.
Definition: StandardChess.h:305
Move marshaler for parsing UCI move strings into StandardChessMove objects.
Definition: StandardChess.h:162
void loadInto(const std::string &token, StandardChessMove &target) const override
Parse a UCI move string into a StandardChessMove.
Definition: StandardChess.h:173
Position formatter for the standard chess starting position.
Definition: StandardChess.h:250
std::string toFen() const override
Get the FEN string for the starting position.
Definition: StandardChess.h:258
std::string toPositionString() const override
Get the UCI position string for the starting position.
Definition: StandardChess.h:264
Definition: Parser.h:34
Definition: UCI.h:363
Standard chess move representation.
Definition: StandardChess.h:62
PromotionTarget promoteTo
Promotion target (None for non-promotions)
Definition: StandardChess.h:65
StandardChessMove & operator=(const StandardChessMove &other)
Assignment operator.
Definition: StandardChess.h:85
unsigned char from
Source square (0-63)
Definition: StandardChess.h:63
bool operator==(const StandardChessMove &other) const
Equality operator for unit testing.
Definition: StandardChess.h:73
unsigned char to
Destination square (0-63)
Definition: StandardChess.h:64