tartan 1.2.2.1
Chess API
Loading...
Searching...
No Matches
board.hpp
1#ifndef _TARTAN_HPP_
2#define _TARTAN_HPP_
3
4#include <array>
5#include <forward_list>
6#include <list>
7#include <ostream>
8#include <typeindex>
9#include <functional>
10#include <initializer_list>
11
13namespace tt {
14
15class Board;
16
23class Piece {
24public:
30 enum class Color {
31 Black = 0, White = 1,
32 };
36 class Position {
37 public:
45 enum class Mode {
46 Normal = 1,
47 Reverse = -1,
48 };
49 private:
50 short p_x = 1;
51 short p_y = 1;
52 Mode p_mode = Mode::Normal;
53 public:
54 Position() = default;
61 Position(int x, int y);
68 Position(char l, int d);
100 Position(const std::string& str);
104 Position(const char* str) : Position(std::string(str)) {};
105 public:
111 Position(const Position&);
117 Position& operator=(const Position&);
118 public:
123 int x() const { return p_x; };
128 int y() const { return p_y; };
135 char letter() const { return p_x + 'a' - 1; };
142 int digit() const { return p_y; };
150 int setX(int x);
158 int setY(int y);
165 char setLetter(char x) { return setX(x-'a'+1) + 'a'-1; };
171 int setDigit(short y) { return setY(y); };
191 Position offset(int dx, int dy) const;
204 Position offset(char dc, int dd) const;
214 bool atLeft() const;
224 bool atRight() const;
234 bool atTop() const;
244 bool atBottom() const;
251 Mode mode() const { return p_mode; };
262 Mode setMode (Mode m);
264 public:
269 std::string str() const;
276 Position operator+(const Position&) const;
283 Position operator-(const Position&) const;
295 Position operator()(int dx, int dy) const;
299 Position operator()(char dc, int dd) const;
306 friend bool operator==(const Position&, const Position&);
315 friend std::ostream& operator<<(
316 std::ostream&, const Position&);
317 };
325 class Turn {
326 public:
337 Turn(const Piece* tpiece,
338 const Position& to,
339 const Piece* cap = nullptr,
340 bool possible = true);
347 Position to() const { return t_to; };
353 Position from() const { return t_from; };
359 const Piece* piece() const { return t_piece; };
363 Piece* piece() { return t_piece; };
370 const Piece* capture() const { return t_capture; };
374 Piece* capture() { return t_capture; };
380 bool possible() const { return t_possible; };
387 bool setPossible(bool c) { return !(t_possible = c); };
398 virtual std::string str() const;
416 virtual void apply(int mode = 0);
424 virtual void undo();
436 virtual bool isEqual(const Turn& rhs) const;
445 friend bool operator==(const Turn& lhs, const Turn& rhs);
454 friend bool operator!=(const Turn&, const Turn&);
464 friend std::ostream& operator<<(std::ostream& os, const Turn& t);
471 virtual auto clone() const -> std::decay<decltype(*this)>::type*;
472 virtual ~Turn() = default;
473 protected:
484 };
489 class TurnMap : public std::list<Turn*> {
490 public:
491 using std::list<Turn*>::list;
493 TurnMap(const TurnMap&);
495 TurnMap(TurnMap&&);
497 TurnMap& operator=(const TurnMap& other);
499 TurnMap& operator=(TurnMap&& other);
506 bool possible() const;
507 ~TurnMap();
515 friend bool operator==(const TurnMap& lhs, const TurnMap& rhs);
523 friend bool operator!=(const TurnMap& lhs, const TurnMap& rhs);
524 };
525protected:
542 Board* p_board = nullptr;
555public:
561 Color color() const { return p_color; };
569 Color setColor(Color col);
576 const Position& position() const { return p_position; };
598 virtual Position move(const Position& to);
614 virtual TurnMap moveMap(int mode = 0) const = 0;
620 const Board* board() const { return p_board; };
624 Board* board() { return p_board; };
632 Board* setBoard(Board* b);
648public:
660 static TurnMap diagonalMoves(const Piece* p);
665 static TurnMap straightMoves(const Piece* p);
666public:
674 Piece(const Position& pos = {1,1}, Color col = Color::White);
675 virtual ~Piece() = default;
676};
677
690class Board : private BoardT {
691 friend class Piece::Turn;
692public:
707public:
709 Board();
715 Board(PieceSetT& set);
716
718 Board(Board&&) = default;
720 Board& operator=(Board&&) = default;
721 Board(const Board&) = delete;
722 Board& operator=(const Board&) = delete;
723public:
730 return b_currentTurnColor;
731 };
739 virtual Piece::Color setCurrentTurn(Piece::Color col);
754 virtual Piece::TurnMap possibleMoves(const Piece* p) const;
765 Piece::TurnMap possibleMoves(const Piece::Position& pos) const;
778 virtual const Piece::Turn* makeTurn(const Piece::Position& from,
779 const Piece::Position& to) = 0;
780protected:
791 virtual Piece* placePiece(Piece* p);
813 Piece::TurnMap produceTurn(const Piece::Position& from,
814 const Piece::Position& to, Piece::Turn** turn);
825 virtual const Piece::Turn* applyTurn(Piece::Turn* turn);
826public:
831 const BoardT& board() const { return static_cast<const BoardT&>(*this); };
835 BoardT& board() { return static_cast<BoardT&>(*this); };
842 HistoryT& history() { return b_history; };
846 const HistoryT& history() const { return b_history; };
853 std::size_t movesMade() const { return b_history.size(); }
864 std::size_t turnIndex() const { return b_turnIndex; };
865
877 virtual Piece* canInsert(Piece* p) const;
891 virtual Piece* insertPiece(Piece* p);
898 virtual void clear();
914
919 void fill();
926 void fill(PieceSetT& set);
937 void fill(const std::string& str);
950 template<class Iterator>
951 void fill(Iterator begin, Iterator end);
960 void refill() { clear(); fill(); };
962
970 virtual PieceSetT defaultPieceSet() const = 0;
971
979 virtual bool isEqual(const Board& other) const = 0;
986 friend bool operator==(const Board& lhs, const Board& rhs);
993 friend bool operator!=(const Board&, const Board&);
1002 Piece*& operator[](const Piece::Position& pos);
1006 const Piece* operator[](const Piece::Position& pos) const;
1012 Piece*& at(const Piece::Position& pos);
1016 const Piece* at(const Piece::Position& pos) const;
1017 using BoardT::iterator;
1018 using BoardT::const_iterator;
1019 using BoardT::begin;
1020 using BoardT::end;
1027 void setPieceGetter(PieceGetterT g) { b_pieceGetter = g; };
1034 PieceGetterT& pieceGetter() { return b_pieceGetter; };
1038 const PieceGetterT& pieceGetter() const { return b_pieceGetter; };
1052 PieceTypesRetT getPieceType(PieceTypesArgT possible);
1061 virtual std::string str() const = 0;
1072 friend std::ostream& operator<<(std::ostream& os, const Board& b);
1073 ~Board();
1074public:
1085 PieceSetT set(const std::string& str) const;
1095 template<class Iterator>
1096 PieceSetT set(Iterator begin, Iterator end) const;
1112 virtual Piece* piece(const std::string& spec) const = 0;
1113private:
1114 void fillBoardWithNullptrs();
1115protected:
1120 Piece::Color b_currentTurnColor = Piece::Color::White;
1156 std::size_t b_turnIndex = 0;
1157};
1158
1159}
1160
1161#endif // !_TARTAN_HPP_
8x8 game board
Definition board.hpp:690
HistoryT & history()
Turn history.
Definition board.hpp:842
void setPieceGetter(PieceGetterT g)
Set the Piece getter functor.
Definition board.hpp:1027
virtual std::string str() const =0
String representation of Board.
virtual PieceSetT defaultPieceSet() const =0
Default piece set for the Board.
Piece::Color currentTurn() const
Current turn color.
Definition board.hpp:729
Board & operator=(Board &&)=default
Move assignment operator.
HistoryT b_history
Board history of applied Turn objects.
Definition board.hpp:1137
const BoardT & board() const
Internal board data structure.
Definition board.hpp:831
PieceGetterT b_pieceGetter
Piece getter variable.
Definition board.hpp:1148
Board(Board &&)=default
Move constructor.
std::size_t turnIndex() const
Turn index.
Definition board.hpp:864
virtual bool isEqual(const Board &other) const =0
Compare 2 Board objects.
std::size_t movesMade() const
Total count of moves made on Board.
Definition board.hpp:853
const PieceGetterT & pieceGetter() const
Set the Piece getter functor.
Definition board.hpp:1038
CapturedT b_capturedPieces
List of captured pieces.
Definition board.hpp:1128
BoardT & board()
Internal board data structure.
Definition board.hpp:835
virtual Piece * piece(const std::string &spec) const =0
Convert std::string Piece spec to actual Piece object pointer.
const HistoryT & history() const
Turn history.
Definition board.hpp:846
virtual const Piece::Turn * makeTurn(const Piece::Position &from, const Piece::Position &to)=0
Make the turn.
PieceGetterT & pieceGetter()
Get current piece getter functor.
Definition board.hpp:1034
void refill()
Clear and fill Board with default piece set.
Definition board.hpp:960
Piece position at the Board.
Definition board.hpp:36
char setLetter(char x)
Set x coordinate with string representation.
Definition board.hpp:165
std::string str() const
String position representation.
Definition position.cpp:115
Position()=default
Create Position object at a1.
Position operator-(const Position &) const
Substraction operator.
Definition position.cpp:132
Mode mode() const
Current mode.
Definition board.hpp:251
bool atLeft() const
Check if the position are at left border.
Definition position.cpp:76
bool atRight() const
Check if the position are at right border.
Definition position.cpp:83
Position offset(int dx, int dy) const
Offset current position.
Definition position.cpp:65
Position(const char *str)
Creates Position object at str position.
Definition board.hpp:104
Position operator-=(const Position &)
cosnt
Definition position.cpp:140
int x() const
x coordinate
Definition board.hpp:123
int setX(int x)
Set x coordinate.
Definition position.cpp:39
Position operator()(int dx, int dy) const
Offset current position.
Definition position.cpp:144
int y() const
y coordinate
Definition board.hpp:128
friend std::ostream & operator<<(std::ostream &, const Position &)
std::ostream output operator
Definition position.cpp:156
friend bool operator==(const Position &, const Position &)
Comparison operator.
Definition position.cpp:152
Position & operator=(const Position &)
Copy assignment operator.
Definition position.cpp:122
Mode
Boolean functions operation mode.
Definition board.hpp:45
@ Reverse
Place origin at the h8.
@ Normal
Place origin at the a1.
int setDigit(short y)
Set y coordinate.
Definition board.hpp:171
bool atBottom() const
Check if the position are at bottom border.
Definition position.cpp:97
int digit() const
Digit representation of y coordinate.
Definition board.hpp:142
int setY(int y)
Set y coordinate.
Definition position.cpp:52
Mode setMode(Mode m)
Set offset mode.
Definition position.cpp:104
Position operator+(const Position &) const
Addition operator.
Definition position.cpp:128
char letter() const
Letter representation of x coordinate.
Definition board.hpp:135
bool atTop() const
Check if the position are at top border.
Definition position.cpp:90
Position operator+=(const Position &)
cosnt
Definition position.cpp:136
List of Turn objects.
Definition board.hpp:489
Generic Piece Turn on Board.
Definition board.hpp:325
const Piece * piece() const
Turn Piece.
Definition board.hpp:359
friend bool operator!=(const Turn &, const Turn &)
Inverted comparison operator.
Definition turn.cpp:63
Position from() const
Position the turn is started at.
Definition board.hpp:353
bool possible() const
Turn viability.
Definition board.hpp:380
friend std::ostream & operator<<(std::ostream &os, const Turn &t)
std::ostream output
Definition turn.cpp:68
virtual std::string str() const
String Turn representation.
Definition turn.cpp:47
Position t_to
Turn piece finishing position
Definition board.hpp:477
Piece * t_capture
Piece that is captured as the resul of turn.
Definition board.hpp:481
bool t_possible
Turn viability.
Definition board.hpp:483
Piece * piece()
Turn Piece.
Definition board.hpp:363
Piece * capture()
Captured piece.
Definition board.hpp:374
Position to() const
Position where piece will end up after turn.
Definition board.hpp:347
virtual void undo()
Undo turn.
Definition turn.cpp:28
Piece * t_piece
Turn Piece inself
Definition board.hpp:479
const Piece * capture() const
Captured piece.
Definition board.hpp:370
bool setPossible(bool c)
Set the turn viability.
Definition board.hpp:387
friend bool operator==(const Turn &lhs, const Turn &rhs)
Comparison operator.
Definition turn.cpp:59
virtual void apply(int mode=0)
Apply turn.
Definition turn.cpp:18
Position t_from
Turn piece starting position
Definition board.hpp:475
virtual auto clone() const -> std::decay< decltype(*this)>::type *
Clone current Turn.
Definition turn.cpp:72
virtual bool isEqual(const Turn &rhs) const
Compare turns.
Definition turn.cpp:39
Generic board memeber API.
Definition board.hpp:23
std::size_t movesMade() const
Moves count.
Definition board.hpp:639
Position & position()
Piece Position.
Definition board.hpp:580
Board * setBoard(Board *b)
Set Piece Board.
Definition piece.cpp:11
Board * p_board
Piece Board.
Definition board.hpp:542
const Board * board() const
Piece board.
Definition board.hpp:620
static TurnMap straightMoves(const Piece *p)
Construct straight moves (up, down, left, right) TurnMap.
Definition piece.cpp:79
Color p_color
Piece color.
Definition board.hpp:530
virtual Position move(const Position &to)
Move piece.
Definition piece.cpp:17
Position setPosition(const Position &)
Set Piece position.
Definition piece.cpp:30
virtual TurnMap moveMap(int mode=0) const =0
Generic moves.
Color setColor(Color col)
Set the color.
Definition piece.cpp:36
Position p_position
Piece Position.
Definition board.hpp:535
Color color() const
Piece color.
Definition board.hpp:561
std::size_t p_movesMade
Turns count Count of turns Piece have made.
Definition board.hpp:547
std::size_t turnIndex() const
Current turn index.
Definition board.hpp:647
Color
Piece color.
Definition board.hpp:30
const Position & position() const
Piece Position.
Definition board.hpp:576
std::size_t p_turnIndex
Last turn index.
Definition board.hpp:554
Board * board()
Piece board.
Definition board.hpp:624
static TurnMap diagonalMoves(const Piece *p)
Construct diagonal moves TurnMap.
Definition piece.cpp:42
STL namespace.
Tartan library namespace.
Definition board.cpp:10
std::ostream & operator<<(std::ostream &os, const Board &cb)
Definition board.cpp:235
bool operator==(const Board &lhs, const Board &rhs)
Definition board.cpp:243