tartan  1.2.1.0
exceptions.hpp
1 #ifndef _TARTAN_BOARD_EXCEPTIONS_HPP_
2 #define _TARTAN_BOARD_EXCEPTIONS_HPP_
3 
4 #include <tartan/board.hpp>
5 #include <stdexcept>
6 #include <string>
7 
9 namespace tt::ex {
10 
18 class tartan : public std::logic_error {
19 public:
20  using logic_error::logic_error;
21  virtual ~tartan() = default;
22 };
23 
30 class bad_set : public tartan {
31 public:
32  bad_set(
33  const std::string& what_arg = "Can not produce piece set"
34  ) : tartan(what_arg) {};
35 };
36 
38 class bad_piece : public tartan {
39 public:
45  const Piece* p,
46  const std::string& what_arg = "Invalid piece"
47  ) : tartan(what_arg) { e_piece = p; }
53  const Piece* piece() const { return e_piece; };
54 private:
55  const Piece* e_piece;
56 };
57 
62 class null_piece : public bad_piece {
63 public:
68  const std::string what_arg = "Piece does not exists"
69  ) : bad_piece(nullptr, what_arg) {};
70  const Piece* piece() const = delete;
71 };
72 
77 class foreign_piece : public bad_piece {
78 public:
86  const Piece* p,
87  const Board* b,
88  const std::string& what_arg = "Piece does not belong to this board"
89  ) : bad_piece(p, what_arg) { e_board = b; };
96  const Board* board() const { return e_board; };
97 private:
98  const Board* e_board;
99 };
100 
105 class bad_piece_spec : public bad_piece {
106 public:
112  const std::string& spec,
113  const std::string what_arg = "Invalid piece specification"
114  ) : bad_piece(nullptr, what_arg) { e_spec = spec; };
120  const std::string& spec() const { return e_spec; };
121 private:
122  std::string e_spec;
123 };
124 
129 class position_is_taken : public bad_piece {
130 public:
132  const Piece* p,
133  const std::string& what_arg = "Piece position is taken"
134  ) : bad_piece(p, what_arg) {};
135 };
136 
141 class illegal_move : public tartan {
142 public:
150  const std::string& what_arg = "Illegal move") :
151  tartan(what_arg), e_piece(p), e_to(to) {
152  };
159  const Piece* piece() const { return e_piece; };
166  const Piece::Position& to() const { return e_to; };
167  virtual ~illegal_move() = default;
168 private:
169  const Piece* e_piece;
170  const Piece::Position& e_to;
171 };
172 
177 class tile_is_empty : public illegal_move {
178 public:
185  const Piece::Position& from,
186  const Piece::Position& to,
187  const std::string& what_arg = "Selected tile is empty")
188  : illegal_move(nullptr, to, what_arg), e_from(from) {};
189  const Piece* piece() = delete;
195  const Piece::Position& from() const { return e_from; };
196 private:
197  const Piece::Position& e_from;
198 };
199 
205 public:
212  const Piece* p,
213  const Piece::Position& to,
214  const std::string& what_arg = "Moved piece is in wrong color")
215  : illegal_move(p, to, what_arg) {};
216 };
217 
222 class can_not_move : public illegal_move {
223 public:
230  const Piece* p,
231  const Piece::Position& to,
232  const std::string& what_arg = "Selected piece can't move")
233  : illegal_move(p, to, what_arg) {};
234 };
235 
240 class no_such_move : public illegal_move {
241 public:
249  const Piece* p,
250  const Piece::Position& to,
251  const std::string& what_arg = "Selected piece can't perform such move")
252  : illegal_move(p, to, what_arg) {};
253 };
254 
259 class illegal_turn : public tartan {
260 public:
264  illegal_turn(const std::string& what_arg) :
265  tartan(what_arg) {};
266  illegal_turn(const char* what_arg) :
267  tartan(what_arg) {};
268 };
269 
274 class bad_piece_type : public tartan {
275 public:
276  bad_piece_type(const std::string what_arg = "Piece type is illegal") :
277  tartan(what_arg) {};
278 };
279 
280 }
281 
282 #endif // !_TARTAN_BOARD_EXCEPTIONS_HPP_
8x8 game board
Definition: board.hpp:692
Piece position at the Board.
Definition: board.hpp:37
Generic board memeber piece API.
Definition: board.hpp:24
Thrown when Board::piece() can not recognize Piece specification.
Definition: exceptions.hpp:105
const std::string & spec() const
Get the malformed spec string.
Definition: exceptions.hpp:120
bad_piece_spec(const std::string &spec, const std::string what_arg="Invalid piece specification")
Definition: exceptions.hpp:111
Thrown when tt::Board::getPieceType() returns std::type_info that is not mentioned in its argument.
Definition: exceptions.hpp:274
Base class for exceptions about pieces.
Definition: exceptions.hpp:38
const Piece * piece() const
Get the reported tt::Piece object.
Definition: exceptions.hpp:53
bad_piece(const Piece *p, const std::string &what_arg="Invalid piece")
Definition: exceptions.hpp:44
Exception when Board::set() functions fail.
Definition: exceptions.hpp:30
Thrown when Board::makeMove() is performed on Piece that can not make any moves.
Definition: exceptions.hpp:222
can_not_move(const Piece *p, const Piece::Position &to, const std::string &what_arg="Selected piece can't move")
Definition: exceptions.hpp:229
Thrown when piece does not belong to Board object in which it is being processed.
Definition: exceptions.hpp:77
foreign_piece(const Piece *p, const Board *b, const std::string &what_arg="Piece does not belong to this board")
Definition: exceptions.hpp:85
const Board * board() const
Get the Board object.
Definition: exceptions.hpp:96
Base class for Board::makeMove() function errors.
Definition: exceptions.hpp:141
const Piece::Position & to() const
Get move target location.
Definition: exceptions.hpp:166
const Piece * piece() const
Get Piece object.
Definition: exceptions.hpp:159
illegal_move(const Piece *p, const Piece::Position &to, const std::string &what_arg="Illegal move")
Definition: exceptions.hpp:149
Thrown when constructed Piece::Turn object is malformed.
Definition: exceptions.hpp:259
illegal_turn(const std::string &what_arg)
Definition: exceptions.hpp:264
Thrown when piece moved with Board::makeMove() can not have such move.
Definition: exceptions.hpp:240
no_such_move(const Piece *p, const Piece::Position &to, const std::string &what_arg="Selected piece can't perform such move")
Definition: exceptions.hpp:248
Thrown when Piece pointer is nullptr when it should not be.
Definition: exceptions.hpp:62
null_piece(const std::string what_arg="Piece does not exists")
Definition: exceptions.hpp:67
Thrown when trying to Board::makeMove() on Piece that has wrong Piece::Color.
Definition: exceptions.hpp:204
piece_in_wrong_color(const Piece *p, const Piece::Position &to, const std::string &what_arg="Moved piece is in wrong color")
Definition: exceptions.hpp:211
Thrown when Piece Position on Board is occupied when it should not to.
Definition: exceptions.hpp:129
Base class for all tartan exceptions.
Definition: exceptions.hpp:18
Thrown when selected Piece location is empty of the Board.
Definition: exceptions.hpp:177
const Piece::Position & from() const
Get empli tile Position.
Definition: exceptions.hpp:195
tile_is_empty(const Piece::Position &from, const Piece::Position &to, const std::string &what_arg="Selected tile is empty")
Definition: exceptions.hpp:184
Tartan tt::Board exceptions set.
Definition: exceptions.hpp:9