void do_perft(position_t *pos, scored_move_t *ms, int ply, int depth) { scored_move_t *msbase = ms; uint8 stm = Stm(ply); scored_move_t *mv; if (Checked(stm^1)) return; if (Checked(stm)) { ms = generate_evasions(pos, ms, ply); for (mv = msbase; mv < ms; mv++) if (PieceType(Capture(mv->move)) == KING) return; } else { ms = generate_captures(pos, ms, ply); for (mv = msbase; mv < ms; mv++) if (PieceType(Capture(mv->move)) == KING) return; ms = generate_noncaptures(pos, ms, ply); } for (mv = msbase; mv < ms; mv++) { make_move(pos, mv->move, ply); if (depth - 1) do_perft(pos, ms, ply + 1, depth - 1); else if (!Checked(stm)) total_moves++; unmake_move(pos, mv->move, ply); } }
void perft(position_t *pos, int depth) { scored_move_t moveStack[4096]; memset(moveStack, 0, sizeof(scored_move_t)*4096); total_moves = 0; states[1] = states[0]; do_perft(pos, moveStack, 1, depth); }
t_nodes do_perft(struct t_board *board, int depth) { struct t_move_list move_list[1]; struct t_move_list bad_move_list[1]; bad_move_list->count = 0; bad_move_list->imove = 0; struct t_undo undo[1]; t_nodes nodes = 0; int i; assert(integrity(board)); if (board->in_check) { generate_evade_check(board, move_list); if (depth == 1) return move_list->count; } else { //generate_captures(board, move_list); //generate_quiet_moves(board, move_list); generate_moves(board, move_list); //if (!equal_move_lists(move_list, xmove_list)){ // write_board(board, "board.txt"); // write_move_list(xmove_list, "all.txt"); // write_move_list(move_list, "inc.txt"); //} if (depth == 1) return legal_move_count(board, move_list); } for (i = move_list->count - 1; i >= 0; i--) { assert(lookup_move(board, move_as_str(move_list->move[i])) == move_list->move[i]); if (make_next_move(board, move_list, bad_move_list, undo)) { assert(integrity(board)); //nodes++; if (depth > 1) nodes += do_perft(board, depth - 1); else nodes++; unmake_move(board, undo); assert(integrity(board)); } else assert(integrity(board)); } return nodes; }
t_nodes perft(struct t_board *board, int depth) { struct t_move_list move_list[1]; struct t_undo undo[1]; t_nodes total_nodes = 0; t_nodes move_nodes = 0; unsigned long start = time_now(); int i; if (board->in_check) generate_evade_check(board, move_list); else generate_moves(board, move_list); for (i = move_list->count - 1; i >= 0; i--) { if (make_move(board, move_list->pinned_pieces, move_list->move[i], undo)) { move_nodes = 0; if (depth > 1) move_nodes += do_perft(board, depth - 1); printf(move_as_str(move_list->move[i])); printf(" = %u\n", move_nodes); unmake_move(board, undo); total_nodes += move_nodes; } } unsigned long finish = time_now(); if (finish == start) printf("Total Nodes: %I64d\n", total_nodes); else printf("Total Nodes: %I64d in %d milliseconds = nps %I64d\n", total_nodes, finish - start, 1000 * total_nodes / (finish - start)); return total_nodes; }