added perft back + fixed some bugs with movegen
This commit is contained in:
1 parent
7e541a40ed
commit
494cd13f7e
5 files changed
+61
-53
No files matched your search
+6
-2
@@ -113,10 +113,14 @@ void Board::SetByFen(const char* fen) {
|
|||||||
section++;
|
section++;
|
||||||
continue;
|
continue;
|
||||||
case 'w':
|
case 'w':
|
||||||
sideToMove = White;
|
if (section == 0) {
|
||||||
|
sideToMove = White;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
sideToMove = Black;
|
if (section == 0) {
|
||||||
|
sideToMove = Black;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'K':
|
case 'K':
|
||||||
castlingRights[White] = true;
|
castlingRights[White] = true;
|
||||||
|
|||||||
+1
-3
@@ -9,9 +9,7 @@ int main() {
|
|||||||
initSliderAttacks();
|
initSliderAttacks();
|
||||||
|
|
||||||
Board board;
|
Board board;
|
||||||
board.PrintBoard();
|
Perft(board, 4);
|
||||||
GenerateMoves(board, board.sideToMove);
|
|
||||||
board.ListMoves();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
+16
-11
@@ -318,21 +318,21 @@ static Bitboard Checkers(Board &board, bool color) {
|
|||||||
static int GetDirection(int square, int target) {
|
static int GetDirection(int square, int target) {
|
||||||
int difference = target - square;
|
int difference = target - square;
|
||||||
|
|
||||||
if (difference % north == 0) {
|
if (difference - north == 0) {
|
||||||
return north;
|
return north;
|
||||||
} else if (difference % south == 0) {
|
} else if (difference - south == 0) {
|
||||||
return south;
|
return south;
|
||||||
} else if (difference % noWe == 0) {
|
} else if (difference - noWe == 0) {
|
||||||
return noWe;
|
return noWe;
|
||||||
} else if (difference % noEa == 0) {
|
} else if (difference - noEa == 0) {
|
||||||
return noEa;
|
return noEa;
|
||||||
} else if (difference % soWe == 0) {
|
} else if (difference - soWe == 0) {
|
||||||
return soWe;
|
return soWe;
|
||||||
} else if (difference % soEa == 0) {
|
} else if (difference - soEa == 0) {
|
||||||
return soEa;
|
return soEa;
|
||||||
} else if (difference < 0 && difference > -8) {
|
} else if (ranks[square / 8] == ranks[target / 8] && target < square) {
|
||||||
return west;
|
return west;
|
||||||
} else if (difference > 0 && difference < 8) {
|
} else if (ranks[square / 8] == ranks[target / 8] && target > square) {
|
||||||
return east;
|
return east;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,6 +344,7 @@ static Bitboard SliderRaysToSquare(int attacker, int target) {
|
|||||||
Bitboard ray;
|
Bitboard ray;
|
||||||
|
|
||||||
int direction = GetDirection(attacker, target);
|
int direction = GetDirection(attacker, target);
|
||||||
|
attacker += direction;
|
||||||
|
|
||||||
if (direction) {
|
if (direction) {
|
||||||
while (attacker != target) {
|
while (attacker != target) {
|
||||||
@@ -358,7 +359,13 @@ static Bitboard GetPinningRay(Board &board, int square, int direction) {
|
|||||||
Bitboard ray;
|
Bitboard ray;
|
||||||
|
|
||||||
if (direction) {
|
if (direction) {
|
||||||
|
int previousRank = (square - direction) / 7;
|
||||||
while (square < 64 && square >= 0) {
|
while (square < 64 && square >= 0) {
|
||||||
|
int currentRank = square / 7;
|
||||||
|
|
||||||
|
if (abs(currentRank - previousRank) > 1) break;
|
||||||
|
previousRank = currentRank;
|
||||||
|
|
||||||
ray.SetBit(square);
|
ray.SetBit(square);
|
||||||
if (board.occupied.IsSet(square)) {
|
if (board.occupied.IsSet(square)) {
|
||||||
int attackerColor = board.GetPieceColor(square);
|
int attackerColor = board.GetPieceColor(square);
|
||||||
@@ -386,7 +393,6 @@ static bool IsPinned(Board &board, int square, int color) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int currentSquare = square - directionToKing;
|
int currentSquare = square - directionToKing;
|
||||||
|
|
||||||
return GetPinningRay(board, currentSquare, -directionToKing).GetBoard();
|
return GetPinningRay(board, currentSquare, -directionToKing).GetBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -811,8 +817,7 @@ static void GenCheckEvasions(Board &board, bool color) {
|
|||||||
int checkerType = board.GetPieceType(checkerSquare);
|
int checkerType = board.GetPieceType(checkerSquare);
|
||||||
|
|
||||||
if (checkerType == Rook || checkerType == Bishop || checkerType == Queen) {
|
if (checkerType == Rook || checkerType == Bishop || checkerType == Queen) {
|
||||||
pushMask = SliderRaysToSquare(checkerSquare, kingSquare)
|
pushMask = SliderRaysToSquare(checkerSquare, kingSquare);
|
||||||
& ~(Bitboard::GetSquare(kingSquare) | Bitboard::GetSquare(checkerSquare));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// looping through own pieces to add captures and blocks
|
// looping through own pieces to add captures and blocks
|
||||||
|
|||||||
+37
-36
@@ -1,45 +1,46 @@
|
|||||||
// #include <chrono>
|
#include <chrono>
|
||||||
// #include "perft.h"
|
#include "perft.h"
|
||||||
|
|
||||||
// U64 HelperPerft(Board board, int depth) {
|
U64 HelperPerft(Board board, int depth) {
|
||||||
// GenerateMoves(board, board.sideToMove);
|
GenerateMoves(board, board.sideToMove);
|
||||||
|
|
||||||
// if (depth == 1) return board.moveList.size();
|
if (depth == 1) return board.moveList.size();
|
||||||
// if (depth == 0) return 1ULL;
|
if (depth == 0) return 1ULL;
|
||||||
|
|
||||||
// U64 nodes = 0;
|
U64 nodes = 0;
|
||||||
|
|
||||||
// for (int i = 0; i < board.moveList.size(); i++) {
|
for (int i = 0; i < board.moveList.size(); i++) {
|
||||||
// board.MakeMove(board.moveList[i]);
|
Board copy = board;
|
||||||
// nodes += HelperPerft(board, depth - 1);
|
board.MakeMove(board.moveList[i]);
|
||||||
// board.UnmakeMove();
|
nodes += HelperPerft(board, depth - 1);
|
||||||
// }
|
board = copy;
|
||||||
// return nodes;
|
}
|
||||||
// }
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
// void Perft(Board &board, int depth) {
|
void Perft(Board &board, int depth) {
|
||||||
// GenerateMoves(board, board.sideToMove);
|
GenerateMoves(board, board.sideToMove);
|
||||||
|
|
||||||
// U64 totalNodes = 0;
|
U64 totalNodes = 0;
|
||||||
|
|
||||||
// for (int i = 0; i < board.moveList.size(); i++) {
|
auto start = std::chrono::steady_clock::now();
|
||||||
// board.MakeMove(board.moveList[i]);
|
for (int i = 0; i < board.moveList.size(); i++) {
|
||||||
// U64 nodeCount = HelperPerft(board, depth - 1);
|
Board copy = board;
|
||||||
// totalNodes += nodeCount;
|
board.MakeMove(board.moveList[i]);
|
||||||
// board.moveList[i].PrintMove();
|
U64 nodeCount = HelperPerft(board, depth - 1);
|
||||||
// board.UnmakeMove();
|
board = copy;
|
||||||
// std::cout << ": " << nodeCount << '\n';
|
totalNodes += nodeCount;
|
||||||
// }
|
std::cout << squareCoords[board.moveList[i].MoveFrom()]
|
||||||
|
<< squareCoords[board.moveList[i].MoveTo()];
|
||||||
|
std::cout << ": " << nodeCount << '\n';
|
||||||
|
}
|
||||||
|
auto finish = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
// auto start = std::chrono::steady_clock::now();
|
double elapsed_seconds = std::chrono::duration_cast<
|
||||||
// U64 nodes = HelperPerft(board, depth);
|
std::chrono::duration<double>>(finish - start).count();
|
||||||
// auto finish = std::chrono::steady_clock::now();
|
|
||||||
|
|
||||||
// double elapsed_seconds = std::chrono::duration_cast<
|
std::cout << "\nDepth: " << depth << '\n';
|
||||||
// std::chrono::duration<double>>(finish - start).count();
|
std::cout << "Total nodes: " << totalNodes << '\n';
|
||||||
|
std::cout << "Time took: " << elapsed_seconds << "s (";
|
||||||
// std::cout << "\nDepth: " << depth << '\n';
|
std::cout << int(totalNodes / elapsed_seconds * 1000) << " nodes/sec)\n";
|
||||||
// std::cout << "Total nodes: " << totalNodes << '\n';
|
}
|
||||||
// std::cout << "Time took: " << elapsed_seconds << "s (";
|
|
||||||
// std::cout << nodes / elapsed_seconds << " nodes/sec)\n";
|
|
||||||
// }
|
|
||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
using U64 = uint64_t;
|
using U64 = uint64_t;
|
||||||
|
|
||||||
constexpr char StartingFen[] = "8/8/8/8/k2Pp2Q/8/8/3K4 b - d3 0 1";
|
constexpr char StartingFen[] = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||||
|
|
||||||
enum COLOR {
|
enum COLOR {
|
||||||
White,
|
White,
|
||||||
|
|||||||
Reference in new issue
Block a user