Move.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file Move.h This file contains the definition of class Move.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef MOVE_H
25 #define MOVE_H
26 
27 #ifndef USE_PCH
28 #endif
29 
30 #include "Index.h"
31 #include "Type.h"
32 
33 namespace cwchess {
34 
35 /** @brief A chess move in a particular chess position.
36 *
37  * This class represents a move as can be done in a particular chess position.
38  * That means that it cannot be used to do the same move in another position
39  * where that move is possible too. The reason for that is that it's internal
40  * representation only stores the squares, and not piece information.
41 *
42  * A Move is therefore always used in combination with a ChessPosition.
43 * /
44 class Move {
45  private:
46  Index M_from; //!< Where the piece comes from.
47  Index M_to; //!< Where the piece moves to.
48  Type M_promotion_type; //!< Set to 'nothing' if not a promotion.
49 
50  public:
51  /** @name Constructors* /
52  //@{
53 
54  //! Construct an uninitialized Move.
55  Move(void) { }
56 
57  /** @brief Construct a Move from square \a from to square \a to.
58  *
59  * If this move represents a pawn promotion then \a promotion
60  * must be one of queen, rook, knight, bishop. Otherwise it
61  * must be empty.
62  * /
63  Move(Index from, Index to, Type promotion) : M_from(from), M_to(to), M_promotion_type(promotion) { }
64 
65  //! Copy-constructor.
66  Move(Move const& move) : M_from(move.M_from), M_to(move.M_to), M_promotion_type(move.M_promotion_type) { }
67 
68  //@}
69 
70  /** @name Assignment operator* /
71  //@{
72 
73  Move& operator=(Move const& move) { M_from = move.M_from; M_to = move.M_to; M_promotion_type = move.M_promotion_type; return* this; }
74 
75  //@}
76 
77  /** @name Comparision operators* /
78  //@{
79 
80  bool operator==(Move const& move) const
81  {
82  if (move.M_to == index_end) return M_to == index_end;
83  return M_from == move.M_from && M_to == move.M_to && M_promotion_type == move.M_promotion_type;
84  }
85 
86  bool operator!=(Move const& move) const
87  {
88  if (move.M_to == index_end) return M_to != index_end;
89  return M_to != move.M_to || M_from != move.M_from || M_promotion_type != move.M_promotion_type;
90  }
91 
92  //@}
93 
94  /** @name Accessors* /
95  //@{
96 
97  //! Return TRUE if this move is a pawn promotion.
98  bool is_promotion(void) const { return M_promotion_type != nothing; }
99 
100  //! Return the square the piece moves from.
101  Index from(void) const { return M_from; }
102 
103  //! Return the square the piece moves to.
104  Index to(void) const { return M_to; }
105 
106  //! Return the promotion type. Returns empty if this isn't a promotion.
107  Type promotion_type(void) const { return M_promotion_type; }
108 
109  //@}
110 
111  /** @name Manipulators* /
112  //@{
113 
114  //! Set a different promotion type.
115  void set_promotion(Type promotion) { M_promotion_type = promotion; }
116 
117  //! Set different target square.
118  void set_to(Index to) { M_to = to; }
119 
120  //! Set from, to and promotion type.
121  void set_move(Index from, Index to, Type promotion) { M_from = from; M_to = to; M_promotion_type = promotion; }
122 
123  //@}
124 };
125 
126 } // namespace cwchess
127 
128 #endif // MOVE_H
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
This file contains the declaration of class Type.
This file contains the declaration of class Index.
A chess move in a particular chess position.
Definition: Move.h:44
Move(void)
Construct an uninitialized Move.
Definition: Move.h:55
void set_to(Index to)
Set different target square.
Definition: Move.h:118
bool is_promotion(void) const
Return TRUE if this move is a pawn promotion.
Definition: Move.h:98
A chess piece type.
Definition: Type.h:87
The index of a chess square.
Definition: Index.h:211
void set_move(Index from, Index to, Type promotion)
Set from, to and promotion type.
Definition: Move.h:121
Move(Move const& move)
Copy-constructor.
Definition: Move.h:66
Index from(void) const
Return the square the piece moves from.
Definition: Move.h:101
void set_promotion(Type promotion)
Set a different promotion type.
Definition: Move.h:115
Index to(void) const
Return the square the piece moves to.
Definition: Move.h:104
Move(Index from, Index to, Type promotion)
Construct a Move from square from to square to.
Definition: Move.h:63
TypeData const nothing
A constant representing the absence of a piece.
Definition: Type.h:63
IndexData const index_end
A constant representing& #39;one past the end&#39;.
Definition: Index.h:186
Type promotion_type(void) const
Return the promotion type. Returns empty if this isn&#39;t a promotion.
Definition: Move.h:107

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.