1
0
Fork 0

Introduce and use struct MoveList

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
sf_2.3.1_base
Marco Costalba 2011-07-02 13:33:06 +01:00
parent 7ac6e3b850
commit 53ccba8457
6 changed files with 38 additions and 37 deletions

View File

@ -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<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();
return MOVE_NONE;
}

View File

@ -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<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();
return MOVE_NONE;
}

View File

@ -36,4 +36,18 @@ enum MoveType {
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)

View File

@ -1724,8 +1724,7 @@ template bool Position::is_draw<true>() 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();
}

View File

@ -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<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)
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<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.
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);

View File

@ -17,7 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <cstring>
#include <iostream>
@ -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();