MoveIterator.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file MoveIterator.h This file contains the declaration of class MoveIterator.
4 //
5 // Copyright (C) 2008 - 2010, 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 MOVEITERATOR_H
25 #define MOVEITERATOR_H
26 
27 #ifndef USE_PCH
28 #include <iterator>
29 #endif
30 
31 #include "Move.h"
32 #include "BitBoard.h"
33 #include "Piece.h"
34 
35 #define CWCHESSBOARD_LIKELY(condition) __builtin_expect(condition, true)
36 
37 namespace cwchess {
38 
39 class ChessPosition;
40 
41 /** @brief Non-mutable iterator over all moves of a given chess piece.
42 *
43  * This iterator generates the Move objects while advancing to the next
44  * move, storing the result in a member variable. The lifetime of the reference
45  * or pointer to the Move is therefore equal to the lifetime of the iterator:
46  * calling the increment or decrement operators overwrites the previous move.
47 *
48  * Usage example:
49 *
50  * \code
51  * for (MoveIterator move_iter = chess_position.move_begin(index); move_iter != chess_position.move_end(); ++move_iter)
52  * {
53  * Move const& move(*move_iter);
54  * // Use 'move'.
55  * }
56  * \endcode
57 * /
58 class MoveIterator : public std::iterator<std::bidirectional_iterator_tag, Move> {
59  protected:
60  ChessPosition const* M_chess_position; //!< The underlaying chess position.
61  BitBoard M_targets; //!< The targets that this piece can move to.
62  Move M_current_move; //!< The actual move that is returned when dereferenced.
63 
64  public:
65  /** @name Constructors* /
66  //@{
67 
68  // Default Constructible.
69  /** @brief Construct a one-past-the-end MoveIterator.
70  *
71  * @sa ChessPosition::move_end()
72  * /
73  MoveIterator(void) : M_current_move(index_end, index_end, nothing) { }
74 
75  // Assignable.
76  //! @brief Copy-Constructor.
77  MoveIterator(MoveIterator const& iter) :
78  M_chess_position(iter.M_chess_position), M_targets(iter.M_targets), M_current_move(iter.M_current_move) { }
79 
80  /** @brief Construct a fully initialized MoveIterator.
81  *
82  * @param chess_position : The ChessPosition that we're generating moves for.
83  * @param index : The index of the piece that we're generating moves for.
84  *
85  * @sa ChessPosition::move_begin(Index const&)
86  * /
87  MoveIterator(ChessPosition const* chess_position, Index const& index);
88 
89  //@}
90 
91  /** @name Assignment operator* /
92  //@{
93 
94  //! @brief Assignment operator.
96  { M_chess_position = iter.M_chess_position; M_targets = iter.M_targets; M_current_move = iter.M_current_move; return* this; }
97 
98  //@}
99 
100  /** @name Comparision operators* /
101  //@{
102 
103  // Equality Comparable.
104  bool operator==(MoveIterator const& iter) const { return M_current_move == iter.M_current_move; }
105  bool operator!=(MoveIterator const& iter) const { return M_current_move != iter.M_current_move; }
106 
107  //@}
108 
109  /** @name Accessors* /
110  //@{
111 
112  // Dereferencable.
113  Move const& operator*() const { return M_current_move; }
114  Move const* operator->(void) const { return& M_current_move; }
115 
116  //@}
117 
118  /** @name Increment and decrement operators* /
119  //@{
120 
121  // Bi-directional iterator.
122  MoveIterator& operator++()
123  {
124  if (CWCHESSBOARD_LIKELY(!M_current_move.is_promotion()) || next_promotion())
125  {
126  Index current_index(M_current_move.to());
127  current_index.next_bit_in(M_targets());
128  M_current_move.set_to(current_index);
129  }
130  return* this;
131  }
132 
133  MoveIterator operator++(int) { MoveIterator result(*this); operator++(); return result; }
134 
135  MoveIterator& operator--()
136  {
137  if (CWCHESSBOARD_LIKELY(!M_current_move.is_promotion()) || prev_promotion())
138  {
139  Index current_index(M_current_move.to());
140  current_index.prev_bit_in(M_targets());
141  M_current_move.set_to(current_index);
142  }
143  return* this;
144  }
145 
146  MoveIterator operator--(int) { MoveIterator result(*this); operator--(); return result; }
147 
148  //@}
149 
150  public_notdocumented:
151  // Indexing.
152  uint32_t index(void) const { return M_current_move.to()(); }
153 
154  private:
155  bool next_promotion(void);
156  bool prev_promotion(void);
157 
158  Index get_first_bitindex(void) const { Index result(index_pre_begin); result.next_bit_in(M_targets()); return result; }
159 
160  Type initial_type(Index const& index) const;
161 };
162 
163 } // namespace cwchess
164 
165 #endif // MOVEITERATOR_H
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
void prev_bit_in(uint64_t mask)
Retreat Index to the previous bit that is set.
Definition: Index.h:432
A chess move in a particular chess position.
Definition: Move.h:44
void next_bit_in(uint64_t mask)
Advance the index to the next bit that is set in mask.
Definition: Index.h:360
Non-mutable iterator over all moves of a given chess piece.
Definition: MoveIterator.h:58
MoveIterator(void)
Construct a one-past-the-end MoveIterator.
Definition: MoveIterator.h:73
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
A one-boolean-per-square chessboard.
Definition: BitBoard.h:266
The index of a chess square.
Definition: Index.h:211
MoveIterator & operator=(MoveIterator const& iter)
Assignment operator.
Definition: MoveIterator.h:95
ChessPosition const * M_chess_position
The underlaying chess position.
Definition: MoveIterator.h:60
Move M_current_move
The actual move that is returned when dereferenced.
Definition: MoveIterator.h:62
This file contains the declaration of class BitBoard.
A chess position.
Definition: ChessPosition.h:50
Index to(void) const
Return the square the piece moves to.
Definition: Move.h:104
MoveIterator(MoveIterator const& iter)
Copy-Constructor.
Definition: MoveIterator.h:77
TypeData const nothing
A constant representing the absence of a piece.
Definition: Type.h:63
IndexData const index_pre_begin
A constant representing& #39;one before the start&#39;.
Definition: Index.h:182
This file contains the definition of class Move.
IndexData const index_end
A constant representing& #39;one past the end&#39;.
Definition: Index.h:186
BitBoard M_targets
The targets that this piece can move to.
Definition: MoveIterator.h:61
This file contains the declaration of class Piece.

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