105 Commits

Author SHA1 Message Date
97b3eeb984 first version of stickfosh, simply evaluates a
Some checks failed
tagged-release / Tagged Release (push) Has been cancelled
position based on the material (no AB-pruning)
2025-02-05 17:11:09 +01:00
982f933698 removed useless include 2025-02-05 17:11:00 +01:00
d21fcde21c renamed stickfosh to perft, it's more appropriate 2025-02-05 16:23:16 +01:00
a47dc9b695 Merge branch 'main' into opti-2-compacting-move
Some checks failed
pre-release / Pre Release (push) Waiting to run
tagged-release / Tagged Release (push) Has been cancelled
2025-02-05 15:37:57 +01:00
e8ca1db8d6 using the thread pool for each move in the top
Some checks failed
tagged-release / Tagged Release (push) Has been cancelled
level of the search (x4~ improvement)
2025-02-05 15:30:55 +01:00
73b2a5b51b created threadpool 2025-02-05 15:17:10 +01:00
3f3017a389 removed useless comments 2025-02-05 14:41:23 +01:00
f768d16a7e removed en passant from move 2025-02-05 14:41:16 +01:00
a658673d2b remove castle side from move 2025-02-05 14:36:00 +01:00
222adbb8f0 removed the is_capturing flag of move 2025-02-05 14:25:35 +01:00
c55645ad73 using global peft
Some checks failed
pre-release / Pre Release (push) Waiting to run
tagged-release / Tagged Release (push) Has been cancelled
2025-02-04 21:53:07 +01:00
1b0af3b486 annotated each depths with its time (to then
compare with the different optimizations)
2025-02-04 21:52:07 +01:00
df20060075 added global peft 2025-02-04 21:51:58 +01:00
151a50fba9 made move str better (includes promotion) 2025-02-04 21:51:14 +01:00
609d5f8c98 fixed castle right rook capture coords 2025-02-04 21:50:43 +01:00
e9c96e83da fixed en passant offset 2025-02-04 21:50:30 +01:00
76310400e8 added empty line before move counts to ease copying 2025-02-03 23:41:27 +01:00
e5bb2d1a5f printing moves normally 2025-02-03 23:41:16 +01:00
4c86d5e815 fixed castling rights on capture 2025-02-03 23:40:30 +01:00
b1812f1df6 removed the inclusion of move in piece 2025-02-03 19:10:49 +01:00
9d21141d3c fixed pawn capture + promotion action 2025-02-03 19:10:07 +01:00
974af5d19f we can now print a move (stockfish stile) 2025-02-03 19:09:21 +01:00
df07b4399d minor fix 2025-02-03 19:09:13 +01:00
fa59784230 fixed castling rights that were not removed
properly
2025-02-03 19:08:26 +01:00
be34045f92 minor fix 2025-02-03 19:08:21 +01:00
4eaed699c0 fixed en passant capture 2025-02-03 16:26:31 +01:00
310abb0611 fixed slight mistake 2025-02-03 16:12:20 +01:00
b70ca5302a put back good code 2025-02-03 15:36:11 +01:00
83af5f0b97 removed problemaic code 2025-02-03 15:35:43 +01:00
72ba6a80ae made types behave 2025-02-03 15:35:03 +01:00
1723356f62 removed useless stuff 2025-02-03 15:34:52 +01:00
75adb4b1ba removed already existing variable 2025-02-03 15:34:12 +01:00
6951b860da forgot to push the normal moves for the pawn, fixed it now 2025-02-03 14:05:32 +01:00
805d9fa95c fixed implicit type casting (warning) 2025-02-03 14:05:16 +01:00
4df7cbf01a fixed piece recognition 2025-02-03 14:04:52 +01:00
00137e022a fixed move for position initial guard 2025-02-03 14:04:27 +01:00
570b8df4a7 fixed look_direction 2025-02-03 14:04:18 +01:00
9a66a71c38 handling better the output of perft 2025-02-03 13:59:33 +01:00
41e4119468 reordered members of enum 2025-02-03 13:51:43 +01:00
e7fc1b06de fixed the is_within_bounds check 2025-02-03 13:50:48 +01:00
5f79b81ce4 added validity check when creating coords from index 2025-02-03 13:50:34 +01:00
e5819ee83b minor fix 2025-02-03 13:50:15 +01:00
58109c5120 fixed condition 2025-02-03 13:49:40 +01:00
6478176a7d fixed check condition 2025-02-03 13:47:27 +01:00
ab088da33c removed useless this-> 2025-02-03 13:47:17 +01:00
31799cb0ec added optimization and warning flags to the compiler 2025-02-03 13:44:53 +01:00
96dcbc0a6c actually counting milliseconds now 2025-02-03 13:41:45 +01:00
ce06097063 i forgot to actually implement the root legal_moves haha 2025-02-03 01:12:30 +01:00
3fc2aa73ad actually counting milliseconds now 2025-02-03 01:06:07 +01:00
de6a03ce08 fixed mistake 2025-02-03 01:05:59 +01:00
dc3031ddc1 we can finally print pieces and colours properly 2025-02-03 01:05:37 +01:00
5e4b880aad checking perft to see if my cpp implementation is
correct
2025-02-03 00:33:12 +01:00
540ffa4fb3 fixed slight mistake 2025-02-03 00:33:04 +01:00
21136a26f0 implemented the last details to make everything
work (hopefully)
2025-02-03 00:08:37 +01:00
fa57dcfc30 apparently the default value of a paramter goes in
the declaration and not the defintion, i didn't know that
2025-02-03 00:08:13 +01:00
8da349579d make_move now checks castle rights after each move 2025-02-02 23:52:24 +01:00
f0467bc516 make_move now handles castling 2025-02-02 23:34:10 +01:00
a7cf8e0c21 make_move now handles the setting of en passant
targets
2025-02-02 23:33:52 +01:00
2128159914 make_move now handles promotion 2025-02-02 23:33:29 +01:00
c564add509 removed useless variable 2025-02-02 23:33:20 +01:00
31b0656332 fixed promotion missing colour 2025-02-02 23:16:53 +01:00
24165bb5bb made comment more accurate 2025-02-02 23:14:13 +01:00
4c8fdfa3b4 implemented en passant in legal moves 2025-02-02 23:13:37 +01:00
84c3af2bb1 fully support FEN strings now
Some checks failed
pre-release / Pre Release (push) Has been cancelled
tagged-release / Tagged Release (push) Has been cancelled
2025-02-02 23:05:42 +01:00
22acfc5027 fixed slight bug 2025-02-02 23:05:26 +01:00
1ff4b6b1f4 implemented the en passant target support for FEN
strings
2025-02-02 22:52:46 +01:00
2493892b05 added algebraic support for coordinates 2025-02-02 22:39:06 +01:00
bdd8011577 now support the castling sides of the FEN string 2025-02-02 22:05:33 +01:00
5549c77177 updated testing library 2025-02-02 22:04:41 +01:00
be016dcbcc fixed type mismatch 2025-02-02 22:04:31 +01:00
efd7bf6794 gave default values to the board fields because
they were causing undefinied behaviour
2025-02-02 22:04:04 +01:00
81b24d3082 extended FEN support to include who's turn it is 2025-02-02 21:39:33 +01:00
497da2de8e updated assert equals to fail fast 2025-02-02 21:39:02 +01:00
0574a11b32 fixed minor bug 2025-02-02 21:37:43 +01:00
472f9e4c7c made CastleSide a normal enum again to use &
Some checks failed
tagged-release / Tagged Release (push) Has been cancelled
2025-02-02 21:30:37 +01:00
f5292fa6d7 added small comment 2025-02-02 21:22:38 +01:00
8a3a92f80f minor change 2025-02-02 21:14:31 +01:00
b7ad7b3111 implemented king legal moves 2025-02-02 21:14:14 +01:00
b329c41bea got rid of magic numbers 2025-02-02 21:13:30 +01:00
1625345f08 fixed board.cpp 2025-02-02 20:28:50 +01:00
a790677bb7 implemented queen legal moves 2025-02-02 20:28:41 +01:00
703dcef59f implemented knight legal moves 2025-02-02 20:24:42 +01:00
d74222c2f4 implemented rook legal moves 2025-02-02 20:19:07 +01:00
01c912435b implemented bishop legal moves 2025-02-02 20:17:36 +01:00
8bf164cb05 extracted coords to its own hpp 2025-02-02 20:17:27 +01:00
e08dbb913e fixed board and pawn moves 2025-02-02 19:49:21 +01:00
fea3f6c98a fixed signature 2025-02-02 18:24:23 +01:00
da55f0085f implemented (partially) the pawn move 2025-02-02 18:17:37 +01:00
95327ec653 cleaned up main 2025-02-02 18:17:19 +01:00
1a4e33201e when loading a fen ref, i return the object and
not a pointer
2025-02-02 18:17:02 +01:00
d6baf1ee53 added main to gitignore 2025-02-02 18:16:45 +01:00
67e377bfde made some seperation of concerns 2025-02-02 17:16:37 +01:00
29453fbb14 implemented some stuff 2025-02-02 17:13:55 +01:00
585e392b6a created move struct 2025-02-02 16:49:24 +01:00
d436c5a032 made stuff a tiny bit more compact 2025-02-02 16:49:10 +01:00
08c0a3b50b added test support to the makefile 2025-02-02 16:37:59 +01:00
0ae37a3eba did some stuff to make makefile work... it was
painful
2025-02-02 16:26:46 +01:00
c83129a0d5 added cpp gitignore 2025-02-02 15:31:45 +01:00
acfa27c83e removed binary files 2025-02-02 15:21:04 +01:00
85a5bfa328 moved some stuff around 2025-02-02 15:15:14 +01:00
139a5c7d8f fixed missing imports
Some checks are pending
pre-release / Pre Release (push) Waiting to run
2025-02-02 14:54:17 +01:00
3a2988d351 fixed permissions of fields 2025-02-02 14:53:17 +01:00
c758d1854f removed useless imports 2025-02-02 14:52:47 +01:00
53a0755547 extracted the assert equals to another file to
have mulitple test files
2025-02-02 14:52:13 +01:00
4792daf127 cpp board can go back and forth from FEN
Some checks failed
pre-release / Pre Release (push) Waiting to run
tagged-release / Tagged Release (push) Has been cancelled
2025-02-02 14:50:44 +01:00
25 changed files with 1441 additions and 1 deletions

