did some stuff to make makefile work... it was

painful
This commit is contained in:
Karma Riuk
2025-02-02 16:26:46 +01:00
parent c83129a0d5
commit 0ae37a3eba
10 changed files with 29 additions and 27 deletions

77
cpp/src/board.cpp Normal file
View File

@ -0,0 +1,77 @@
#include "board.hpp"
#include <cctype>
#include <map>
Board* Board::setup_fen_position(std::string fen) {
Board* board = new Board();
std::map<char, Piece> c2p{
{'k', Piece::King},
{'p', Piece::Pawn},
{'n', Piece::Knigt},
{'b', Piece::Bishop},
{'r', Piece::Rook},
{'q', Piece::Queen},
};
std::string fen_board = fen.substr(0, fen.find(' '));
int rank = 7, file = 0;
for (char symbol : fen_board) {
if (symbol == '/') {
file = 0;
rank--;
continue;
}
if (std::isdigit(symbol))
file += symbol - '0';
else {
Colour colour =
std::isupper(symbol) ? Colour::White : Colour::Black;
Piece piece = c2p[std::tolower(symbol)];
board->squares[rank * 8 + file] = colour | piece;
file++;
}
}
return board;
}
std::string Board::to_fen() {
std::map<int, char> p2c{
{Piece::King, 'k'},
{Piece::Pawn, 'p'},
{Piece::Knigt, 'n'},
{Piece::Bishop, 'b'},
{Piece::Rook, 'r'},
{Piece::Queen, 'q'},
};
std::string ret;
for (int rank = 7; rank >= 0; rank--) {
int empty_cell_counter = 0;
for (int file = 0; file < 8; file++) {
if (this->squares[rank * 8 + file] == Piece::None) {
empty_cell_counter++;
continue;
}
int full_piece = this->squares[rank * 8 + file];
char piece = p2c[full_piece & 0b111];
Colour colour = (full_piece & 0b11000) == Colour::White
? Colour::White
: Colour::Black;
if (empty_cell_counter > 0) {
ret += std::to_string(empty_cell_counter);
empty_cell_counter = 0;
}
ret += colour == Colour::White ? std::toupper(piece) : piece;
}
if (empty_cell_counter > 0)
ret += std::to_string(empty_cell_counter);
if (rank > 0)
ret += "/";
}
return ret;
}

22
cpp/src/board.hpp Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include "piece.hpp"
#include <string>
enum CastleRights {
KingSide = 1,
QueenSide = 2,
};
class Board {
private:
int8_t squares[64] = {Piece::None};
Colour turn;
int8_t castle_rights;
public:
static Board* setup_fen_position(std::string fen);
std::string to_fen();
};

22
cpp/src/main.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "board.hpp"
#include <iostream>
int main(int argc, char* argv[]) {
std::string pos =
"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8";
Board* b = Board::setup_fen_position(pos);
std::cout << sizeof(Board) << std::endl;
std::cout << sizeof(int8_t[64]) << std::endl;
std::cout << sizeof(Colour) << std::endl;
std::string fen = b->to_fen();
std::cout << pos << std::endl;
std::cout << fen << std::endl;
std::cout << (fen.substr(0, fen.find(" ")) == pos.substr(0, pos.find(" ")))
<< std::endl;
return 0;
}

14
cpp/src/piece.hpp Normal file
View File

@ -0,0 +1,14 @@
enum Piece {
None = 0,
King = 1,
Pawn = 2,
Knigt = 3,
Bishop = 4,
Rook = 5,
Queen = 6,
};
enum Colour {
White = 8,
Black = 16,
};

0
cpp/src/stickfosh.cpp Normal file
View File