From e46c9bb21fc211973fac28e93ec9425ec549517e Mon Sep 17 00:00:00 2001 From: Mark <115177215+rektdie@users.noreply.github.com> Date: Mon, 26 Feb 2024 23:31:39 +0100 Subject: [PATCH] Files uploaded from project --- bitboards.cpp | 63 +++++++++++++++++ bitboards.h | 34 ++++++++++ board.cpp | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++ board.h | 21 ++++++ main.cpp | 13 ++++ types.h | 71 +++++++++++++++++++ 6 files changed, 386 insertions(+) create mode 100644 bitboards.cpp create mode 100644 bitboards.h create mode 100644 board.cpp create mode 100644 board.h create mode 100644 main.cpp create mode 100644 types.h diff --git a/bitboards.cpp b/bitboards.cpp new file mode 100644 index 0000000..0406a10 --- /dev/null +++ b/bitboards.cpp @@ -0,0 +1,63 @@ +#include +#include "bitboards.h" +#include + +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); +} \ No newline at end of file diff --git a/bitboards.h b/bitboards.h new file mode 100644 index 0000000..8592568 --- /dev/null +++ b/bitboards.h @@ -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~(); +}; \ No newline at end of file diff --git a/board.cpp b/board.cpp new file mode 100644 index 0000000..b3c4b75 --- /dev/null +++ b/board.cpp @@ -0,0 +1,184 @@ +#include "board.h" +#include "types.h" +#include "bitboards.h" +#include + +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(); + this->colors = std::array(); +} + +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]); +} \ No newline at end of file diff --git a/board.h b/board.h new file mode 100644 index 0000000..6d54671 --- /dev/null +++ b/board.h @@ -0,0 +1,21 @@ +#pragma once +#include "bitboards.h" +#include + +class Board { +public: + std::array pieces; + std::array colors; + Bitboard occupied; + + bool sideToMove = White; + int halfMoves = 0; + int fullMoves = 0; + + std::array castlingRights = { false, false, false, false }; + Bitboard enPassantTarget; + + void Init(); + void Reset(); + void SetByFen(const char* fen); +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..301eb5d --- /dev/null +++ b/main.cpp @@ -0,0 +1,13 @@ +#include +#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; +} \ No newline at end of file diff --git a/types.h b/types.h new file mode 100644 index 0000000..3474afd --- /dev/null +++ b/types.h @@ -0,0 +1,71 @@ +#pragma once +#include +#include +#include + +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 files { + 0x0101010101010101, + 0x2020202020202020, + 0x0404040404040404, + 0x0808080808080808, + 0x1010101010101010, + 0x2020202020202020, + 0x4040404040404040, + 0x8080808080808080 +}; + +constexpr std::array 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 +}; \ No newline at end of file