From: Marco Costalba Date: Sat, 2 Jul 2011 12:33:06 +0000 (+0100) Subject: Introduce and use struct MoveList X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=53ccba8457231677897f531ff283136edc550cf2 Introduce and use struct MoveList No functional change. Signed-off-by: Marco Costalba --- diff --git a/src/book.cpp b/src/book.cpp index ed5cba4b..0cf4ed0d 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -455,18 +455,16 @@ Move Book::get_move(const Position& pos, bool findBestMove) { // 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; - if (p) + if (promotion) 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(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 ml(pos); !ml.end(); ++ml) + if ((ml.move() & ~(3 << 14)) == bookMove) // Mask out special flags + return ml.move(); return MOVE_NONE; } diff --git a/src/move.cpp b/src/move.cpp index 3c906b2a..060e206b 100644 --- a/src/move.cpp +++ b/src/move.cpp @@ -71,12 +71,9 @@ const string move_to_uci(Move m, bool chess960) { Move move_from_uci(const Position& pos, const string& str) { - MoveStack mlist[MAX_MOVES]; - MoveStack* last = generate(pos, mlist); - - for (MoveStack* cur = mlist; cur != last; cur++) - if (str == move_to_uci(cur->move, pos.is_chess960())) - return cur->move; + for (MoveList ml(pos); !ml.end(); ++ml) + if (str == move_to_uci(ml.move(), pos.is_chess960())) + return ml.move(); return MOVE_NONE; } diff --git a/src/movegen.h b/src/movegen.h index 580ca2e7..4e58d0ff 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -36,4 +36,18 @@ enum MoveType { template MoveStack* generate(const Position& pos, MoveStack* mlist); +template +struct MoveList { + + explicit MoveList(const Position& pos) : cur(mlist), last(generate(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) diff --git a/src/position.cpp b/src/position.cpp index 1e01dc1f..7490953d 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -1724,8 +1724,7 @@ template bool Position::is_draw() const; bool Position::is_mate() const { - MoveStack moves[MAX_MOVES]; - return in_check() && generate(*this, moves) == moves; + return in_check() && !MoveList(*this).size(); } diff --git a/src/search.cpp b/src/search.cpp index 4402cbd5..3512f20e 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -363,27 +363,24 @@ void init_search() { int64_t perft(Position& pos, Depth depth) { - MoveStack mlist[MAX_MOVES]; StateInfo st; - Move m; int64_t sum = 0; // Generate all legal moves - MoveStack* last = generate(pos, mlist); + MoveList 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) - return int(last - mlist); + return ml.size(); // 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); - pos.undo_move(m); + pos.undo_move(ml.move()); } return sum; } @@ -1992,25 +1989,22 @@ split_point_start: // At split points actual search starts from here void RootMoveList::init(Position& pos, Move searchMoves[]) { - MoveStack mlist[MAX_MOVES]; Move* sm; - - clear(); bestMoveChanges = 0; + clear(); // Generate all legal moves and add them to RootMoveList - MoveStack* last = generate(pos, mlist); - for (MoveStack* cur = mlist; cur != last; cur++) + for (MoveList 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. - 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()) continue; RootMove rm; - rm.pv[0] = cur->move; + rm.pv[0] = ml.move(); rm.pv[1] = MOVE_NONE; rm.pv_score = -VALUE_INFINITE; push_back(rm); diff --git a/src/tt.cpp b/src/tt.cpp index 86b16c85..b2701ef0 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -17,7 +17,6 @@ along with this program. If not, see . */ -#include #include #include @@ -60,7 +59,7 @@ void TranspositionTable::set_size(size_t 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();