Files uploaded from project

This commit is contained in:
rektdie authored and GitHub committed 2024-02-26 23:31:39 +01:00
1 parent 274db1aefd
commit e46c9bb21f
6 files changed
+386

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
#include <iostream>
#include "bitboards.h"
#include <bitset>
void Bitboard::PrintBoard() const {
Bitboard mask = 0xff00000000000000;
for (int i = 0; i < 8; i++) {
std::bitset<8> currentRank = ((this->m_board & mask.m_board) >> (7 - i) * 8);
std::cout << "+---+---+---+---+---+---+---+---+\n";
std::cout << "| ";
for (int j = 0; j < 8; j++) {
std::cout << currentRank[j] << " | ";
}
std::cout << ' ' << 8 - i << '\n';
mask.m_board = (mask.m_board >> 8);
}
std::cout << "+---+---+---+---+---+---+---+---+\n";
std::cout << " a b c d e f g h\n\n";
}
bool Bitboard::IsSet(int square) const {
return (1ULL << square) & this->m_board;
}
// Operator overloading for easier comparisons
Bitboard Bitboard::operator&(const Bitboard& other) const {
return this->m_board & other.m_board;
}
Bitboard Bitboard::operator|(const Bitboard& other) const {
return this->m_board | other.m_board;
}
Bitboard Bitboard::operator^(const Bitboard& other) const {
return this->m_board ^ other.m_board;
}
Bitboard Bitboard::operator*(const Bitboard& other) const {
return this->m_board * other.m_board;
}
Bitboard Bitboard::operator&=(const Bitboard& other) {
return this->m_board &= other.m_board;
}
Bitboard Bitboard::operator|=(const Bitboard& other) {
return this->m_board |= other.m_board;
}
Bitboard Bitboard::operator^=(const Bitboard& other) {
return this->m_board ^= other.m_board;
}
Bitboard Bitboard::operator*=(const Bitboard& other) {
return this->m_board *= other.m_board;
}
Bitboard Bitboard::operator<<(int other) {
return this->m_board << other;
}
Bitboard Bitboard::operator>>(int other) {
return this->m_board >> other;
}
Bitboard Bitboard::operator~() {
return ~(this->m_board);
}
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include "types.h"
struct Bitboard {
private:
U64 m_board;
public:
Bitboard()
: m_board(0ULL) {}
Bitboard(const U64& board)
: m_board(board) {}
bool IsSet(int square) const;
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() {
this->SetByFen(StartingFen);
}
void Board::Reset() {
this->castlingRights = { false, false, false, false };
this->enPassantTarget = 0ULL;
this->halfMoves = 0;
this->fullMoves = 0;
this->sideToMove = White;
this->occupied = 0ULL;
std::cout << '\n';
this->pieces = std::array<Bitboard, 6>();
this->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++;
}
this->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;
bool 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);
};
+13
View File
@@ -0,0 +1,13 @@
#include <iostream>
#include "types.h"
#include "board.h"
int main() {
Board board;
board.Init();
// Printing the white pawns
(board.colors[White] & board.pieces[Pawn]).PrintBoard();
return 0;
}
+71
View File
@@ -0,0 +1,71 @@
#pragma once
#include <cstdint>
#include <array>
#include <string>
typedef uint64_t U64;
#define StartingFen "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 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,
0x2020202020202020,
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,
west = 1,
south = -north,
east = -west,
noWe = north + west,
noEa = north + east,
soWe = south + west,
soEa = south + east
};