+
+/// perft() is our utility to verify move generation is bug free. All the
+/// legal moves up to given depth are generated and counted and the sum returned.
+
+int perft(Position& pos, Depth depth)
+{
+ Move move;
+ MovePicker mp = MovePicker(pos, MOVE_NONE, depth, H);
+ Bitboard dcCandidates = mp.discovered_check_candidates();
+ int sum = 0;
+
+ // If we are at the last ply we don't need to do and undo
+ // the moves, just to count them.
+ if (depth <= OnePly) // Replace with '<' to test also qsearch
+ {
+ while ((move = mp.get_next_move()) != MOVE_NONE) sum++;
+ return sum;
+ }
+
+ // Loop through all legal moves
+ while ((move = mp.get_next_move()) != MOVE_NONE)
+ {
+ StateInfo st;
+ pos.do_move(move, st, dcCandidates);
+ sum += perft(pos, depth - OnePly);
+ pos.undo_move(move);
+ }
+ return sum;
+}
+
+