2
.gitignore vendored
View File

@ -171,4 +171,4 @@ cython_debug/
.ruff_cache/
# PyPI configuration file
.pypirc
.pypirc

34
cpp/.gitignore vendored Normal file
View File

@ -0,0 +1,34 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.appobj/
test_bin/
main

58
cpp/Makefile Normal file
View File

@ -0,0 +1,58 @@
CXXFLAGS += -O3 -Wall
# Add .d to Make's recognized suffixes.
SUFFIXES += .d
#We don't need to clean up when we're making these targets
NODEPS:=clean tags svn
#Find all the C++ files in the src/ directory
SOURCES:=$(shell find src/ -name "*.cpp")
OBJFILES := $(patsubst src/%.cpp,obj/%.o,$(SOURCES))
#These are the dependency files, which make will clean up after it creates them
DEPFILES:=$(patsubst %.cpp,%.d,$(SOURCES))
#Don't create dependencies when we're cleaning, for instance
ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))
#Chances are, these files don't exist. GMake will create them and
#clean up automatically afterwards
-include $(DEPFILES)
endif
#This is the rule for creating the dependency files
src/%.d: src/%.cpp
$(CXX) $(CXXFLAGS) -MM -MT '$(patsubst src/%.cpp,obj/%.o,$<)' $< -MF $@
#This rule does the compilation
obj/%.o:
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -o $@ -c $<
main: $(OBJFILES)
$(CXX) $(LDFLAGS) $(OBJFILES) $(LOADLIBES) $(LDLIBS) -o main
clean:
rm -rf obj/* $(DEPFILES) test_bin/
# --- Test Support ---
# Find all test source files in the tests directory
TESTS := $(shell find tests -name "*.cpp")
# Define corresponding test executable names, e.g. tests/foo.cpp -> test_bin/foo
TEST_BIN := $(patsubst tests/%.cpp,test_bin/%,$(TESTS))
LIBS := $(filter-out obj/main.o,$(OBJFILES))
# Pattern rule: how to build a test executable from a test source file.
# You can adjust CXXFLAGS or add include directories if needed.
test_bin/%: tests/%.cpp $(LIBS)
@echo $(LIBS)
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -o $@ $< $(LIBS)
# The 'test' target builds all tests and then runs each one.
.PHONY: test
test: $(TEST_BIN)
@echo "Running all tests..."
@for t in $(TEST_BIN); do \
echo "---- Running $$t ----"; \
./$$t || exit 1; \
done

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

@ -0,0 +1,343 @@
#include "board.hpp"
#include "coords.hpp"
#include "move.hpp"
#include "pieces/piece.hpp"
#include <algorithm>
#include <cctype>
#include <map>
#include <stdexcept>
Board Board::setup_fen_position(std::string fen) {
Board board;
std::map<char, Piece> c2p{
{'k', Piece::King},
{'p', Piece::Pawn},
{'n', Piece::Knigt},
{'b', Piece::Bishop},
{'r', Piece::Rook},
{'q', Piece::Queen},
};
// -- Pieces
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++;
}
}
// -- Active colour
int index = fen.find(' ');
index++;
board.white_to_play = fen[index] == 'w';
// Castling Rights
index += 2;
for (char symbol : fen.substr(index, fen.find(' ', index + 1))) {
index++;
if (symbol == ' ' || symbol == '-') {
if (symbol == '-')
index++;
break;
}
switch (symbol) {
case 'K':
board.w_castle_rights |= CastleSide::KingSide;
break;
case 'Q':
board.w_castle_rights |= CastleSide::QueenSide;
break;
case 'k':
board.b_castle_rights |= CastleSide::KingSide;
break;
case 'q':
board.b_castle_rights |= CastleSide::QueenSide;
break;
}
}
// -- En passant target
if (fen[index] != '-') {
Coords c = Coords::from_algebraic(fen.substr(index, 2));
index += 2;
board.en_passant_target = c.to_index();
} else {
index++;
}
// -- Half move clock
index = fen.find(' ', index) + 1;
board.n_half_moves =
std::stoi(fen.substr(index, fen.find(' ', index + 1) - index));
// -- Full move number
index = fen.find(' ', index) + 1;
board.n_full_moves = std::stoi(fen.substr(index));
return board;
}
std::string Board::to_fen() const {
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;
// -- Pieces
for (int rank = 7; rank >= 0; rank--) {
int empty_cell_counter = 0;
for (int file = 0; file < 8; file++) {
if (squares[rank * 8 + file] == Piece::None) {
empty_cell_counter++;
continue;
}
int full_piece = squares[rank * 8 + file];
char piece = p2c[full_piece & 0b111];
int8_t colour = colour_at({file, rank});
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 += "/";
}
ret += " ";
// -- Active colour
ret += white_to_play ? 'w' : 'b';
ret += " ";
// -- Castling Rights
if (w_castle_rights == CastleSide::NeitherSide
&& b_castle_rights == CastleSide::NeitherSide)
ret += '-';
else {
if (w_castle_rights & CastleSide::KingSide)
ret += 'K';
if (w_castle_rights & CastleSide::QueenSide)
ret += 'Q';
if (b_castle_rights & CastleSide::KingSide)
ret += 'k';
if (b_castle_rights & CastleSide::QueenSide)
ret += 'q';
}
ret += ' ';
// -- En passant target
ret += en_passant_target == -1
? "-"
: Coords::from_index(en_passant_target).to_algebraic();
ret += ' ';
// -- Half move clock
ret += std::to_string(n_half_moves);
ret += ' ';
// -- Full moves number
ret += std::to_string(n_full_moves);
return ret;
}
Board Board::make_move(Move move) const {
Board ret;
std::copy(
std::begin(this->squares),
std::end(this->squares),
std::begin(ret.squares)
);
ret.white_to_play = !this->white_to_play;
// -- Actually make the move
ret.squares[move.source_square] = Piece::None;
ret.squares[move.target_square] = this->squares[move.source_square];
// -- Handle en passant target being eaten
if (en_passant_target != -1
&& (squares[move.source_square] & 0b111) == Piece::Pawn
&& squares[move.target_square] == Piece::None)
ret.squares[move.target_square + (white_to_play ? -8 : 8)] =
Piece::None;
// -- Handle promotion
if (move.promoting_to != Piece::None)
ret.squares[move.target_square] = move.promoting_to;
// -- Set en passant target if need
if ((squares[move.source_square] & 0b111) == Piece::Pawn
&& std::abs(move.target_square - move.source_square) == 16) {
if (white_to_play)
ret.en_passant_target = move.target_square - 8;
else
ret.en_passant_target = move.target_square + 8;
} else {
ret.en_passant_target = -1;
}
// -- Handle castling (just move the rook over)
Coords c = Coords::from_index(move.source_square);
if ((squares[move.source_square] & 0b111) == Piece::King) {
if (move.target_square - move.source_square == 2) { // king side castle
Coords rook_source{7, c.y};
int8_t old_rook = ret.squares[rook_source.to_index()];
ret.squares[rook_source.to_index()] = Piece::None;
Coords rook_dest{5, c.y};
ret.squares[rook_dest.to_index()] = old_rook;
} else if (move.target_square - move.source_square == -2) { // queen
Coords rook_source{0, c.y};
int8_t old_rook = ret.squares[rook_source.to_index()];
ret.squares[rook_source.to_index()] = Piece::None;
Coords rook_dest{3, c.y};
ret.squares[rook_dest.to_index()] = old_rook;
}
}
// -- Check for castling rights
ret.w_castle_rights = w_castle_rights;
ret.b_castle_rights = b_castle_rights;
bool is_capturing = squares[move.target_square] != Piece::None;
if (white_to_play) {
if ((squares[move.source_square] & 0b111) == King)
ret.w_castle_rights = NeitherSide;
if ((squares[move.source_square] & 0b111) == Rook) {
if (c.x == 0 && (ret.w_castle_rights & QueenSide))
ret.w_castle_rights &= ~(QueenSide);
if (c.x == 7 && (ret.w_castle_rights & KingSide))
ret.w_castle_rights &= ~(KingSide);
}
Coords target = Coords::from_index(move.target_square);
if (is_capturing && target.y == 7
&& (squares[move.target_square] & 0b111) == Rook) {
if (target.x == 0 && (ret.b_castle_rights & QueenSide))
ret.b_castle_rights &= ~(QueenSide);
if (target.x == 7 && (ret.b_castle_rights & KingSide))
ret.b_castle_rights &= ~(KingSide);
}
} else {
if ((squares[move.source_square] & 0b111) == King)
ret.b_castle_rights = NeitherSide;
if ((squares[move.source_square] & 0b111) == Rook) {
if (c.x == 0 && (ret.b_castle_rights & QueenSide))
ret.b_castle_rights &= ~(QueenSide);
if (c.x == 7 && (ret.b_castle_rights & KingSide))
ret.b_castle_rights &= ~(KingSide);
}
Coords target = Coords::from_index(move.target_square);
if (is_capturing && target.y == 0
&& (squares[move.target_square] & 0b111) == Rook) {
if (target.x == 0 && (ret.w_castle_rights & QueenSide))
ret.w_castle_rights &= ~(QueenSide);
if (target.x == 7 && (ret.w_castle_rights & KingSide))
ret.w_castle_rights &= ~(KingSide);
}
}
return ret;
}
int8_t Board::get_king_of(int8_t colour) const {
for (int i = 0; i < 64; i++)
if (squares[i] == (colour | Piece::King))
return i;
throw std::domain_error(
"Apparently there no kings of the such color in this board"
);
}
std::vector<int8_t> to_target_square(std::vector<Move> moves) {
std::vector<int8_t> ret;
for (Move move : moves)
ret.push_back(move.target_square);
return ret;
}
bool Board::is_check_for(int8_t colour) const {
int8_t king_idx = this->get_king_of(colour);
for (int i = 0; i < 64; i++) {
if (this->squares[i] == Piece::None || colour_at(i) == colour)
continue;
std::vector<int8_t> targets;
if ((squares[i] & 0b00111) == King) {
// special case for the king, because it creates infinite recursion
// (since he looks if he's walking into a check)
Coords king_pos = Coords::from_index(i);
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
Coords c{king_pos.x + dx, king_pos.y + dy};
if (c.is_within_bounds())
targets.push_back(c.to_index());
}
}
} else {
std::vector<Move> moves = legal_moves(
this->squares[i],
*this,
Coords::from_index(i),
true
);
targets = to_target_square(moves);
}
if (std::find(targets.begin(), targets.end(), king_idx)
!= targets.end())
return true;
}
return false;
}
bool Board::no_legal_moves_for(int8_t colour) const {
for (int i = 0; i < 64; i++) {
if (colour_at(i) == colour) {
std::vector<Move> moves =
legal_moves(squares[i], *this, Coords::from_index(i));
if (moves.size() > 0)
return false;
}
}
return true;
}
std::vector<Move> Board::all_legal_moves() const {
std::vector<Move> ret;
for (int i = 0; i < 64; i++) {
if ((colour_at(i) == White && white_to_play)
|| (colour_at(i) == Black && !white_to_play)) {
std::vector<Move> moves =
legal_moves(squares[i], *this, Coords::from_index(i));
ret.insert(ret.end(), moves.begin(), moves.end());
}
}
return ret;
}

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

@ -0,0 +1,51 @@
#pragma once
#include "coords.hpp"
#include "move.hpp"
#include "pieces/piece.hpp"
#include <string>
struct Board {
private:
int8_t get_king_of(int8_t) const;
bool no_legal_moves_for(int8_t) const;
public:
int8_t squares[64] = {Piece::None};
bool white_to_play = true;
int8_t w_castle_rights = CastleSide::NeitherSide;
int8_t b_castle_rights = CastleSide::NeitherSide;
int8_t en_passant_target = -1;
uint8_t n_half_moves = 0;
uint8_t n_full_moves = 0;
static Board setup_fen_position(std::string fen);
Board make_move(Move) const;
std::string to_fen() const;
bool is_check_for(int8_t) const;
std::vector<Move> all_legal_moves() const;
bool is_checkmate_for(int8_t colour) const {
return is_check_for(colour) && no_legal_moves_for(colour);
}
bool is_stalemate_for(int8_t colour) const {
return !is_check_for(colour) && no_legal_moves_for(colour);
}
bool is_terminal() const {
return is_checkmate_for(White) || is_checkmate_for(Black)
|| is_stalemate_for(White) || is_stalemate_for(Black);
}
int8_t colour_at(int8_t idx) const {
return squares[idx] & 0b11000;
}
int8_t colour_at(Coords xy) const {
return colour_at(xy.to_index());
}
};

7
cpp/src/castle_side.hpp Normal file
View File

@ -0,0 +1,7 @@
#include <cstdint>
enum CastleSide : int8_t {
NeitherSide = 0,
KingSide = 1,
QueenSide = 2,
};

71
cpp/src/coords.hpp Normal file
View File

@ -0,0 +1,71 @@
#pragma once
#include <iostream>
#include <stdexcept>
#include <string>
static std::string _FILES = "abcdefgh";
static std::string _RANKS = "12345678";
struct Coords {
int x, y;
Coords(int x, int y): x(x), y(y) {}
int8_t to_index() const {
return this->y * 8 + this->x;
}
static Coords from_index(int idx) {
if (idx < 0 || idx > 63)
throw std::invalid_argument("The index is outside the board...");
return {idx % 8, idx / 8};
}
static Coords from_algebraic(std::string pos) {
if (pos.size() != 2)
throw std::invalid_argument(
"An algebraic coordinate should only have two characters"
);
size_t x = _FILES.find(pos[0]);
if (x == std::string::npos)
throw std::invalid_argument("The first character of the given "
"algebraic coordinate is invalid");
size_t y = _RANKS.find(pos[1]);
if (y == std::string::npos)
throw std::invalid_argument("The second character of the given "
"algebraic coordinate is invalid");
return Coords{(int) x, (int) y};
}
std::string to_algebraic() const {
std::string ret;
if (x > 7 || y > 7)
throw std::invalid_argument(
"Can't give the algebraic vesion of an invalid coord"
);
ret += _FILES[x];
ret += _RANKS[y];
return ret;
}
bool is_within_bounds() const {
return 0 <= x && x < 8 && 0 <= y && y < 8;
}
};
inline bool operator==(const Coords& a, const Coords& b) {
return a.x == b.x && a.y == b.y;
}
inline bool operator!=(const Coords& a, const Coords& b) {
return !(a == b);
}
inline std::ostream& operator<<(std::ostream& os, const Coords& coords) {
os << coords.to_algebraic();
return os;
}

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

@ -0,0 +1,11 @@
#include "stickfosh.hpp"
#include <iostream>
int main(int argc, char* argv[]) {
std::string pos =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
std::string move = search(pos, 4);
std::cout << move << std::endl;
return 0;
}

45
cpp/src/move.hpp Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include "castle_side.hpp"
#include "coords.hpp"
#include "pieces/piece.hpp"
#include <cstdint>
#include <sstream>
struct Move {
int8_t source_square;
int8_t target_square;
int8_t promoting_to = Piece::None;
std::string to_string() const {
std::stringstream ss;
ss << Coords::from_index(source_square)
<< Coords::from_index(target_square);
if (promoting_to != Piece::None) {
switch (promoting_to & 0b00111) {
case Queen:
ss << 'q';
break;
case Bishop:
ss << 'b';
break;
case Knigt:
ss << 'n';
break;
case Rook:
ss << 'r';
break;
default:
break;
}
}
return ss.str();
}
};
inline std::ostream& operator<<(std::ostream& os, const Move& m) {
os << m.to_string();
return os;
}

187
cpp/src/perft.cpp Normal file
View File

@ -0,0 +1,187 @@
#include "perft.hpp"
#include "board.hpp"
#include "move.hpp"
#include "threadpool.hpp"
#include <chrono>
#include <map>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>
static std::string tick = "\033[92m✔\033[0m"; // Green Tick;
static std::string cross = "\033[91m❌\033[0m"; // Red Cross
static std::map<std::string, std::map<int, int>> pos2expected{
// -- Position 1
{
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
{
{1, 20}, // 0
{2, 400}, // 1
{3, 8902}, // 38
{4, 197281}, // 971
// {5, 4865609}, // 23032
},
},
// -- Position 2
{
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 2",
{
{1, 48}, // 0
{2, 2039}, // 16
{3, 97862}, // 602
// {4, 4085603}, // 26612
},
},
// -- Position 3
{
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 3",
{
{1, 14}, // 0
{2, 191}, // 1
{3, 2812}, // 11
{4, 43238}, // 157
{5, 674624}, // 2199
// {6, 11030083},
},
},
// -- Position 4a
{
"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 4",
{
{1, 6}, // 0
{2, 264}, // 1
{3, 9467}, // 69
{4, 422333}, // 3085
// {5, 15833292}, // 124452
},
},
// -- Position 4b
{
"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5",
{
{1, 6}, // 0
{2, 264}, // 2
{3, 9467}, // 104
{4, 422333}, // 3742
// {5, 15833292}, // 136784
},
},
// -- Position 5
{
"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 6",
{
{1, 44}, // 0
{2, 1486}, // 12
{3, 62379}, // 357
// {4, 2103487}, // 13804
// {5, 89941194}, // 1230428
},
},
// -- Position 6
{
"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 "
"7",
{
{1, 46}, // 0
{2, 2079}, // 16
{3, 89890}, // 602
// {4, 3894594}, // 26612
// {5, 164075551}, // 1230428
},
},
};
static std::stringstream res;
int move_generation_test(
const Board& b, int depth, int max_depth, ThreadPool& pool
) {
if (depth == max_depth) {
res.str("");
res.clear();
}
if (b.is_terminal())
return 0;
if (depth == 0)
return 1;
std::vector<Move> moves = b.all_legal_moves();
if (depth == 1)
return moves.size();
int num_pos = 0;
if (depth == max_depth) {
// Parallel execution at the top level
std::vector<std::future<int>> futures;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
futures.push_back(pool.enqueue(
move_generation_test,
tmp_board,
depth - 1,
max_depth,
std::ref(pool)
));
}
for (auto& future : futures)
num_pos += future.get(); // Retrieve the result of each task
} else {
// Regular sequential execution
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
int n = move_generation_test(tmp_board, depth - 1, max_depth, pool);
if (depth == max_depth)
res << move << ": " << n << std::endl;
num_pos += n;
}
}
return num_pos;
}
void perft() {
for (auto& [key, value] : pos2expected)
perft(key);
}
void perft(std::string pos) {
std::cout << pos << std::endl;
std::map<int, int> expected = pos2expected[pos];
Board b = Board::setup_fen_position(pos);
ThreadPool pool(std::thread::hardware_concurrency());
for (const auto& [depth, expected_n_moves] : expected) {
std::cout << "Depth: " << depth << " " << std::flush;
auto start = std::chrono::steady_clock::now();
int moves = move_generation_test(b, depth, depth, pool);
auto end = std::chrono::steady_clock::now();
auto elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
.count();
std::cout << "Results: " << moves << " ";
if (moves == expected_n_moves)
std::cout << tick << " ";
else
std::cout << cross << " (expected " << expected_n_moves << ") ";
std::cout << "positions Time: " << elapsed << " milliseconds"
<< std::endl;
if (moves != expected_n_moves) {
std::cout << std::endl << res.str();
exit(1);
}
}
}

6
cpp/src/perft.hpp Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <string>
void perft(std::string pos);
void perft();

23
cpp/src/pieces/bishop.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "../board.hpp"
#include "../coords.hpp"
#include "../move.hpp"
#include "piece.hpp"
#include <vector>
std::vector<Move> bishop_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
auto ne = look_direction(b, xy, 1, 1);
ret.insert(ret.end(), ne.begin(), ne.end());
auto se = look_direction(b, xy, 1, -1);
ret.insert(ret.end(), se.begin(), se.end());
auto sw = look_direction(b, xy, -1, -1);
ret.insert(ret.end(), sw.begin(), sw.end());
auto nw = look_direction(b, xy, -1, 1);
ret.insert(ret.end(), nw.begin(), nw.end());
return ret;
}

74
cpp/src/pieces/king.cpp Normal file
View File

@ -0,0 +1,74 @@
#include "../board.hpp"
#include "../coords.hpp"
#include "piece.hpp"
static bool is_clear_king_side(const Board& b, const Coords xy) {
for (int dx = 1; dx < 3; dx++) {
Coords c{xy.x + dx, xy.y};
if (b.squares[c.to_index()] != Piece::None)
return false;
std::optional<Move> move = move_for_position(b, xy, c);
Board board_after_move = b.make_move(move.value());
if (board_after_move.is_check_for(b.colour_at(xy)))
return false;
}
return true;
}
static bool is_clear_queen_side(const Board& b, const Coords xy) {
for (int dx = 1; dx < 4; dx++) {
Coords c{xy.x - dx, xy.y};
if (b.squares[c.to_index()] != Piece::None)
return false;
std::optional<Move> move = move_for_position(b, xy, c);
Board board_after_move = b.make_move(move.value());
if (dx < 3 && board_after_move.is_check_for(b.colour_at(xy)))
return false;
}
return true;
}
std::vector<Move> king_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
// -- Regular moves
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) // skip staying in the same position
continue;
Coords c{xy.x + dx, xy.y + dy};
std::optional<Move> move = move_for_position(b, xy, c);
if (move.has_value())
ret.push_back(move.value());
}
}
if (b.is_check_for(b.colour_at(xy)))
return keep_only_blocking(ret, b);
// -- Castles
int8_t castling_rights = b.colour_at(xy) == Colour::White
? b.w_castle_rights
: b.b_castle_rights;
if (castling_rights == CastleSide::NeitherSide)
return ret;
if (castling_rights & CastleSide::KingSide && is_clear_king_side(b, xy)) {
ret.push_back(Move{
xy.to_index(),
Coords{6, xy.y}.to_index(),
});
}
if (castling_rights & CastleSide::QueenSide && is_clear_queen_side(b, xy)) {
ret.push_back(Move{
xy.to_index(),
Coords{2, xy.y}.to_index(),
});
}
return ret;
}

28
cpp/src/pieces/knight.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "../board.hpp"
#include "../coords.hpp"
#include "../move.hpp"
#include "piece.hpp"
#include <vector>
std::vector<Move> knight_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
std::vector<std::pair<int, int>> moves = {
{+2, +1},
{+1, +2}, // north east
{+2, -1},
{+1, -2}, // south east
{-2, -1},
{-1, -2}, // south west
{-2, +1},
{-1, +2} // north west
};
for (const auto& [dx, dy] : moves) {
std::optional<Move> move =
move_for_position(b, xy, Coords{xy.x + dx, xy.y + dy});
if (move.has_value())
ret.push_back(move.value());
}
return ret;
}

84
cpp/src/pieces/pawn.cpp Normal file
View File

@ -0,0 +1,84 @@
#include "../board.hpp"
#include "../coords.hpp"
#include "../move.hpp"
#include "piece.hpp"
std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
std::vector<Move> ret{};
int8_t my_colour = b.colour_at(xy);
// -- Capture to the left
if (xy.x > 0) {
int dy = my_colour == Colour::White ? 1 : -1;
Coords left{xy.x - 1, xy.y + dy};
int8_t capturable_piece = b.squares[left.to_index()];
if (capturable_piece != 0) {
if (my_colour != b.colour_at(left))
if ((my_colour == White && left.y == 7)
|| (my_colour == Black && left.y == 0))
for (auto piece : {Rook, Knigt, Bishop, Queen})
ret.push_back(Move{
xy.to_index(),
left.to_index(),
.promoting_to = (int8_t) (my_colour | piece)
});
else
ret.push_back(Move{xy.to_index(), left.to_index()});
}
}
// -- Capture to the right
if (xy.x < 7) {
int dy = my_colour == Colour::White ? 1 : -1;
Coords right{xy.x + 1, xy.y + dy};
int8_t capturable_piece = b.squares[right.to_index()];
if (capturable_piece != 0) {
if (my_colour != b.colour_at(right))
if ((my_colour == White && right.y == 7)
|| (my_colour == Black && right.y == 0))
for (auto piece : {Rook, Knigt, Bishop, Queen})
ret.push_back(Move{
xy.to_index(),
right.to_index(),
.promoting_to = (int8_t) (my_colour | piece)
});
else
ret.push_back(Move{xy.to_index(), right.to_index()});
}
}
// -- Capture en passant
if (b.en_passant_target != -1) {
Coords c = Coords::from_index(b.en_passant_target);
int dy = my_colour == Colour::White ? 1 : -1;
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 1))
ret.push_back(Move{xy.to_index(), c.to_index()});
}
// -- Normal move + promotion
bool is_on_starting_rank =
my_colour == Colour::White ? xy.y == 1 : xy.y == 6;
int max_dy = is_on_starting_rank ? 3 : 2;
for (int dy = 1; dy < max_dy; dy++) {
int actual_dy = my_colour == Colour::White ? dy : -dy;
Coords new_xy{xy.x, xy.y + actual_dy};
if (b.squares[new_xy.to_index()] != Piece::None)
break;
if (new_xy.y == 7 || new_xy.y == 0)
for (auto piece : {Rook, Knigt, Bishop, Queen})
ret.push_back(Move{
xy.to_index(),
new_xy.to_index(),
.promoting_to = (int8_t) (my_colour | piece)
});
else
ret.push_back(Move{
xy.to_index(),
new_xy.to_index(),
});
}
return ret;
}

81
cpp/src/pieces/piece.cpp Normal file
View File

@ -0,0 +1,81 @@
#include "piece.hpp"
#include "../board.hpp"
#include "../coords.hpp"
std::vector<Move>
keep_only_blocking(const std::vector<Move> candidates, const Board& board) {
if (candidates.size() == 0)
return {};
int8_t my_colour = board.colour_at(candidates[0].source_square);
std::vector<Move> ret;
for (Move move : candidates) {
Board board_after_move = board.make_move(move);
if (!board_after_move.is_check_for(my_colour))
ret.push_back(move);
}
return ret;
}
std::vector<Move>
legal_moves(int8_t p, const Board& b, const Coords xy, bool looking_for_check) {
std::vector<Move> ret;
int8_t simple_piece = p & 0b00111;
switch (simple_piece) {
case Piece::Pawn:
ret = pawn_moves(b, xy);
break;
case Piece::Bishop:
ret = bishop_moves(b, xy);
break;
case Piece::Rook:
ret = rook_moves(b, xy);
break;
case Piece::Knigt:
ret = knight_moves(b, xy);
break;
case Piece::Queen:
ret = queen_moves(b, xy);
break;
case Piece::King:
ret = king_moves(b, xy);
break;
default:
break;
}
if (!looking_for_check)
return keep_only_blocking(ret, b);
return ret;
}
std::optional<Move>
move_for_position(const Board& board, const Coords source, const Coords dest) {
if (!dest.is_within_bounds()
|| board.colour_at(source) == board.colour_at(dest))
return {};
return Move{source.to_index(), dest.to_index()};
}
std::vector<Move>
look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
std::vector<Move> ret;
for (int d = 1; d < 8; d++) {
int dx = mult_dx * d;
int dy = mult_dy * d;
Coords target{xy.x + dx, xy.y + dy};
std::optional<Move> move = move_for_position(board, xy, target);
if (move.has_value()) {
ret.push_back(move.value());
if (board.squares[target.to_index()] != Piece::None)
break;
} else {
break;
}
}
return ret;
}

42
cpp/src/pieces/piece.hpp Normal file
View File

@ -0,0 +1,42 @@
#pragma once
#include <cstdint>
#include <optional>
#include <ostream>
#include <vector>
enum Piece : int8_t {
None = 0,
Rook = 1,
Knigt = 2,
Bishop = 3,
Queen = 4,
King = 5,
Pawn = 6,
};
enum Colour : int8_t {
White = 8,
Black = 16,
};
inline std::ostream& operator<<(std::ostream& os, const int8_t& i) {
os << std::to_string(i);
return os;
}
class Board;
struct Coords;
struct Move;
std::vector<Move> legal_moves(int8_t, const Board&, const Coords, bool = false);
std::vector<Move> keep_only_blocking(const std::vector<Move>, const Board&);
std::optional<Move> move_for_position(const Board&, const Coords, const Coords);
std::vector<Move> look_direction(const Board&, const Coords, int, int);
std::vector<Move> pawn_moves(const Board&, const Coords);
std::vector<Move> rook_moves(const Board&, const Coords);
std::vector<Move> knight_moves(const Board&, const Coords);
std::vector<Move> bishop_moves(const Board&, const Coords);
std::vector<Move> queen_moves(const Board&, const Coords);
std::vector<Move> king_moves(const Board&, const Coords);

35
cpp/src/pieces/queen.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "../board.hpp"
#include "../coords.hpp"
#include "../move.hpp"
#include "piece.hpp"
#include <vector>
std::vector<Move> queen_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
auto e = look_direction(b, xy, 1, 0);
ret.insert(ret.end(), e.begin(), e.end());
auto s = look_direction(b, xy, 0, -1);
ret.insert(ret.end(), s.begin(), s.end());
auto w = look_direction(b, xy, -1, 0);
ret.insert(ret.end(), w.begin(), w.end());
auto n = look_direction(b, xy, 0, 1);
ret.insert(ret.end(), n.begin(), n.end());
auto ne = look_direction(b, xy, 1, 1);
ret.insert(ret.end(), ne.begin(), ne.end());
auto se = look_direction(b, xy, 1, -1);
ret.insert(ret.end(), se.begin(), se.end());
auto sw = look_direction(b, xy, -1, -1);
ret.insert(ret.end(), sw.begin(), sw.end());
auto nw = look_direction(b, xy, -1, 1);
ret.insert(ret.end(), nw.begin(), nw.end());
return ret;
}

23
cpp/src/pieces/rook.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "../board.hpp"
#include "../coords.hpp"
#include "../move.hpp"
#include "piece.hpp"
#include <vector>
std::vector<Move> rook_moves(const Board& b, const Coords xy) {
std::vector<Move> ret;
auto e = look_direction(b, xy, 1, 0);
ret.insert(ret.end(), e.begin(), e.end());
auto s = look_direction(b, xy, 0, -1);
ret.insert(ret.end(), s.begin(), s.end());
auto w = look_direction(b, xy, -1, 0);
ret.insert(ret.end(), w.begin(), w.end());
auto n = look_direction(b, xy, 0, 1);
ret.insert(ret.end(), n.begin(), n.end());
return ret;
}

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

@ -0,0 +1,96 @@
#include "stickfosh.hpp"
#include "pieces/piece.hpp"
#include "threadpool.hpp"
#include <future>
#include <map>
static int INFINITY = std::numeric_limits<int>::max();
std::string search(std::string pos, int depth) {
Board b = Board::setup_fen_position(pos);
ThreadPool pool(std::thread::hardware_concurrency());
std::vector<Move> moves = b.all_legal_moves();
std::map<std::string, std::future<int>> futures;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
futures.insert(
{move.to_string(), pool.enqueue(minimax, tmp_board, depth - 1)}
);
}
std::string best_move;
int best_eval = -INFINITY;
for (auto& [move, future] : futures) {
int eval = future.get();
if (eval > best_eval) {
best_eval = eval;
best_move = move;
}
}
return best_move;
}
int minimax(const Board& b, int depth) {
if (b.is_checkmate_for(b.white_to_play ? White : Black))
return -INFINITY;
if (depth == 0)
return eval(b);
std::vector<Move> moves = b.all_legal_moves();
int best_evaluation = 0;
Move best_move;
for (const Move& move : moves) {
Board tmp_board = b.make_move(move);
int tmp_eval = -minimax(tmp_board, depth - 1);
best_evaluation = std::max(best_evaluation, tmp_eval);
}
return best_evaluation;
}
static int PawnValue = 100;
static int KnightValue = 300;
static int BishopValue = 320;
static int RookValue = 500;
static int QueenValue = 900;
int count_material(const Board& b, int8_t colour) {
int ret = 0;
for (int i = 0; i < 64; i++) {
if (b.colour_at(i) == colour)
switch (b.squares[i] & 0b111) {
case Piece::Pawn:
ret += PawnValue;
break;
case Piece::Knigt:
ret += KnightValue;
break;
case Piece::Bishop:
ret += BishopValue;
break;
case Piece::Rook:
ret += RookValue;
break;
case Piece::Queen:
ret += QueenValue;
break;
}
}
return ret;
}
int eval(const Board& b) {
int white_eval = count_material(b, Colour::White);
int black_eval = count_material(b, Colour::Black);
int evaluation = white_eval - black_eval;
int perspective = b.white_to_play ? 1 : -1;
return perspective * evaluation;
}

9
cpp/src/stickfosh.hpp Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "board.hpp"
#include <string>
std::string search(std::string, int);
int minimax(const Board&, int);
int eval(const Board&);

73
cpp/src/threadpool.hpp Normal file
View File

@ -0,0 +1,73 @@
#pragma once
#include <condition_variable>
#include <functional>
#include <future>
#include <iostream>
#include <queue>
#include <thread>
#include <vector>
class ThreadPool {
public:
ThreadPool(size_t numThreads) {
for (size_t i = 0; i < numThreads; ++i) {
workers.emplace_back([this] {
while (true) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(queueMutex);
condition.wait(lock, [this] {
return stop || !tasks.empty();
});
if (stop && tasks.empty())
return;
task = std::move(tasks.front());
tasks.pop();
}
task();
}
});
}
}
template <class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::invoke_result<F, Args...>::type> {
using return_type = typename std::invoke_result<F, Args...>::type;
auto task = std::make_shared<std::packaged_task<return_type()>>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queueMutex);
tasks.emplace([task]() { (*task)(); });
}
condition.notify_one();
return res;
}
void waitAll() {
std::unique_lock<std::mutex> lock(queueMutex);
condition.wait(lock, [this] { return tasks.empty(); });
}
~ThreadPool() {
{
std::unique_lock<std::mutex> lock(queueMutex);
stop = true;
}
condition.notify_all();
for (std::thread& worker : workers)
worker.join();
}
private:
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
std::mutex queueMutex;
std::condition_variable condition;
bool stop = false;
};

23
cpp/tests/fen.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "../src/board.hpp"
#include "lib.hpp"
int main() {
std::string pos =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen());
pos = "r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1 b Qk - 0 1";
ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen());
pos = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1";
ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen());
pos = "4k2r/6r1/8/8/8/8/3R4/R3K3 w Qk - 0 1";
ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen());
pos = "8/8/8/4p1K1/2k1P3/8/8/8 b - - 0 1";
ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen());
pos = "8/5k2/3p4/1p1Pp2p/pP2Pp1P/P4P1K/8/8 b - - 99 50";
ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen());
}

16
cpp/tests/lib.hpp Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <iostream>
#define ASSERT_EQUALS(x, y) \
{ \
auto expected = x; \
auto actual = y; \
if (expected != actual) { \
std::cerr << "Expected: " << std::endl \
<< '\t' << expected << std::endl \
<< "Got: " << std::endl \
<< '\t' << actual << std::endl; \
exit(1); \
} \
}

20
cpp/tests/positions.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "../src/coords.hpp"
#include "lib.hpp"
int main() {
ASSERT_EQUALS(Coords(0, 0).to_algebraic(), "a1");
ASSERT_EQUALS(Coords(1, 0).to_algebraic(), "b1");
ASSERT_EQUALS(Coords(2, 1).to_algebraic(), "c2");
ASSERT_EQUALS(Coords(4, 2).to_algebraic(), "e3");
ASSERT_EQUALS(Coords(7, 7).to_algebraic(), "h8");
ASSERT_EQUALS(Coords::from_algebraic("a1"), Coords(0, 0));
ASSERT_EQUALS(Coords::from_algebraic("b1"), Coords(1, 0));
ASSERT_EQUALS(Coords::from_algebraic("c2"), Coords(2, 1));
ASSERT_EQUALS(Coords::from_algebraic("e3"), Coords(4, 2));
ASSERT_EQUALS(Coords::from_algebraic("h8"), Coords(7, 7));
}