No functional change.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
// book move is a promotion we have to convert to our representation, in
// all other cases we can directly compare with a Move after having
// masked out special Move's flags that are not supported by PolyGlot.
// book move is a promotion we have to convert to our representation, in
// all other cases we can directly compare with a Move after having
// masked out special Move's flags that are not supported by PolyGlot.
- int p = (bookMove >> 12) & 7;
+ int promotion = (bookMove >> 12) & 7;
bookMove = int(make_promotion_move(move_from(Move(bookMove)),
bookMove = int(make_promotion_move(move_from(Move(bookMove)),
- move_to(Move(bookMove)), PieceType(p + 1)));
-
- // Verify the book move (if any) is legal
- MoveStack mlist[MAX_MOVES];
- MoveStack* last = generate<MV_LEGAL>(pos, mlist);
- for (MoveStack* cur = mlist; cur != last; cur++)
- if ((int(cur->move) & ~(3 << 14)) == bookMove) // Mask out special flags
- return cur->move;
+ move_to(Move(bookMove)),
+ PieceType(promotion + 1)));
+ // Verify the book move is legal
+ for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
+ if ((ml.move() & ~(3 << 14)) == bookMove) // Mask out special flags
+ return ml.move();
Move move_from_uci(const Position& pos, const string& str) {
Move move_from_uci(const Position& pos, const string& str) {
- MoveStack mlist[MAX_MOVES];
- MoveStack* last = generate<MV_LEGAL>(pos, mlist);
-
- for (MoveStack* cur = mlist; cur != last; cur++)
- if (str == move_to_uci(cur->move, pos.is_chess960()))
- return cur->move;
+ for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
+ if (str == move_to_uci(ml.move(), pos.is_chess960()))
+ return ml.move();
template<MoveType>
MoveStack* generate(const Position& pos, MoveStack* mlist);
template<MoveType>
MoveStack* generate(const Position& pos, MoveStack* mlist);
+template<MoveType T>
+struct MoveList {
+
+ explicit MoveList(const Position& pos) : cur(mlist), last(generate<T>(pos, mlist)) {}
+ void operator++() { cur++; }
+ bool end() const { return cur == last; }
+ Move move() const { return cur->move; }
+ int size() const { return last - mlist; }
+
+private:
+ MoveStack mlist[MAX_MOVES];
+ MoveStack *cur, *last;
+};
+
#endif // !defined(MOVEGEN_H_INCLUDED)
#endif // !defined(MOVEGEN_H_INCLUDED)
bool Position::is_mate() const {
bool Position::is_mate() const {
- MoveStack moves[MAX_MOVES];
- return in_check() && generate<MV_LEGAL>(*this, moves) == moves;
+ return in_check() && !MoveList<MV_LEGAL>(*this).size();
int64_t perft(Position& pos, Depth depth) {
int64_t perft(Position& pos, Depth depth) {
- MoveStack mlist[MAX_MOVES];
int64_t sum = 0;
// Generate all legal moves
int64_t sum = 0;
// Generate all legal moves
- MoveStack* last = generate<MV_LEGAL>(pos, mlist);
+ MoveList<MV_LEGAL> ml(pos);
// If we are at the last ply we don't need to do and undo
// the moves, just to count them.
if (depth <= ONE_PLY)
// If we are at the last ply we don't need to do and undo
// the moves, just to count them.
if (depth <= ONE_PLY)
- return int(last - mlist);
// Loop through all legal moves
CheckInfo ci(pos);
// Loop through all legal moves
CheckInfo ci(pos);
- for (MoveStack* cur = mlist; cur != last; cur++)
+ for ( ; !ml.end(); ++ml)
- m = cur->move;
- pos.do_move(m, st, ci, pos.move_gives_check(m, ci));
+ pos.do_move(ml.move(), st, ci, pos.move_gives_check(ml.move(), ci));
sum += perft(pos, depth - ONE_PLY);
sum += perft(pos, depth - ONE_PLY);
+ pos.undo_move(ml.move());
void RootMoveList::init(Position& pos, Move searchMoves[]) {
void RootMoveList::init(Position& pos, Move searchMoves[]) {
- MoveStack mlist[MAX_MOVES];
// Generate all legal moves and add them to RootMoveList
// Generate all legal moves and add them to RootMoveList
- MoveStack* last = generate<MV_LEGAL>(pos, mlist);
- for (MoveStack* cur = mlist; cur != last; cur++)
+ for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
- // If we have a searchMoves[] list then verify cur->move
+ // If we have a searchMoves[] list then verify the move
// is in the list before to add it.
// is in the list before to add it.
- for (sm = searchMoves; *sm && *sm != cur->move; sm++) {}
+ for (sm = searchMoves; *sm && *sm != ml.move(); sm++) {}
- if (searchMoves[0] && *sm != cur->move)
+ if (sm != searchMoves && *sm != ml.move())
rm.pv[1] = MOVE_NONE;
rm.pv_score = -VALUE_INFINITE;
push_back(rm);
rm.pv[1] = MOVE_NONE;
rm.pv_score = -VALUE_INFINITE;
push_back(rm);
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include <iostream>
#include <cstring>
#include <iostream>
if (!entries)
{
std::cerr << "Failed to allocate " << mbSize
if (!entries)
{
std::cerr << "Failed to allocate " << mbSize
- << " MB for transposition table." << std::endl;
+ << "MB for transposition table." << std::endl;
exit(EXIT_FAILURE);
}
clear();
exit(EXIT_FAILURE);
}
clear();