Pseudo legal movegen #1
No files matched your search
@@ -36,9 +36,7 @@ void Bitboard::SetBit(int square) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Bitboard::PopBit(int square) {
|
void Bitboard::PopBit(int square) {
|
||||||
if (Bitboard(m_board).IsSet(square)) {
|
m_board &= ~(1ULL << square);
|
||||||
m_board &= (~Bitboard::GetSquare(square)).GetBoard();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int Bitboard::PopCount() const {
|
int Bitboard::PopCount() const {
|
||||||
|
|||||||
@@ -22,10 +22,6 @@ public:
|
|||||||
int getLS1BIndex() const;
|
int getLS1BIndex() const;
|
||||||
static Bitboard getOccupancy(int index, Bitboard attackMask);
|
static Bitboard getOccupancy(int index, Bitboard attackMask);
|
||||||
|
|
||||||
static Bitboard GetSquare(int square) {
|
|
||||||
return (1ULL << square);
|
|
||||||
};
|
|
||||||
|
|
||||||
Bitboard operator&(const Bitboard& other) const;
|
Bitboard operator&(const Bitboard& other) const;
|
||||||
Bitboard operator|(const Bitboard& other) const;
|
Bitboard operator|(const Bitboard& other) const;
|
||||||
Bitboard operator^(const Bitboard& other) const;
|
Bitboard operator^(const Bitboard& other) const;
|
||||||
|
|||||||
+8
-6
@@ -1,5 +1,4 @@
|
|||||||
#include "board.h"
|
#include "board.h"
|
||||||
#include "movegen.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void Board::Init() {
|
void Board::Init() {
|
||||||
@@ -16,6 +15,8 @@ void Board::Reset() {
|
|||||||
|
|
||||||
pieces = std::array<Bitboard, 6>();
|
pieces = std::array<Bitboard, 6>();
|
||||||
colors = std::array<Bitboard, 2>();
|
colors = std::array<Bitboard, 2>();
|
||||||
|
|
||||||
|
moveList = std::array<Move, 218>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::SetByFen(const char* fen) {
|
void Board::SetByFen(const char* fen) {
|
||||||
@@ -79,7 +80,7 @@ void Board::SetByFen(const char* fen) {
|
|||||||
pieceColor = White;
|
pieceColor = White;
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
currentSquare = currentSquare >> 8 + digits;
|
currentSquare = currentSquare >> (8 + digits);
|
||||||
digits = 0;
|
digits = 0;
|
||||||
fen++;
|
fen++;
|
||||||
continue;
|
continue;
|
||||||
@@ -236,18 +237,19 @@ void Board::PrintBoard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Board::AddMove(Move move) {
|
void Board::AddMove(Move move) {
|
||||||
moveList.push_back(move);
|
moveList[currentMoveIndex] = move;
|
||||||
|
currentMoveIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::ResetMoves() {
|
void Board::ResetMoves() {
|
||||||
moveList.clear();
|
currentMoveIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::ListMoves() {
|
void Board::ListMoves() {
|
||||||
int count = 1;
|
int count = 1;
|
||||||
for (Move move : moveList) {
|
for (int i = 0; i < currentMoveIndex; i++) {
|
||||||
std::cout << count << ". ";
|
std::cout << count << ". ";
|
||||||
move.PrintMove();
|
moveList[i].PrintMove();
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -17,7 +17,8 @@ public:
|
|||||||
|
|
||||||
Bitboard occupied;
|
Bitboard occupied;
|
||||||
|
|
||||||
std::vector<Move> moveList;
|
std::array<Move, 218> moveList;
|
||||||
|
int currentMoveIndex = 0;
|
||||||
|
|
||||||
bool sideToMove = White;
|
bool sideToMove = White;
|
||||||
int halfMoves = 0;
|
int halfMoves = 0;
|
||||||
|
|||||||
+5
-5
@@ -18,7 +18,7 @@ static Bitboard notHGfile = notHfile & ~files[G];
|
|||||||
|
|
||||||
Bitboard maskPawnAttacks(int side, int square) {
|
Bitboard maskPawnAttacks(int side, int square) {
|
||||||
Bitboard attacks;
|
Bitboard attacks;
|
||||||
Bitboard attacker = Bitboard::GetSquare(square);
|
Bitboard attacker = (1ULL << square);
|
||||||
|
|
||||||
// White
|
// White
|
||||||
if (!side) {
|
if (!side) {
|
||||||
@@ -35,7 +35,7 @@ Bitboard maskPawnAttacks(int side, int square) {
|
|||||||
|
|
||||||
Bitboard maskKnightAttacks(int square) {
|
Bitboard maskKnightAttacks(int square) {
|
||||||
Bitboard attacks;
|
Bitboard attacks;
|
||||||
Bitboard attacker = Bitboard::GetSquare(square);
|
Bitboard attacker = (1ULL << square);
|
||||||
|
|
||||||
if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north);
|
if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north);
|
||||||
if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north);
|
if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north);
|
||||||
@@ -52,7 +52,7 @@ Bitboard maskKnightAttacks(int square) {
|
|||||||
|
|
||||||
Bitboard maskKingAttacks(int square) {
|
Bitboard maskKingAttacks(int square) {
|
||||||
Bitboard attacks;
|
Bitboard attacks;
|
||||||
Bitboard attacker = Bitboard::GetSquare(square);
|
Bitboard attacker = (1ULL << square);
|
||||||
|
|
||||||
attacks |= (attacker << north);
|
attacks |= (attacker << north);
|
||||||
attacks |= (attacker << south);
|
attacks |= (attacker << south);
|
||||||
@@ -369,10 +369,10 @@ static void GenKingMoves(Board &board, bool color) {
|
|||||||
|
|
||||||
void GenerateMoves(Board &board) {
|
void GenerateMoves(Board &board) {
|
||||||
bool side = board.sideToMove;
|
bool side = board.sideToMove;
|
||||||
board.moveList.clear();
|
board.ResetMoves();
|
||||||
|
|
||||||
GenPawnMoves(board, side);
|
GenPawnMoves(board, side);
|
||||||
GenKnightMoves(board, side);
|
GenKnightMoves(board, side);
|
||||||
GenKingMoves(board, side);
|
GenKingMoves(board, side);
|
||||||
GenBishopMoves(board, side);
|
GenBishopMoves(board, side);
|
||||||
GenRookMoves(board, side);
|
GenRookMoves(board, side);
|
||||||
|
|||||||
+7
-9
@@ -4,16 +4,15 @@
|
|||||||
U64 HelperPerft(Board board, int depth) {
|
U64 HelperPerft(Board board, int depth) {
|
||||||
GenerateMoves(board);
|
GenerateMoves(board);
|
||||||
|
|
||||||
if (depth == 1) return board.moveList.size();
|
if (depth == 1) return board.currentMoveIndex;
|
||||||
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.currentMoveIndex; i++) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
board.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i]);
|
||||||
nodes += HelperPerft(board, depth - 1);
|
nodes += HelperPerft(copy, depth - 1);
|
||||||
board = copy;
|
|
||||||
}
|
}
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
@@ -24,11 +23,10 @@ void Perft(Board &board, int depth) {
|
|||||||
U64 totalNodes = 0;
|
U64 totalNodes = 0;
|
||||||
|
|
||||||
auto start = std::chrono::steady_clock::now();
|
auto start = std::chrono::steady_clock::now();
|
||||||
for (int i = 0; i < board.moveList.size(); i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
board.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i]);
|
||||||
U64 nodeCount = HelperPerft(board, depth - 1);
|
U64 nodeCount = HelperPerft(copy, depth - 1);
|
||||||
board = copy;
|
|
||||||
totalNodes += nodeCount;
|
totalNodes += nodeCount;
|
||||||
std::cout << squareCoords[board.moveList[i].MoveFrom()]
|
std::cout << squareCoords[board.moveList[i].MoveFrom()]
|
||||||
<< squareCoords[board.moveList[i].MoveTo()];
|
<< squareCoords[board.moveList[i].MoveTo()];
|
||||||
|
|||||||
Reference in new issue
Block a user