tartan 1.2.2.1
Chess API
Loading...
Searching...
No Matches
chess.hpp
1#ifndef _TARTAN_CHESS_HPP_
2#define _TARTAN_CHESS_HPP_
3
4#include <tartan/board.hpp>
5
7namespace tt::chess {
8
9class King;
10
22class Chessboard : public Board {
23public:
24 using Board::Board;
40 virtual const Piece::Turn* makeTurn(const Piece::Position& from,
41 const Piece::Position& to) override;
70 virtual Piece* piece(const std::string& spec) const override;
82 virtual Piece* canInsert(Piece* p) const override;
96 virtual Piece* insertPiece(Piece* p) override;
105 virtual void clear() override;
109 virtual bool isEqual(const Board&) const override;
117 virtual std::string str() const override;
124 virtual Piece::Color setCurrentTurn(Piece::Color) override;
131 virtual Piece::TurnMap possibleMoves(const Piece*) const override;
133
140 const King* whiteKing() const { return c_whiteKing; };
144 King* whiteKing() { return c_whiteKing; };
151 const King* blackKing() const { return c_blackKing; };
155 King* blackKing() { return c_blackKing; };
165 const King* currentKing() const { return c_currentKing; };
173 const King* currentEnemyKing() const { return c_currentEnemyKing; };
219 PieceSetT defaultPieceSet() const override;
226 enum Mode : int {
242 };
243protected:
251 void markChecks(Piece::TurnMap& map) const;
252protected:
258 King* c_whiteKing = nullptr;
264 King* c_blackKing = nullptr;
272 King* c_currentKing = nullptr;
283};
284
286class Pawn : public Piece {
287public:
288 using Piece::Piece;
296 virtual TurnMap moveMap(int mode = 0) const override;
302 class Turn : public Piece::Turn {
303 public:
304 using Piece::Turn::Turn;
305 private:
306 std::type_index t_promoteTo = typeid(nullptr);
307 public:
308 bool isEqual(const Piece::Turn &) const override;
309 virtual void apply(int mode = 0) override;
318 const std::type_index& promoteTo() const { return t_promoteTo; };
319 };
320};
321
323class Knight : public Piece {
324public:
325 using Piece::Piece;
326 virtual TurnMap moveMap(int mode = 0) const override;
334 class Turn : public Piece::Turn {
335 public:
336 using Piece::Turn::Turn;
337 public:
338 bool isEqual(const Piece::Turn &) const override;
339 };
340};
341
343class Bishop : public Piece {
344public:
345 using Piece::Piece;
346 virtual TurnMap moveMap(int mode = 0) const override;
352 class Turn : public Piece::Turn {
353 public:
354 using Piece::Turn::Turn;
355 public:
356 bool isEqual(const Piece::Turn &) const override;
357 };
358};
359
361class Rook : public Piece {
362public:
363 using Piece::Piece;
364 virtual TurnMap moveMap(int mode = 0) const override;
370 class Turn : public Piece::Turn {
371 public:
372 using Piece::Turn::Turn;
373 public:
374 bool isEqual(const Piece::Turn &) const override;
375 };
376};
377
379class Queen : public Piece {
380public:
381 using Piece::Piece;
382 virtual TurnMap moveMap(int mode = 0) const override;
388 class Turn : public Piece::Turn {
389 public:
390 using Piece::Turn::Turn;
391 public:
392 bool isEqual(const Piece::Turn &) const override;
393 };
394};
395
397class King : public Piece {
398public:
399 using Piece::Piece;
400 virtual TurnMap moveMap(int mode = 0) const override;
406 class Turn : public Piece::Turn {
407 public:
421 const Piece* f,
422 const Position& t,
423 const Piece* c = nullptr,
424 Rook::Turn* castling = nullptr,
425 bool p = true
426 ) : Piece::Turn(f, t, c, p) {
427 k_castlingTurn = castling;
428 };
429 public:
430 bool isEqual(const Piece::Turn &) const override;
431 virtual void apply(int mode = 0) override;
432 virtual void undo() override;
433 virtual std::string str() const override;
434 virtual ~Turn() override {
435 delete k_castlingTurn;
436 }
437 private:
438 Rook::Turn* k_castlingTurn;
439 };
446 bool check() const;
453 bool checkmate() const;
461 bool castled() const { return k_castled; };
462private:
463 bool calculateCheck() const;
464 bool calculateCheckmate() const;
465 bool k_castled = false;
466 mutable bool k_check = false;
467 mutable bool k_checkmate = false;
468 mutable std::size_t k_checkTurnIndex = -1;
469 mutable std::size_t k_checkmateTurnIndex = -1;
470};
471
472}
473
474#endif // !_TARTAN_CHESS_HPP_
8x8 game board
Definition board.hpp:690
Board()
Construct empty Board object.
Definition board.cpp:16
std::list< Piece * > PieceSetT
Type for representing set of pointers to Piece objects.
Definition board.hpp:700
virtual Piece::TurnMap possibleMoves(const Piece *p) const
Possible moves of a Piece by it's adress.
Definition board.cpp:71
Piece position at the Board.
Definition board.hpp:36
List of Turn objects.
Definition board.hpp:489
Generic Piece Turn on Board.
Definition board.hpp:325
Turn(const Piece *tpiece, const Position &to, const Piece *cap=nullptr, bool possible=true)
Construct new Turn object.
Definition turn.cpp:8
Generic board memeber API.
Definition board.hpp:23
Piece(const Position &pos={1, 1}, Color col=Color::White)
Construct new Piece.
Definition piece.cpp:6
Color
Piece color.
Definition board.hpp:30
Bishop Turn.
Definition chess.hpp:352
bool isEqual(const Piece::Turn &) const override
Compare turns.
Bishop chess Piece.
Definition chess.hpp:343
virtual TurnMap moveMap(int mode=0) const override
Generic moves.
Definition bishop.cpp:7
Chess game board.
Definition chess.hpp:22
King * currentKing()
King that has the color of currentTurn()
Definition chess.hpp:161
const King * whiteKing() const
Current White King.
Definition chess.hpp:140
King * blackKing()
Current Black King.
Definition chess.hpp:155
King * c_currentEnemyKing
Current enemy King object.
Definition chess.hpp:282
virtual std::string str() const override
String represeantation of current Chessboard.
Definition chess.cpp:192
PieceSetT defaultPieceSet() const override
Get default chessboard Piece set.
Definition chess.cpp:160
virtual Piece * piece(const std::string &spec) const override
Convert std::string Piece spec to actual Piece object pointer.
Definition chess.cpp:13
Mode
Modes to use with Turn::apply and Piece::moveMap.
Definition chess.hpp:226
@ CheckingMode
Apply the turn or produce moveMap as performing the validation for King check.
Definition chess.hpp:241
@ DefaultMode
Default mode. Act as usual.
Definition chess.hpp:227
virtual Piece * canInsert(Piece *p) const override
Throws an exception if Piece cannot be placed at the Board.
Definition chess.cpp:74
King * currentEnemyKing()
King that has the color opposite to currentTurn()
Definition chess.hpp:177
const King * blackKing() const
Current Black King.
Definition chess.hpp:151
virtual Piece::Color setCurrentTurn(Piece::Color) override
Set current turn Color.
Definition chess.cpp:138
King * c_currentKing
Current King object.
Definition chess.hpp:272
const King * currentKing() const
King that has the color of currentTurn()
Definition chess.hpp:165
King * c_whiteKing
White King object.
Definition chess.hpp:258
virtual void clear() override
Clear current Chessboard.
Definition chess.cpp:152
void markChecks(Piece::TurnMap &map) const
Marks Tunr objects that will put current King in check.
Definition chess.cpp:127
virtual Piece * insertPiece(Piece *p) override
Place piece on Board.
Definition chess.cpp:91
virtual bool isEqual(const Board &) const override
Compare 2 Board objects.
Definition chess.cpp:231
virtual const Piece::Turn * makeTurn(const Piece::Position &from, const Piece::Position &to) override
Make turn on Chessboard.
Definition chess.cpp:114
King * c_blackKing
Black King object.
Definition chess.hpp:264
King * whiteKing()
Current White King.
Definition chess.hpp:144
virtual Piece::TurnMap possibleMoves(const Piece *) const override
Possible moves of a Piece by it's adress.
Definition chess.cpp:108
const King * currentEnemyKing() const
King that has the color opposite to currentTurn()
Definition chess.hpp:173
King Turn.
Definition chess.hpp:406
virtual std::string str() const override
String Turn representation.
virtual void undo() override
Undo turn.
bool isEqual(const Piece::Turn &) const override
Compare turns.
Turn(const Piece *f, const Position &t, const Piece *c=nullptr, Rook::Turn *castling=nullptr, bool p=true)
Make new King Turn object.
Definition chess.hpp:420
virtual void apply(int mode=0) override
Apply turn.
King chess Piece.
Definition chess.hpp:397
bool castled() const
Has the King object already performed the castling Turn.
Definition chess.hpp:461
bool checkmate() const
Check if a King is under checkmate.
Definition king.cpp:109
bool check() const
Check if a King is under check.
Definition king.cpp:74
virtual TurnMap moveMap(int mode=0) const override
Generic moves.
Definition king.cpp:9
Knight Turn.
Definition chess.hpp:334
bool isEqual(const Piece::Turn &) const override
Compare turns.
Knight chess Piece.
Definition chess.hpp:323
virtual TurnMap moveMap(int mode=0) const override
Generic moves.
Definition knight.cpp:7
Pawn Turn.
Definition chess.hpp:302
bool isEqual(const Piece::Turn &) const override
Compare turns.
virtual void apply(int mode=0) override
Apply turn.
const std::type_index & promoteTo() const
Get prometion Piece type.
Definition chess.hpp:318
Pawn chess Piece.
Definition chess.hpp:286
virtual TurnMap moveMap(int mode=0) const override
Moves of a Pawn object.
Definition pawn.cpp:7
Queen Turn.
Definition chess.hpp:388
bool isEqual(const Piece::Turn &) const override
Compare turns.
Queen chess Piece.
Definition chess.hpp:379
virtual TurnMap moveMap(int mode=0) const override
Generic moves.
Definition queen.cpp:7
Rook Turn.
Definition chess.hpp:370
bool isEqual(const Piece::Turn &) const override
Compare turns.
Rook chess Piece.
Definition chess.hpp:361
virtual TurnMap moveMap(int mode=0) const override
Generic moves.
Definition rook.cpp:7
Chess game namespace.
Definition chess.cpp:7