Added perft function + fixed some move generation bugs(there are still some)
This commit is contained in:
1 parent
915b93ad5a
commit
5962c37546
6 files changed
+93
-38
No files matched your search
+3
-3
@@ -394,6 +394,9 @@ void Board::UnmakeMove() {
|
|||||||
|
|
||||||
int direction = lastMove.capturedColor ? south : north;
|
int direction = lastMove.capturedColor ? south : north;
|
||||||
|
|
||||||
|
// Removing attacker piece from new square
|
||||||
|
RemovePiece(lastMove.attackerPiece, lastMove.moveTo, !lastMove.capturedColor);
|
||||||
|
|
||||||
// Setting back the attacker
|
// Setting back the attacker
|
||||||
SetPiece(lastMove.attackerPiece, lastMove.moveFrom, !lastMove.capturedColor);
|
SetPiece(lastMove.attackerPiece, lastMove.moveFrom, !lastMove.capturedColor);
|
||||||
|
|
||||||
@@ -431,9 +434,6 @@ void Board::UnmakeMove() {
|
|||||||
SetPiece(lastMove.capturedPiece, lastMove.moveTo, lastMove.capturedColor);
|
SetPiece(lastMove.capturedPiece, lastMove.moveTo, lastMove.capturedColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removing attacker piece from new square
|
|
||||||
RemovePiece(lastMove.attackerPiece, lastMove.moveTo, !lastMove.capturedColor);
|
|
||||||
|
|
||||||
castlingRights = lastMove.castlingRights;
|
castlingRights = lastMove.castlingRights;
|
||||||
if (halfMoves & 2 == 0) fullMoves--;
|
if (halfMoves & 2 == 0) fullMoves--;
|
||||||
halfMoves--;
|
halfMoves--;
|
||||||
|
|||||||
+2
-3
@@ -2,15 +2,14 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
|
#include "perft.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
initLeaperAttacks();
|
initLeaperAttacks();
|
||||||
initSliderAttacks();
|
initSliderAttacks();
|
||||||
|
|
||||||
Board board;
|
Board board;
|
||||||
GenerateMoves(board);
|
Perft(board, 4);
|
||||||
board.ListMoves();
|
|
||||||
board.PrintBoard();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+25
-31
@@ -314,24 +314,14 @@ static void GenPawnMoves(Board &board, bool color) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (firstAhead.GetBoard()) {
|
if (firstAhead.GetBoard()) {
|
||||||
|
int secondRank = color ? r_7 : r_2;
|
||||||
if ((firstAhead & ranks[lastRank]).GetBoard()) {
|
if ((firstAhead & ranks[lastRank]).GetBoard()) {
|
||||||
for (int type = knightPromotion; type <= queenPromotion; type++) {
|
for (int type = knightPromotion; type <= queenPromotion; type++) {
|
||||||
board.AddMove(Move(square, firstAhead.getLS1BIndex(), type));
|
board.AddMove(Move(square, firstAhead.getLS1BIndex(), type));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
board.AddMove(Move(square, firstAhead.getLS1BIndex(), quiet));
|
board.AddMove(Move(square, firstAhead.getLS1BIndex(), quiet));
|
||||||
}
|
if (secondAhead.GetBoard() && Bitboard(ranks[secondRank]).IsSet(square)) {
|
||||||
}
|
|
||||||
|
|
||||||
if (color == White) {
|
|
||||||
if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_2).GetBoard()) {
|
|
||||||
if ((secondAhead).GetBoard()) {
|
|
||||||
board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (Bitboard::GetSquare(firstAhead.getLS1BIndex() & RANKS::r_7).GetBoard()) {
|
|
||||||
if ((secondAhead).GetBoard()) {
|
|
||||||
board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush));
|
board.AddMove(Move(square, secondAhead.getLS1BIndex(), doublePawnPush));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -503,44 +493,48 @@ static void GenKingMoves(Board &board, bool color) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isLegalMove(Board &board, Move move) {
|
static bool isLegalMove(Board &board, Move move) {
|
||||||
|
if (board.InCheck(board.sideToMove)) {
|
||||||
|
if (move.moveFlags == kingCastle || move.moveFlags == queenCastle) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
board.MakeMove(move);
|
board.MakeMove(move);
|
||||||
if (board.InCheck(!board.sideToMove)) {
|
if (board.InCheck(!board.sideToMove)) {
|
||||||
board.UnmakeMove();
|
board.UnmakeMove();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
board.UnmakeMove();
|
board.UnmakeMove();
|
||||||
|
} else {
|
||||||
|
board.MakeMove(move);
|
||||||
|
if (board.InCheck(!board.sideToMove)) {
|
||||||
|
board.UnmakeMove();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
board.UnmakeMove();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenerateMoves(Board &board) {
|
void GenerateMoves(Board &board, bool side) {
|
||||||
board.moveList.clear();
|
board.moveList.clear();
|
||||||
|
|
||||||
GenPawnMoves(board, White);
|
GenPawnMoves(board, side);
|
||||||
GenPawnMoves(board, Black);
|
|
||||||
|
|
||||||
GenKnightMoves(board, White);
|
GenKnightMoves(board, side);
|
||||||
GenKnightMoves(board, Black);
|
|
||||||
|
|
||||||
GenRookMoves(board, White);
|
GenRookMoves(board, side);
|
||||||
GenRookMoves(board, Black);
|
|
||||||
|
|
||||||
GenBishopMoves(board, White);
|
GenBishopMoves(board, side);
|
||||||
GenBishopMoves(board, Black);
|
|
||||||
|
|
||||||
GenQueenMoves(board, White);
|
GenQueenMoves(board, side);
|
||||||
GenQueenMoves(board, Black);
|
|
||||||
|
|
||||||
GenKingMoves(board, White);
|
GenKingMoves(board, side);
|
||||||
GenKingMoves(board, Black);
|
|
||||||
|
|
||||||
int listSize = sizeof(board.moveList);
|
int listSize = board.moveList.size();
|
||||||
// Filtering illegal moves
|
// Filtering illegal moves
|
||||||
for (int i = 0; i < listSize; i++) {
|
for (int i = listSize - 1; i >= 0; --i) {
|
||||||
int removedCount = 0;
|
|
||||||
if (!isLegalMove(board, board.moveList[i])) {
|
if (!isLegalMove(board, board.moveList[i])) {
|
||||||
board.moveList.erase(board.moveList.begin() + i - removedCount);
|
board.moveList.erase(board.moveList.begin() + i);
|
||||||
removedCount++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -204,4 +204,4 @@ Bitboard getBishopAttack(int square, U64 occupancy);
|
|||||||
Bitboard getRookAttack(int square, U64 occupancy);
|
Bitboard getRookAttack(int square, U64 occupancy);
|
||||||
Bitboard getQueenAttack(int square, U64 occupancy);
|
Bitboard getQueenAttack(int square, U64 occupancy);
|
||||||
|
|
||||||
void GenerateMoves(Board &board);
|
void GenerateMoves(Board &board, bool side);
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include "perft.h"
|
||||||
|
|
||||||
|
static int captures = 0;
|
||||||
|
static int epCaptures = 0;
|
||||||
|
static int castles = 0;
|
||||||
|
static int promotions = 0;
|
||||||
|
static int checks = 0;
|
||||||
|
|
||||||
|
|
||||||
|
U64 HelperPerft(Board board, int depth) {
|
||||||
|
if (depth == 0) return 1ULL;
|
||||||
|
|
||||||
|
GenerateMoves(board, board.sideToMove);
|
||||||
|
|
||||||
|
U64 nodes = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < board.moveList.size(); i++) {
|
||||||
|
if (board.moveList[i].moveFlags == capture) captures++;
|
||||||
|
if (board.moveList[i].moveFlags == epCapture) epCaptures++;
|
||||||
|
if (board.moveList[i].moveFlags == kingCastle || board.moveList[i].moveFlags == queenCastle) castles++;
|
||||||
|
if (board.moveList[i].moveFlags >= knightPromotion) promotions++;
|
||||||
|
board.MakeMove(board.moveList[i]);
|
||||||
|
if (board.InCheck(board.sideToMove)) checks++;
|
||||||
|
nodes += HelperPerft(board, depth - 1);
|
||||||
|
board.UnmakeMove();
|
||||||
|
}
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Perft(Board &board, int depth) {
|
||||||
|
using std::chrono::high_resolution_clock;
|
||||||
|
using std::chrono::milliseconds;
|
||||||
|
using std::chrono::duration_cast;
|
||||||
|
|
||||||
|
captures = 0;
|
||||||
|
epCaptures = 0;
|
||||||
|
castles = 0;
|
||||||
|
promotions = 0;
|
||||||
|
checks = 0;
|
||||||
|
|
||||||
|
auto start = high_resolution_clock::now();
|
||||||
|
U64 nodes = HelperPerft(board, depth);
|
||||||
|
auto end = high_resolution_clock::now();
|
||||||
|
|
||||||
|
auto duration = duration_cast<milliseconds>(end - start);
|
||||||
|
|
||||||
|
std::cout << "Depth: " << depth << '\n';
|
||||||
|
std::cout << "Nodes searched: " << nodes << '\n';
|
||||||
|
std::cout << "Captures: " << captures << '\n';
|
||||||
|
std::cout << "epCaptures: " << epCaptures << '\n';
|
||||||
|
std::cout << "Castles: " << castles << '\n';
|
||||||
|
std::cout << "Promotions: " << promotions << '\n';
|
||||||
|
std::cout << "Checks: " << checks << '\n' << '\n';
|
||||||
|
std::cout << "Total time taken: " << duration.count() << " ms";
|
||||||
|
std::cout << " (" << (nodes / duration.count()) * 1000 << " nodes/sec)\n";
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "board.h"
|
||||||
|
#include "movegen.h"
|
||||||
|
|
||||||
|
void Perft(Board &board, int depth);
|
||||||
Reference in new issue
Block a user