Added makefile and rook mask generation

This commit is contained in:
rektdie committed 2024-03-03 12:45:01 +01:00
1 parent 9976cb0968
commit 78fe989180
11 files changed
+76 -10

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
#include <iostream>
#include "bitboards.h"
void Bitboard::PrintBoard() const {
for (int rank = 7; rank >= 0; rank--) {
std::cout << "+---+---+---+---+---+---+---+---+\n";
std::cout << "| ";
for (int file = 0; file < 8; file++) {
int square = rank * 8 + file;
std::cout << IsSet(square) << " | ";
}
std::cout << ' ' << rank + 1 << '\n';
}
std::cout << "+---+---+---+---+---+---+---+---+\n";
std::cout << " a b c d e f g h\n\n";
}
U64 Bitboard::GetBoard() const {
return m_board;
}
bool Bitboard::IsSet(int square) const {
return (1ULL << square) & m_board;
}
void Bitboard::SetBit(int square) {
m_board |= (1ULL << square);
}
// Operator overloading for easier comparisons
Bitboard Bitboard::operator&(const Bitboard& other) const {
return m_board & other.m_board;
}
Bitboard Bitboard::operator|(const Bitboard& other) const {
return m_board | other.m_board;
}
Bitboard Bitboard::operator^(const Bitboard& other) const {
return m_board ^ other.m_board;
}
Bitboard Bitboard::operator*(const Bitboard& other) const {
return m_board * other.m_board;
}
Bitboard Bitboard::operator&=(const Bitboard& other) {
return m_board &= other.m_board;
}
Bitboard Bitboard::operator|=(const Bitboard& other) {
return m_board |= other.m_board;
}
Bitboard Bitboard::operator^=(const Bitboard& other) {
return m_board ^= other.m_board;
}
Bitboard Bitboard::operator*=(const Bitboard& other) {
return m_board *= other.m_board;
}
Bitboard Bitboard::operator<<(int other) {
if (other > 0) {
return m_board << other;
}
else {
return m_board >> other * -1;
}
}
Bitboard Bitboard::operator>>(int other) {
if (other > 0) {
return m_board >> other;
}
else {
return m_board << other * -1;
}
}
Bitboard Bitboard::operator~() {
return ~(m_board);
}
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#include "types.h"
struct Bitboard {
private:
U64 m_board;
public:
Bitboard()
: m_board(0ULL) {}
Bitboard(const U64& board)
: m_board(board) {}
U64 GetBoard() const;
bool IsSet(int square) const;
void SetBit(int square);
void PrintBoard() const;
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);
Bitboard operator|=(const Bitboard& other);
Bitboard operator^=(const Bitboard& other);
Bitboard operator*=(const Bitboard& other);
Bitboard operator<<(int other);
Bitboard operator>>(int other);
Bitboard operator~();
};
+184
View File
@@ -0,0 +1,184 @@
#include "board.h"
#include "types.h"
#include "bitboards.h"
#include <iostream>
void Board::Init() {
SetByFen(StartingFen);
}
void Board::Reset() {
castlingRights = { false, false, false, false };
enPassantTarget = 0ULL;
halfMoves = 0;
fullMoves = 0;
sideToMove = White;
occupied = 0ULL;
std::cout << '\n';
pieces = std::array<Bitboard, 6>();
colors = std::array<Bitboard, 2>();
}
void Board::SetByFen(const char* fen) {
Reset();
// Starting from top left
Bitboard currentSquare = 0x100000000000000;
int digits = 0;
while (*fen != ' ' && *fen) {
PIECE pieceType = Pawn;
COLOR pieceColor = Black;
switch (*fen) {
case 'p':
pieceType = Pawn;
pieceColor = Black;
break;
case 'P':
pieceType = Pawn;
pieceColor = White;
break;
case 'n':
pieceType = Knight;
pieceColor = Black;
break;
case 'N':
pieceType = Knight;
pieceColor = White;
break;
case 'b':
pieceType = Bishop;
pieceColor = Black;
break;
case 'B':
pieceType = Bishop;
pieceColor = White;
break;
case 'r':
pieceType = Rook;
pieceColor = Black;
break;
case 'R':
pieceType = Rook;
pieceColor = White;
break;
case 'k':
pieceType = King;
pieceColor = Black;
break;
case 'K':
pieceType = King;
pieceColor = White;
break;
case 'q':
pieceType = Queen;
pieceColor = Black;
break;
case 'Q':
pieceType = Queen;
pieceColor = White;
break;
case '/':
currentSquare = currentSquare >> 8 + digits;
digits = 0;
fen++;
continue;
default:
int num = (*fen) - 48;
fen++;
if (*fen != '/') {
currentSquare = currentSquare << num;
digits += num;
}
continue;
}
pieces[pieceType] |= currentSquare;
colors[pieceColor] |= currentSquare;
fen++;
if (*fen != '/') {
digits++;
currentSquare = currentSquare << 1;
}
}
fen++; // skipping previous space
int section = 0;
while (*fen) {
switch (*fen) {
case ' ':
fen++;
section++;
continue;
case 'w':
sideToMove = White;
break;
case 'b':
sideToMove = Black;
break;
case 'K':
castlingRights[White] = true;
break;
case 'Q':
castlingRights[White + 2] = true;
break;
case 'k':
castlingRights[Black] = true;
break;
case 'q':
castlingRights[Black + 2] = true;
break;
default:
if (*fen != '-') {
if (section == 2) {
int passantFile;
switch (*fen)
{
case 'a':
passantFile = 0;
break;
case 'b':
passantFile = 1;
break;
case 'c':
passantFile = 2;
break;
case 'd':
passantFile = 3;
break;
case 'e':
passantFile = 4;
break;
case 'f':
passantFile = 5;
break;
case 'g':
passantFile = 6;
break;
case 'h':
passantFile = 7;
break;
}
fen++;
const int passantRank = *fen - 49;
enPassantTarget = Bitboard::GetSquare(passantFile + passantRank*8);
}
else if (section == 3) {
halfMoves = (*fen) - 48;
}
else if (section == 4) {
fullMoves = (*fen) - 48;
}
}
break;
}
fen++;
}
occupied = (colors[White] | colors[Black]);
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "bitboards.h"
#include <array>
class Board {
public:
std::array<Bitboard, 6> pieces;
std::array<Bitboard, 2> colors;
Bitboard occupied;
COLOR sideToMove = White;
int halfMoves = 0;
int fullMoves = 0;
std::array<bool, 4> castlingRights = { false, false, false, false };
Bitboard enPassantTarget;
void Init();
void Reset();
void SetByFen(const char* fen);
};
+14
View File
@@ -0,0 +1,14 @@
#include <iostream>
#include "types.h"
#include "board.h"
#include "movegen.h"
int main() {
Board board;
board.Init();
initLeaperAttacks();
maskRookAttacks(e4).PrintBoard();
return 0;
}
+129
View File
@@ -0,0 +1,129 @@
#include "movegen.h"
Bitboard pawnAttacks[2][64];
Bitboard knightAttacks[64];
Bitboard kingAttacks[64];
static Bitboard notAfile = ~files[A];
static Bitboard notHfile = ~files[H];
static Bitboard notABfile = notAfile & ~files[B];
static Bitboard notHGfile = notHfile & ~files[G];
Bitboard maskPawnAttacks(int side, int square) {
Bitboard attacks;
Bitboard attacker = Bitboard::GetSquare(square);
// White
if (!side) {
if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa);
if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe);
}
// Black
else {
if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa);
if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe);
}
return attacks;
}
Bitboard maskKnightAttacks(int square) {
Bitboard attacks;
Bitboard attacker = Bitboard::GetSquare(square);
if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north);
if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north);
if (((attacker << noWe + west) & notHGfile).GetBoard()) attacks |= (attacker << noWe + west);
if (((attacker << noEa + east) & notABfile).GetBoard()) attacks |= (attacker << noEa + east);
if (((attacker << soWe + south) & notHfile).GetBoard()) attacks |= (attacker << soWe + south);
if (((attacker << soEa + south) & notAfile).GetBoard()) attacks |= (attacker << soEa + south);
if (((attacker << soEa + east) & notABfile).GetBoard()) attacks |= (attacker << soEa + east);
if (((attacker << soWe + west) & notHGfile).GetBoard()) attacks |= (attacker << soWe + west);
return attacks;
}
Bitboard maskKingAttacks(int square) {
Bitboard attacks;
Bitboard attacker = Bitboard::GetSquare(square);
attacks |= (attacker << north);
attacks |= (attacker << south);
if (((attacker << west) & notHfile).GetBoard()) attacks |= (attacker << west);
if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe);
if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa);
if (((attacker << east) & notAfile).GetBoard()) attacks |= (attacker << east);
if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa);
if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe);
return attacks;
}
Bitboard maskBishopAttacks(int square) {
Bitboard attacks;
// target rank and files
int tr = square / 8;
int tf = square % 8;
// mask relevant occupancy bits
// North West
for (int r = tr + 1, f = tf - 1; r <= 6 && f >= 1; r++, f--) {
attacks |= (1ULL << (r * 8 + f));
}
// North East
for (int r = tr + 1, f = tf + 1; r <= 6 && f <= 6; r++, f++) {
attacks |= (1ULL << (r * 8 + f));
}
//South West
for (int r = tr - 1, f = tf - 1; r >= 1 && f >= 1; r--, f--) {
attacks |= (1ULL << (r * 8 + f));
}
//South East
for (int r = tr - 1, f = tf + 1; r >= 1 && f <= 6; r--, f++) {
attacks |= (1ULL << (r * 8 + f));
}
return attacks;
}
Bitboard maskRookAttacks(int square) {
Bitboard attacks;
// target rank and files
int tr = square / 8;
int tf = square % 8;
// mask relevant occupancy bits
// North
for (int r = tr + 1; r <= 6; r++) {
attacks |= (1ULL << (r * 8 + tf));
}
// South
for (int r = tr - 1; r >= 1; r--) {
attacks |= (1ULL << (r * 8 + tf));
}
// East
for (int f = tf + 1; f <= 6; f++) {
attacks |= (1ULL << (tr * 8 + f));
}
// West
for (int f = tf - 1; f >= 1; f--) {
attacks |= (1ULL << (tr * 8 + f));
}
return attacks;
}
void initLeaperAttacks() {
for (int square = 0; square < 64; square++) {
pawnAttacks[White][square] = maskPawnAttacks(White, square);
pawnAttacks[Black][square] = maskPawnAttacks(Black, square);
knightAttacks[square] = maskKnightAttacks(square);
kingAttacks[square] = maskKingAttacks(square);
}
}
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include <array>
#include "bitboards.h"
// pawn attacks table [side][square]
extern Bitboard pawnAttacks[2][64];
extern Bitboard knightAttacks[64];
extern Bitboard kingAttacks[64];
// generate pawn attacks
Bitboard maskPawnAttacks(int square, int side);
// generate knight attacks
Bitboard maskKnightAttacks(int square);
// generate king attacks
Bitboard maskKingAttacks(int square);
// generate bishop attacks
Bitboard maskBishopAttacks(int square);
// generate rook attacks
Bitboard maskRookAttacks(int square);
void initLeaperAttacks();
+70
View File
@@ -0,0 +1,70 @@
#pragma once
#include <cstdint>
#include <array>
using U64 = uint64_t;
constexpr char StartingFen[] = "3b4/3P1N2/2b1P2K/n4r2/6Pk/3p2r1/2P1qQ2/6n1 w - - 0 1";
enum COLOR {
White,
Black
};
enum PIECE {
Pawn,
Knight,
Bishop,
Rook,
Queen,
King
};
enum FILES { A, B, C, D, E, F, G, H };
enum RANKS { r_1, r_2, r_3, r_4, r_5, r_6, r_7, r_8 };
constexpr std::array<U64, 8> files {
0x0101010101010101,
0x0202020202020202,
0x0404040404040404,
0x0808080808080808,
0x1010101010101010,
0x2020202020202020,
0x4040404040404040,
0x8080808080808080
};
constexpr std::array<U64, 8> ranks {
0x00000000000000ff,
0x000000000000ff00,
0x0000000000ff0000,
0x00000000ff000000,
0x000000ff00000000,
0x0000ff0000000000,
0x00ff000000000000,
0xff00000000000000
};
enum SQUARE {
a1, b1, c1, d1, e1, f1, g1, h1,
a2, b2, c2, d2, e2, f2, g2, h2,
a3, b3, c3, d3, e3, f3, g3, h3,
a4, b4, c4, d4, e4, f4, g4, h4,
a5, b5, c5, d5, e5, f5, g5, h5,
a6, b6, c6, d6, e6, f6, g6, h6,
a7, b7, c7, d7, e7, f7, g7, h7,
a8, b8, c8, d8, e8, f8, g8, h8
};
enum DIRECTION {
north = 8,
east = 1,
south = -north,
west = -east,
noWe = north + west,
noEa = north + east,
soWe = south + west,
soEa = south + east
};