Compare commits
31 Commits
Author | SHA1 | Date | |
---|---|---|---|
472f9e4c7c | |||
f5292fa6d7 | |||
8a3a92f80f | |||
b7ad7b3111 | |||
b329c41bea | |||
1625345f08 | |||
a790677bb7 | |||
703dcef59f | |||
d74222c2f4 | |||
01c912435b | |||
8bf164cb05 | |||
e08dbb913e | |||
fea3f6c98a | |||
da55f0085f | |||
95327ec653 | |||
1a4e33201e | |||
d6baf1ee53 | |||
67e377bfde | |||
29453fbb14 | |||
585e392b6a | |||
d436c5a032 | |||
08c0a3b50b | |||
0ae37a3eba | |||
c83129a0d5 | |||
acfa27c83e | |||
85a5bfa328 | |||
139a5c7d8f | |||
3a2988d351 | |||
c758d1854f | |||
53a0755547 | |||
4792daf127 |
34
cpp/.gitignore
vendored
Normal file
34
cpp/.gitignore
vendored
Normal 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
|
56
cpp/Makefile
Normal file
56
cpp/Makefile
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
# 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
|
131
cpp/src/board.cpp
Normal file
131
cpp/src/board.cpp
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
#include "board.hpp"
|
||||||
|
|
||||||
|
#include "coords.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},
|
||||||
|
};
|
||||||
|
|
||||||
|
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() 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;
|
||||||
|
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];
|
||||||
|
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 += "/";
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
Board Board::make_move(Move move) const {
|
||||||
|
int8_t dest_piece = this->squares[move.target_square];
|
||||||
|
|
||||||
|
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 (move.en_passant)
|
||||||
|
ret.squares[move.target_square - 8] = Piece::None;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t Board::get_king_of(int8_t colour) const {
|
||||||
|
for (int i = 0; i < 64; i++)
|
||||||
|
if (this->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)
|
||||||
|
continue;
|
||||||
|
std::vector<Move> moves =
|
||||||
|
legal_moves(this->squares[i], *this, Coords::from_index(i), true);
|
||||||
|
std::vector<int8_t> targets = to_target_square(moves);
|
||||||
|
if (std::find(targets.begin(), targets.end(), i) != targets.end())
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
32
cpp/src/board.hpp
Normal file
32
cpp/src/board.hpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "coords.hpp"
|
||||||
|
#include "move.hpp"
|
||||||
|
#include "pieces/piece.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct Board {
|
||||||
|
private:
|
||||||
|
int8_t get_king_of(int8_t colour) const;
|
||||||
|
|
||||||
|
public:
|
||||||
|
int8_t squares[64] = {Piece::None};
|
||||||
|
bool white_to_play;
|
||||||
|
CastleSide w_castle_rights;
|
||||||
|
CastleSide b_castle_rights;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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
7
cpp/src/castle_side.hpp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
enum CastleSide : int8_t {
|
||||||
|
NeitherSide = 0,
|
||||||
|
KingSide = 1,
|
||||||
|
QueenSide = 2,
|
||||||
|
};
|
19
cpp/src/coords.hpp
Normal file
19
cpp/src/coords.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
struct Coords {
|
||||||
|
int x, y;
|
||||||
|
|
||||||
|
int8_t to_index() const {
|
||||||
|
return this->y * 8 + this->x;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Coords from_index(int idx) {
|
||||||
|
return {idx % 8, idx / 8};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_within_bounds() const {
|
||||||
|
return this->to_index() < 64;
|
||||||
|
}
|
||||||
|
};
|
10
cpp/src/main.cpp
Normal file
10
cpp/src/main.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "board.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
std::string pos =
|
||||||
|
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||||
|
Board b = Board::setup_fen_position(pos);
|
||||||
|
return 0;
|
||||||
|
}
|
15
cpp/src/move.hpp
Normal file
15
cpp/src/move.hpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "castle_side.hpp"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
struct Move {
|
||||||
|
int8_t source_square;
|
||||||
|
int8_t target_square;
|
||||||
|
|
||||||
|
bool is_capturing = false;
|
||||||
|
CastleSide castle_side = CastleSide::NeitherSide;
|
||||||
|
bool en_passant = false;
|
||||||
|
int8_t promoting_to = 0;
|
||||||
|
};
|
23
cpp/src/pieces/bishop.cpp
Normal file
23
cpp/src/pieces/bishop.cpp
Normal 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;
|
||||||
|
}
|
76
cpp/src/pieces/king.cpp
Normal file
76
cpp/src/pieces/king.cpp
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#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 ret;
|
||||||
|
|
||||||
|
// -- Castles
|
||||||
|
CastleSide 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(),
|
||||||
|
.castle_side = CastleSide::KingSide
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (castling_rights & CastleSide::QueenSide && is_clear_queen_side(b, xy)) {
|
||||||
|
ret.push_back(Move{
|
||||||
|
xy.to_index(),
|
||||||
|
Coords{2, xy.y}.to_index(),
|
||||||
|
.castle_side = CastleSide::QueenSide
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
28
cpp/src/pieces/knight.cpp
Normal file
28
cpp/src/pieces/knight.cpp
Normal 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;
|
||||||
|
}
|
51
cpp/src/pieces/pawn.cpp
Normal file
51
cpp/src/pieces/pawn.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#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))
|
||||||
|
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))
|
||||||
|
ret.push_back(Move{xy.to_index(), right.to_index()});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- 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 : {Queen, Knigt, Bishop, Rook})
|
||||||
|
ret.push_back(Move{
|
||||||
|
xy.to_index(),
|
||||||
|
new_xy.to_index(),
|
||||||
|
.promoting_to = piece
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
72
cpp/src/pieces/piece.cpp
Normal file
72
cpp/src/pieces/piece.cpp
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
#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 = false
|
||||||
|
) {
|
||||||
|
std::vector<Move> ret;
|
||||||
|
switch (p) {
|
||||||
|
case Piece::Pawn:
|
||||||
|
ret = pawn_moves(b, xy);
|
||||||
|
case Piece::Bishop:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!looking_for_check)
|
||||||
|
return keep_only_blocking(ret, b);
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<Move>
|
||||||
|
move_for_position(const Board& board, const Coords source, const Coords dest) {
|
||||||
|
if (dest.is_within_bounds())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
int8_t piece = board.squares[dest.to_index()];
|
||||||
|
if (piece == Piece::None)
|
||||||
|
return Move{source.to_index(), dest.to_index()};
|
||||||
|
|
||||||
|
int8_t source_colour = board.colour_at(source);
|
||||||
|
int8_t dest_colour = board.colour_at(dest);
|
||||||
|
if (source_colour != dest_colour)
|
||||||
|
return Move{source.to_index(), dest.to_index(), true};
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Move>
|
||||||
|
look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
|
||||||
|
std::vector<Move> ret;
|
||||||
|
for (int d = 0; d < 8; d++) {
|
||||||
|
int dx = mult_dx * d;
|
||||||
|
int dy = mult_dy * d;
|
||||||
|
|
||||||
|
std::optional<Move> move =
|
||||||
|
move_for_position(board, xy, Coords{xy.x + dx, xy.y + dy});
|
||||||
|
if (move.has_value()) {
|
||||||
|
ret.push_back(move.value());
|
||||||
|
if (move.value().is_capturing)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
37
cpp/src/pieces/piece.hpp
Normal file
37
cpp/src/pieces/piece.hpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../move.hpp"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
enum Piece : int8_t {
|
||||||
|
None = 0,
|
||||||
|
King = 1,
|
||||||
|
Pawn = 2,
|
||||||
|
Knigt = 3,
|
||||||
|
Bishop = 4,
|
||||||
|
Rook = 5,
|
||||||
|
Queen = 6,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Colour : int8_t {
|
||||||
|
White = 8,
|
||||||
|
Black = 16,
|
||||||
|
};
|
||||||
|
|
||||||
|
class Board;
|
||||||
|
struct Coords;
|
||||||
|
|
||||||
|
std::vector<Move> legal_moves(int8_t, const Board&, const Coords, bool);
|
||||||
|
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
35
cpp/src/pieces/queen.cpp
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#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());
|
||||||
|
|
||||||
|
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
23
cpp/src/pieces/rook.cpp
Normal 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;
|
||||||
|
}
|
0
cpp/src/stickfosh.cpp
Normal file
0
cpp/src/stickfosh.cpp
Normal file
22
cpp/tests/fen.cpp
Normal file
22
cpp/tests/fen.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#include "../src/board.hpp"
|
||||||
|
#include "lib.hpp"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::string pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
|
||||||
|
ASSERT_EQUALS(pos, Board::setup_fen_position(pos)->to_fen());
|
||||||
|
|
||||||
|
pos = "r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1";
|
||||||
|
ASSERT_EQUALS(pos, Board::setup_fen_position(pos)->to_fen());
|
||||||
|
|
||||||
|
pos = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR";
|
||||||
|
ASSERT_EQUALS(pos, Board::setup_fen_position(pos)->to_fen());
|
||||||
|
|
||||||
|
pos = "4k2r/6r1/8/8/8/8/3R4/R3K3";
|
||||||
|
ASSERT_EQUALS(pos, Board::setup_fen_position(pos)->to_fen());
|
||||||
|
|
||||||
|
pos = "8/8/8/4p1K1/2k1P3/8/8/8";
|
||||||
|
ASSERT_EQUALS(pos, Board::setup_fen_position(pos)->to_fen());
|
||||||
|
|
||||||
|
pos = "8/5k2/3p4/1p1Pp2p/pP2Pp1P/P4P1K/8/8";
|
||||||
|
ASSERT_EQUALS(pos, Board::setup_fen_position(pos)->to_fen());
|
||||||
|
}
|
12
cpp/tests/lib.hpp
Normal file
12
cpp/tests/lib.hpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#define ASSERT_EQUALS(expected, actual) \
|
||||||
|
{ \
|
||||||
|
if (expected != actual) \
|
||||||
|
std::cout << "Expected: " << std::endl \
|
||||||
|
<< '\t' << expected << std::endl \
|
||||||
|
<< "Got: " << std::endl \
|
||||||
|
<< '\t' << actual << std::endl; \
|
||||||
|
}
|
Reference in New Issue
Block a user