Compare commits
41 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 | |||
947114877b | |||
166e1c7664 | |||
84d73511d2 | |||
4bb068b2a5 | |||
92e1ff26fc | |||
c7884e227b | |||
e6fafc8081 | |||
1be71bf203 | |||
e862ab6b0b | |||
6c0819428e |
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
@ -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
@ -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
@ -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
@ -0,0 +1,7 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
enum CastleSide : int8_t {
|
||||||
|
NeitherSide = 0,
|
||||||
|
KingSide = 1,
|
||||||
|
QueenSide = 2,
|
||||||
|
};
|
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
@ -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
@ -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
@ -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
@ -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
@ -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
@ -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
@ -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
@ -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
@ -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
@ -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
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
@ -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; \
|
||||||
|
}
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 734 B After Width: | Height: | Size: 734 B |
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 2.7 KiB |
Before Width: | Height: | Size: 1014 B After Width: | Height: | Size: 1014 B |
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.5 KiB |
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
169
python/src/ai/ai.py
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
from collections import defaultdict
|
||||||
|
import time
|
||||||
|
|
||||||
|
from tqdm import tqdm
|
||||||
|
from logic.board import INITIAL_BOARD, Board
|
||||||
|
from logic.move import Move
|
||||||
|
from logic.pieces.piece import Colour
|
||||||
|
|
||||||
|
pos2expected = {
|
||||||
|
# -- Position 1
|
||||||
|
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1": {
|
||||||
|
1: 20,
|
||||||
|
2: 400,
|
||||||
|
3: 8_902,
|
||||||
|
4: 197_281,
|
||||||
|
},
|
||||||
|
|
||||||
|
# -- Position 2
|
||||||
|
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1": {
|
||||||
|
1: 48,
|
||||||
|
2: 2_039,
|
||||||
|
3: 97_862,
|
||||||
|
4: 4_085_603,
|
||||||
|
},
|
||||||
|
|
||||||
|
# -- Position 3
|
||||||
|
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1": {
|
||||||
|
1: 14,
|
||||||
|
2: 191,
|
||||||
|
3: 2_812,
|
||||||
|
4: 43_238,
|
||||||
|
5: 674_624,
|
||||||
|
},
|
||||||
|
|
||||||
|
# -- Position 4
|
||||||
|
"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1": {
|
||||||
|
1: 6,
|
||||||
|
2: 264,
|
||||||
|
3: 9467,
|
||||||
|
4: 422_333,
|
||||||
|
},
|
||||||
|
|
||||||
|
# -- Position 5
|
||||||
|
"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8": {
|
||||||
|
1: 44,
|
||||||
|
2: 1486,
|
||||||
|
3: 62379,
|
||||||
|
4: 2103487,
|
||||||
|
},
|
||||||
|
|
||||||
|
# -- Position 6
|
||||||
|
"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10": {
|
||||||
|
1: 46,
|
||||||
|
2: 2079,
|
||||||
|
3: 89890,
|
||||||
|
4: 3894594,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
tick = "\033[92m✔️\033[0m" # Green Tick
|
||||||
|
cross = "\033[91m❌\033[0m" # Red Cross
|
||||||
|
|
||||||
|
res = defaultdict(lambda : 0)
|
||||||
|
|
||||||
|
def peft(pos: str):
|
||||||
|
global res
|
||||||
|
expected = pos2expected[pos]
|
||||||
|
board = Board.setup_FEN_position(pos)
|
||||||
|
for depth in expected:
|
||||||
|
with tqdm(total=expected[depth], desc=f"Depth: {depth}") as bar:
|
||||||
|
start = time.process_time()
|
||||||
|
moves = move_generation_test(bar, board, depth, depth)
|
||||||
|
bar.close()
|
||||||
|
elapsed = time.process_time() - start
|
||||||
|
elapsed *= 1_000
|
||||||
|
|
||||||
|
print("Depth:", depth, end=" ")
|
||||||
|
print("Result:", moves, end=" ")
|
||||||
|
if moves == expected[depth]:
|
||||||
|
print(f"{tick}", end=" ")
|
||||||
|
else:
|
||||||
|
print(f"{cross} (expected {expected[depth]})", end=" ")
|
||||||
|
print("positions Time:", int(elapsed), "milliseconds")
|
||||||
|
|
||||||
|
if moves != expected[depth]:
|
||||||
|
print()
|
||||||
|
for key, value in res.items():
|
||||||
|
print(f"{key}: {value}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def move_generation_test(bar, board: Board, depth: int, max_depth, first = None):
|
||||||
|
global res
|
||||||
|
if first is None:
|
||||||
|
res = defaultdict(lambda : 0)
|
||||||
|
|
||||||
|
if board.is_terminal():
|
||||||
|
# bar.update(1)
|
||||||
|
# res[first] += 1
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if depth == 0:
|
||||||
|
res[first] += 1
|
||||||
|
bar.update(1)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
moves = board.legal_moves()
|
||||||
|
if depth == 1:
|
||||||
|
res[first] += len(moves)
|
||||||
|
bar.update(len(moves))
|
||||||
|
return len(moves)
|
||||||
|
|
||||||
|
num_pos = 0
|
||||||
|
for move in moves:
|
||||||
|
tmp_board = board.make_move(move)
|
||||||
|
if first is None:
|
||||||
|
first = move.piece.pos.to_algebraic() + move.pos.to_algebraic()
|
||||||
|
|
||||||
|
if first == "f7h8":
|
||||||
|
print(tmp_board.legal_moves())
|
||||||
|
num_pos += move_generation_test(bar, tmp_board, depth - 1, max_depth, first = first)
|
||||||
|
|
||||||
|
if depth == max_depth:
|
||||||
|
first = None
|
||||||
|
|
||||||
|
return num_pos
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def play_game(board: Board, strategies: dict, verbose: bool = False) -> Board:
|
||||||
|
"""Play a turn-taking game. `strategies` is a {player_name: function} dict,
|
||||||
|
where function(state) is used to get the player's move."""
|
||||||
|
state = board
|
||||||
|
move_counter = 1
|
||||||
|
while not (board.is_checkmate_for(board._turn) or board.is_stalemate_for(board._turn)):
|
||||||
|
player = board._turn
|
||||||
|
move = strategies[player](state)
|
||||||
|
state = board.make_move(move)
|
||||||
|
if verbose:
|
||||||
|
if player == Colour.WHITE:
|
||||||
|
print(str(move_counter) + ".", move, end=" ")
|
||||||
|
else:
|
||||||
|
print(move)
|
||||||
|
|
||||||
|
return state
|
||||||
|
|
||||||
|
def minmax_search(state: Board) -> tuple[float, Move]:
|
||||||
|
"""Search game tree to determine best move; return (value, move) pair."""
|
||||||
|
return _max_value(state) if state._turn == Colour.WHITE else _min_value(state)
|
||||||
|
|
||||||
|
def _max_value(state: Board) -> tuple[float, Move]:
|
||||||
|
if state.is_terminal():
|
||||||
|
return state.utility(), None
|
||||||
|
v, move = -float("inf"), None
|
||||||
|
for a in state.legal_moves():
|
||||||
|
v2, _ = _min_value(state.make_move(a))
|
||||||
|
if v2 > v:
|
||||||
|
v, move = v2, a
|
||||||
|
return v, move
|
||||||
|
|
||||||
|
def _min_value(state: Board) -> tuple[float, Move]:
|
||||||
|
if state.is_terminal():
|
||||||
|
return state.utility(), None
|
||||||
|
v, move = float("inf"), None
|
||||||
|
for a in state.legal_moves():
|
||||||
|
v2, _ = _min_value(state.make_move(a))
|
||||||
|
if v2 < v:
|
||||||
|
v, move = v2, a
|
||||||
|
return v, move
|
@ -55,3 +55,9 @@ class Controller:
|
|||||||
else:
|
else:
|
||||||
move = legal_moves_positions[0]
|
move = legal_moves_positions[0]
|
||||||
self._make_move(move)
|
self._make_move(move)
|
||||||
|
|
||||||
|
if self._board.is_checkmate_for(self._board._turn):
|
||||||
|
self._view.notify_checkmate(self._board._turn)
|
||||||
|
|
||||||
|
if self._board.is_stalemate_for(self._board._turn):
|
||||||
|
self._view.notify_stalemate(self._board._turn)
|
360
python/src/logic/board.py
Normal file
@ -0,0 +1,360 @@
|
|||||||
|
from logic.move import CastleSide, Move
|
||||||
|
from logic.pieces.bishop import Bishop
|
||||||
|
from logic.pieces.king import King
|
||||||
|
from logic.pieces.knight import Knight
|
||||||
|
from logic.pieces.queen import Queen
|
||||||
|
from logic.pieces.rook import Rook
|
||||||
|
from logic.pieces.pawn import Pawn
|
||||||
|
from logic.pieces.piece import Colour, Piece
|
||||||
|
from logic.position import Position
|
||||||
|
|
||||||
|
from typing import Type
|
||||||
|
|
||||||
|
class Board:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self._white: dict[Position, Piece] = {}
|
||||||
|
self._black: dict[Position, Piece] = {}
|
||||||
|
self._turn = None
|
||||||
|
self._white_castling_rights = set()
|
||||||
|
self._black_castling_rights = set()
|
||||||
|
self._en_passant_target = None
|
||||||
|
self._n_moves = 0
|
||||||
|
self._n_half_moves = 0
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _piece_class_from_char(c: str) -> Type[Piece]:
|
||||||
|
assert len(c) == 1, f"The piece {c} isn't denoted by 1 character"
|
||||||
|
c = c.lower()
|
||||||
|
if c == "p":
|
||||||
|
return Pawn
|
||||||
|
if c == "r":
|
||||||
|
return Rook
|
||||||
|
if c == "n":
|
||||||
|
return Knight
|
||||||
|
if c == "b":
|
||||||
|
return Bishop
|
||||||
|
if c == "q":
|
||||||
|
return Queen
|
||||||
|
if c == "k":
|
||||||
|
return King
|
||||||
|
raise ValueError(f"Unknown piece '{c}'")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def setup_FEN_position(position: str) -> "Board":
|
||||||
|
ret = Board()
|
||||||
|
index = 0
|
||||||
|
|
||||||
|
# -- Pieces
|
||||||
|
pieces = "prnbqk" # possible pieces
|
||||||
|
numbers = "12345678" # possible number of empty squares
|
||||||
|
|
||||||
|
x = 0
|
||||||
|
y = 7 # FEN starts from the top left, so 8th rank
|
||||||
|
for c in position:
|
||||||
|
index += 1
|
||||||
|
if c == " ":
|
||||||
|
break
|
||||||
|
if c in pieces or c in pieces.upper():
|
||||||
|
pos = Position(x, y)
|
||||||
|
piece = Board._piece_class_from_char(c)
|
||||||
|
if c.isupper():
|
||||||
|
ret._white[pos] = piece(pos, Colour.WHITE)
|
||||||
|
else:
|
||||||
|
ret._black[pos] = piece(pos, Colour.BLACK)
|
||||||
|
|
||||||
|
x += 1
|
||||||
|
continue
|
||||||
|
if c in numbers:
|
||||||
|
x += int(c)
|
||||||
|
if c == '/':
|
||||||
|
x = 0
|
||||||
|
y -= 1
|
||||||
|
|
||||||
|
|
||||||
|
# -- Active colour
|
||||||
|
if position[index] == "w":
|
||||||
|
ret._turn = Colour.WHITE
|
||||||
|
elif position[index] == "b":
|
||||||
|
ret._turn = Colour.BLACK
|
||||||
|
else:
|
||||||
|
raise ValueError(f"The FEN position is malformed, the active colour should be either 'w' or 'b', but is '{position[index]}'")
|
||||||
|
index += 2
|
||||||
|
|
||||||
|
|
||||||
|
# -- Castling Rights
|
||||||
|
for c in position[index:]:
|
||||||
|
index += 1
|
||||||
|
if c == "-" or c == " ":
|
||||||
|
if c == "-":
|
||||||
|
index += 1
|
||||||
|
break
|
||||||
|
|
||||||
|
sides = "kq"
|
||||||
|
assert c in sides or c in sides.upper(), f"The FEN position is malformed, the castling rights should be either k or q (both either lower- or upper-case), instead is '{c}'"
|
||||||
|
if c == "K":
|
||||||
|
ret._white_castling_rights.add(CastleSide.King)
|
||||||
|
if c == "Q":
|
||||||
|
ret._white_castling_rights.add(CastleSide.Queen)
|
||||||
|
if c == "k":
|
||||||
|
ret._black_castling_rights.add(CastleSide.King)
|
||||||
|
if c == "q":
|
||||||
|
ret._black_castling_rights.add(CastleSide.Queen)
|
||||||
|
|
||||||
|
# -- En passant target
|
||||||
|
if position[index] != "-":
|
||||||
|
pos = Position.from_algebraic(position[index:index+2])
|
||||||
|
index += 2
|
||||||
|
if pos.y == 2:
|
||||||
|
pos.y += 1
|
||||||
|
assert pos in ret._white, "En passant target is not in the position"
|
||||||
|
ret._en_passant_target = ret._white[pos]
|
||||||
|
elif pos.y == 5:
|
||||||
|
pos.y -= 1
|
||||||
|
assert pos in ret._black, "En passant target is not in the position"
|
||||||
|
ret._en_passant_target = ret._black[pos]
|
||||||
|
else:
|
||||||
|
raise ValueError("You can't have a en passant target that is not on the third or sixth rank")
|
||||||
|
else:
|
||||||
|
index += 1
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
ret._n_half_moves = int(position[index:position.find(" ", index + 1)])
|
||||||
|
ret._n_moves = int(position[position.find(" ", index)+1:])
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def piece_at(self, x: int, y: int) -> Piece | None:
|
||||||
|
pos = Position(x, y)
|
||||||
|
white_piece = self._white.get(pos, None)
|
||||||
|
black_piece = self._black.get(pos, None)
|
||||||
|
|
||||||
|
assert white_piece == None or black_piece == None, f"There are two pieces at the same position {pos}, this shouldn't happen!"
|
||||||
|
|
||||||
|
if white_piece != None:
|
||||||
|
return white_piece
|
||||||
|
return black_piece
|
||||||
|
|
||||||
|
def is_check_for(self, colour: Colour) -> bool:
|
||||||
|
""" Is it check for the defending colour passed as parameter """
|
||||||
|
defending_pieces, attacking_pieces = (self._white, self._black) if colour == Colour.WHITE else (self._black, self._white)
|
||||||
|
|
||||||
|
kings = [piece for piece in defending_pieces.values() if type(piece) == King]
|
||||||
|
assert len(kings) == 1, f"We have more than one king for {colour}, that is no buono..."
|
||||||
|
king = kings[0]
|
||||||
|
|
||||||
|
for piece in attacking_pieces.values():
|
||||||
|
possible_pos = []
|
||||||
|
if type(piece) == King:
|
||||||
|
# special case for the king, because it creates infinite recursion (since he looks if he's walking into a check)
|
||||||
|
for dx in [-1, 0, 1]:
|
||||||
|
for dy in [-1, 0, 1]:
|
||||||
|
x, y = piece.pos.x + dx, piece.pos.y + dy
|
||||||
|
if Position.is_within_bounds(x, y):
|
||||||
|
possible_pos.append(Position(x, y))
|
||||||
|
else:
|
||||||
|
possible_pos += [move.pos for move in piece.legal_moves(self, looking_for_check=True)]
|
||||||
|
if king.pos in possible_pos:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def is_checkmate_for(self, colour: Colour) -> bool:
|
||||||
|
""" Is it checkmate for the defending colour passed as parameter """
|
||||||
|
return self.is_check_for(colour) and self._no_legal_moves_for(colour)
|
||||||
|
|
||||||
|
def is_stalemate_for(self, colour: Colour) -> bool:
|
||||||
|
""" Is it stalemate for the defending colour passed as parameter """
|
||||||
|
return not self.is_check_for(colour) and self._no_legal_moves_for(colour)
|
||||||
|
|
||||||
|
def _no_legal_moves_for(self, colour: Colour) -> bool:
|
||||||
|
""" Return true if there are indeed no legal moves for the given colour (for checkmate or stalemate)"""
|
||||||
|
pieces = self._white if colour == Colour.WHITE else self._black
|
||||||
|
for piece in pieces.values():
|
||||||
|
if len(piece.legal_moves(self)) > 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def castling_rights_for(self, colour: Colour) -> set[CastleSide]:
|
||||||
|
return self._white_castling_rights if colour == Colour.WHITE else self._black_castling_rights
|
||||||
|
|
||||||
|
def make_move(self, move: Move) -> "Board":
|
||||||
|
dest_piece = self.piece_at(move.pos.x, move.pos.y)
|
||||||
|
|
||||||
|
if dest_piece:
|
||||||
|
assert dest_piece.colour != move.piece.colour, "A piece cannot cannot eat another piece of the same colour"
|
||||||
|
|
||||||
|
# -- Copy current state
|
||||||
|
ret = Board()
|
||||||
|
ret._white = self._white.copy()
|
||||||
|
ret._black = self._black.copy()
|
||||||
|
ret._turn = Colour.WHITE if self._turn == Colour.BLACK else Colour.BLACK
|
||||||
|
ret._white_castling_rights = self._white_castling_rights.copy()
|
||||||
|
ret._black_castling_rights = self._black_castling_rights.copy()
|
||||||
|
|
||||||
|
|
||||||
|
piece = move.piece
|
||||||
|
|
||||||
|
# -- Actually make the move
|
||||||
|
pieces_moving, other_pieces = (ret._white, ret._black) if piece.colour == Colour.WHITE else (ret._black, ret._white)
|
||||||
|
|
||||||
|
del pieces_moving[piece.pos]
|
||||||
|
pieces_moving[move.pos] = piece.move_to(move.pos)
|
||||||
|
if move.pos in other_pieces:
|
||||||
|
del other_pieces[move.pos]
|
||||||
|
|
||||||
|
if piece.colour == Colour.BLACK:
|
||||||
|
ret._n_moves = self._n_moves + 1
|
||||||
|
|
||||||
|
if move.is_capturing or type(piece) == Pawn:
|
||||||
|
ret._n_half_moves = 0
|
||||||
|
else:
|
||||||
|
ret._n_half_moves = self._n_half_moves + 1
|
||||||
|
|
||||||
|
if move.en_passant:
|
||||||
|
pos_to_remove = Position(move.pos.x, move.pos.y + (1 if self._turn == Colour.BLACK else -1))
|
||||||
|
del other_pieces[pos_to_remove]
|
||||||
|
|
||||||
|
if move.promotes_to is not None:
|
||||||
|
assert type(piece) == Pawn, "Trying to promote something that is not a pawn: not good!"
|
||||||
|
pieces_moving[move.pos] = move.promotes_to(move.pos, piece.colour)
|
||||||
|
|
||||||
|
# -- Set en passant target if needed
|
||||||
|
if move.becomes_en_passant_target:
|
||||||
|
ret._en_passant_target = pieces_moving[move.pos]
|
||||||
|
else:
|
||||||
|
ret._en_passant_target = None
|
||||||
|
|
||||||
|
# -- Handle castling (just move the rook over)
|
||||||
|
if move.castle_side == CastleSide.King:
|
||||||
|
rook_pos = Position(7, piece.pos.y)
|
||||||
|
assert rook_pos in pieces_moving and type(pieces_moving[rook_pos]) == Rook, "Either rook is absent from the king side or you are trying to castle with something else than a rook..."
|
||||||
|
del pieces_moving[rook_pos]
|
||||||
|
new_rook_pos = Position(5, piece.pos.y)
|
||||||
|
pieces_moving[new_rook_pos] = Rook(new_rook_pos, piece.colour)
|
||||||
|
|
||||||
|
elif move.castle_side == CastleSide.Queen:
|
||||||
|
rook_pos = Position(0, piece.pos.y)
|
||||||
|
assert rook_pos in pieces_moving and type(pieces_moving[rook_pos]) == Rook, "Either rook is absent from the queen side or you are trying to castle with something else than a rook..."
|
||||||
|
del pieces_moving[rook_pos]
|
||||||
|
new_rook_pos = Position(3, piece.pos.y)
|
||||||
|
pieces_moving[new_rook_pos] = Rook(new_rook_pos, piece.colour)
|
||||||
|
|
||||||
|
# -- Check for castling rights
|
||||||
|
if piece.colour == Colour.WHITE:
|
||||||
|
if type(piece) == King:
|
||||||
|
ret._white_castling_rights = set()
|
||||||
|
|
||||||
|
if type(piece) == Rook:
|
||||||
|
if piece.pos.x == 0 and CastleSide.Queen in ret._white_castling_rights:
|
||||||
|
ret._white_castling_rights.remove(CastleSide.Queen)
|
||||||
|
elif piece.pos.x == 7 and CastleSide.King in ret._white_castling_rights:
|
||||||
|
ret._white_castling_rights.remove(CastleSide.King)
|
||||||
|
|
||||||
|
if move.is_capturing and move.pos.y == 7 and move.pos in self._black and type(self._black[move.pos]) == Rook:
|
||||||
|
if move.pos.x == 0 and CastleSide.Queen in ret._black_castling_rights:
|
||||||
|
ret._black_castling_rights.remove(CastleSide.Queen)
|
||||||
|
elif move.pos.x == 7 and CastleSide.King in ret._black_castling_rights:
|
||||||
|
ret._black_castling_rights.remove(CastleSide.King)
|
||||||
|
else:
|
||||||
|
if type(piece) == King:
|
||||||
|
ret._black_castling_rights = set()
|
||||||
|
|
||||||
|
if type(piece) == Rook:
|
||||||
|
if piece.pos.x == 0 and CastleSide.Queen in ret._black_castling_rights:
|
||||||
|
ret._black_castling_rights.remove(CastleSide.Queen)
|
||||||
|
elif piece.pos.x == 7 and CastleSide.King in ret._black_castling_rights:
|
||||||
|
ret._black_castling_rights.remove(CastleSide.King)
|
||||||
|
|
||||||
|
if move.is_capturing and move.pos.y == 0 and move.pos in self._white and type(self._white[move.pos]) == Rook:
|
||||||
|
if move.pos.x == 0 and CastleSide.Queen in ret._white_castling_rights:
|
||||||
|
ret._white_castling_rights.remove(CastleSide.Queen)
|
||||||
|
elif move.pos.x == 7 and CastleSide.King in ret._white_castling_rights:
|
||||||
|
ret._white_castling_rights.remove(CastleSide.King)
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def to_fen_string(self):
|
||||||
|
ret = ""
|
||||||
|
for y in range(7, -1, -1):
|
||||||
|
empty_cell_counter = 0
|
||||||
|
for x in range(8):
|
||||||
|
pos = Position(x, y)
|
||||||
|
|
||||||
|
piece = None
|
||||||
|
if pos in self._white:
|
||||||
|
piece = self._white[pos]
|
||||||
|
elif pos in self._black:
|
||||||
|
piece = self._black[pos]
|
||||||
|
|
||||||
|
if piece is None:
|
||||||
|
empty_cell_counter += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
if empty_cell_counter > 0:
|
||||||
|
ret += str(empty_cell_counter)
|
||||||
|
empty_cell_counter = 0
|
||||||
|
letter = piece.letter()
|
||||||
|
ret += letter.lower() if piece.colour == Colour.BLACK else letter.upper()
|
||||||
|
|
||||||
|
if empty_cell_counter > 0:
|
||||||
|
ret += str(empty_cell_counter)
|
||||||
|
|
||||||
|
if y > 0:
|
||||||
|
ret += "/"
|
||||||
|
ret += " "
|
||||||
|
|
||||||
|
ret += "w" if self._turn == Colour.WHITE else "b"
|
||||||
|
ret += " "
|
||||||
|
|
||||||
|
if len(self._white_castling_rights) == 0 and len(self._black_castling_rights) == 0:
|
||||||
|
ret += "-"
|
||||||
|
else:
|
||||||
|
if CastleSide.King in self._white_castling_rights:
|
||||||
|
ret += "K"
|
||||||
|
if CastleSide.Queen in self._white_castling_rights:
|
||||||
|
ret += "Q"
|
||||||
|
|
||||||
|
if CastleSide.King in self._black_castling_rights:
|
||||||
|
ret += "k"
|
||||||
|
if CastleSide.Queen in self._black_castling_rights:
|
||||||
|
ret += "q"
|
||||||
|
ret += " "
|
||||||
|
|
||||||
|
if self._en_passant_target is not None:
|
||||||
|
pos = Position(self._en_passant_target.pos.x, self._en_passant_target.pos.y)
|
||||||
|
pos.y += -1 if self._en_passant_target.colour == Colour.WHITE else 1
|
||||||
|
ret += pos.to_algebraic()
|
||||||
|
else:
|
||||||
|
ret += "-"
|
||||||
|
ret += " "
|
||||||
|
|
||||||
|
ret += str(self._n_half_moves)
|
||||||
|
ret += " "
|
||||||
|
ret += str(self._n_moves)
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def legal_moves(self) -> list[Move]:
|
||||||
|
ret = []
|
||||||
|
pieces = self._white if self._turn == Colour.WHITE else self._black
|
||||||
|
for piece in pieces.values():
|
||||||
|
ret += piece.legal_moves(self)
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def is_terminal(self) -> bool:
|
||||||
|
return self.is_stalemate_for(Colour.WHITE) or self.is_stalemate_for(Colour.BLACK) or self.is_checkmate_for(Colour.WHITE) or self.is_checkmate_for(Colour.BLACK)
|
||||||
|
|
||||||
|
def utility(self) -> int:
|
||||||
|
if self.is_stalemate_for(Colour.WHITE) or self.is_stalemate_for(Colour.BLACK):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if self.is_checkmate_for(Colour.WHITE):
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if self.is_checkmate_for(Colour.BLACK):
|
||||||
|
return -1
|
||||||
|
|
||||||
|
raise ValueError("Cannot determine the utility of board become it neither checkmate nor stalemate for either players")
|
||||||
|
|
||||||
|
_fen_pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
||||||
|
INITIAL_BOARD = Board.setup_FEN_position(_fen_pos)
|
@ -1,4 +1,5 @@
|
|||||||
# from logic.pieces.piece import Piece
|
# from logic.pieces.piece import Piece
|
||||||
|
from typing import Type
|
||||||
from logic.position import Position
|
from logic.position import Position
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
@ -8,11 +9,14 @@ class CastleSide(Enum):
|
|||||||
Queen = "O-O-O"
|
Queen = "O-O-O"
|
||||||
|
|
||||||
class Move:
|
class Move:
|
||||||
def __init__(self, piece: "Piece", pos: Position,/, is_capturing: bool = False, castle_side: CastleSide = CastleSide.Neither) -> None:
|
def __init__(self, piece: "Piece", pos: Position,/, is_capturing: bool = False, castle_side: CastleSide = CastleSide.Neither, en_passant: bool = False, becomes_en_passant_target: bool = False, promotes_to: Type["Piece"] = None) -> None:
|
||||||
self.piece = piece
|
self.piece = piece
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
self.is_capturing = is_capturing
|
self.is_capturing = is_capturing
|
||||||
self.castle_side = castle_side
|
self.castle_side = castle_side
|
||||||
|
self.becomes_en_passant_target = becomes_en_passant_target
|
||||||
|
self.en_passant = en_passant
|
||||||
|
self.promotes_to = promotes_to
|
||||||
|
|
||||||
def to_algebraic(self) -> str:
|
def to_algebraic(self) -> str:
|
||||||
raise NotImplementedError("The move can't be translated to algbraic notation, as it was not implemented")
|
raise NotImplementedError("The move can't be translated to algbraic notation, as it was not implemented")
|
||||||
@ -28,10 +32,19 @@ class Move:
|
|||||||
return "O-O-O"
|
return "O-O-O"
|
||||||
|
|
||||||
ret = ""
|
ret = ""
|
||||||
if type(self.piece).__name__ != "Pawn":
|
if type(self.piece).__name__ == "Pawn":
|
||||||
|
if self.is_capturing:
|
||||||
|
ret += self.piece.pos.to_algebraic()[0]
|
||||||
|
ret += "x"
|
||||||
|
ret += self.pos.to_algebraic()
|
||||||
|
else:
|
||||||
|
ret += self.pos.to_algebraic()
|
||||||
|
else:
|
||||||
ret += self.piece.letter().upper()
|
ret += self.piece.letter().upper()
|
||||||
|
if self.is_capturing:
|
||||||
|
ret += "x"
|
||||||
ret += str(self.pos)
|
ret += str(self.pos)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
@ -2,7 +2,7 @@ from logic.move import Move
|
|||||||
from .piece import Piece
|
from .piece import Piece
|
||||||
|
|
||||||
class Bishop(Piece):
|
class Bishop(Piece):
|
||||||
def legal_moves(self, board: "Board") -> list[Move]:
|
def legal_moves(self, board: "Board", / , looking_for_check = False) -> list[Move]:
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
# looking north east
|
# looking north east
|
||||||
@ -17,5 +17,8 @@ class Bishop(Piece):
|
|||||||
# looking north west
|
# looking north west
|
||||||
ret.extend(self._look_direction(board, -1, 1))
|
ret.extend(self._look_direction(board, -1, 1))
|
||||||
|
|
||||||
|
if not looking_for_check:# and board.is_check_for(self.colour):
|
||||||
|
return self.keep_only_blocking(ret, board)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
@ -19,13 +19,15 @@ class King(Piece):
|
|||||||
if not board_after_move.is_check_for(self.colour):
|
if not board_after_move.is_check_for(self.colour):
|
||||||
ret.append(move)
|
ret.append(move)
|
||||||
|
|
||||||
# -- Castles
|
if board.is_check_for(self.colour):
|
||||||
castling_writes = board.castling_writes_for(self.colour)
|
return self.keep_only_blocking(ret, board)
|
||||||
if len(castling_writes) == 0:
|
|
||||||
return ret
|
|
||||||
print(castling_writes)
|
|
||||||
|
|
||||||
if CastleSide.King in castling_writes:
|
# -- Castles
|
||||||
|
castling_rights = board.castling_rights_for(self.colour)
|
||||||
|
if len(castling_rights) == 0:
|
||||||
|
return ret
|
||||||
|
|
||||||
|
if CastleSide.King in castling_rights:
|
||||||
clear = True
|
clear = True
|
||||||
for dx in range(1, 3):
|
for dx in range(1, 3):
|
||||||
x = self.pos.x + dx
|
x = self.pos.x + dx
|
||||||
@ -43,9 +45,9 @@ class King(Piece):
|
|||||||
if clear:
|
if clear:
|
||||||
ret.append(Move(self, Position(6, self.pos.y), castle_side=CastleSide.King))
|
ret.append(Move(self, Position(6, self.pos.y), castle_side=CastleSide.King))
|
||||||
|
|
||||||
if CastleSide.Queen in castling_writes:
|
if CastleSide.Queen in castling_rights:
|
||||||
clear = True
|
clear = True
|
||||||
for dx in range(1, 3):
|
for dx in range(1, 4):
|
||||||
x = self.pos.x - dx
|
x = self.pos.x - dx
|
||||||
y = self.pos.y
|
y = self.pos.y
|
||||||
|
|
||||||
@ -55,15 +57,13 @@ class King(Piece):
|
|||||||
|
|
||||||
move = self._move_for_position(board, x, y)
|
move = self._move_for_position(board, x, y)
|
||||||
board_after_move = board.make_move(move)
|
board_after_move = board.make_move(move)
|
||||||
if board_after_move.is_check_for(self.colour):
|
if dx < 3 and board_after_move.is_check_for(self.colour):
|
||||||
clear = False
|
clear = False
|
||||||
break
|
break
|
||||||
|
|
||||||
if clear:
|
if clear:
|
||||||
ret.append(Move(self, Position(2, self.pos.y), castle_side=CastleSide.Queen))
|
ret.append(Move(self, Position(2, self.pos.y), castle_side=CastleSide.Queen))
|
||||||
|
|
||||||
print(ret)
|
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
@ -4,7 +4,7 @@ class Knight(Piece):
|
|||||||
def letter(self):
|
def letter(self):
|
||||||
return "n"
|
return "n"
|
||||||
|
|
||||||
def legal_moves(self, board: "Board") -> list["Move"]:
|
def legal_moves(self, board: "Board", / , looking_for_check = False) -> list["Move"]:
|
||||||
ret = []
|
ret = []
|
||||||
for dx, dy in [
|
for dx, dy in [
|
||||||
(+2, +1), (+1, +2), # north east
|
(+2, +1), (+1, +2), # north east
|
||||||
@ -15,5 +15,9 @@ class Knight(Piece):
|
|||||||
move = self._move_for_position(board, self.pos.x + dx, self.pos.y + dy)
|
move = self._move_for_position(board, self.pos.x + dx, self.pos.y + dy)
|
||||||
if move is not None:
|
if move is not None:
|
||||||
ret.append(move)
|
ret.append(move)
|
||||||
|
|
||||||
|
if not looking_for_check:# and board.is_check_for(self.colour):
|
||||||
|
return self.keep_only_blocking(ret, board)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
80
python/src/logic/pieces/pawn.py
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
from logic.move import Move
|
||||||
|
from logic.pieces.bishop import Bishop
|
||||||
|
from logic.pieces.knight import Knight
|
||||||
|
from logic.pieces.piece import Colour, Piece
|
||||||
|
from logic.pieces.queen import Queen
|
||||||
|
from logic.pieces.rook import Rook
|
||||||
|
from logic.position import Position
|
||||||
|
|
||||||
|
class Pawn(Piece):
|
||||||
|
def legal_moves(self, board, / , looking_for_check = False) -> list[Move]:
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
# can we capture to the left?
|
||||||
|
if self.pos.x > 0 and (
|
||||||
|
(self.colour == Colour.WHITE and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y + 1)))
|
||||||
|
or
|
||||||
|
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y - 1)))
|
||||||
|
):
|
||||||
|
if capturable_piece.colour != self.colour:
|
||||||
|
if (self.colour == Colour.WHITE and capturable_piece.pos.y == 7) or (self.colour == Colour.BLACK and capturable_piece.pos.y == 0):
|
||||||
|
for piece in [Queen, Knight, Bishop, Rook]:
|
||||||
|
ret.append(Move(self, capturable_piece.pos, is_capturing=True, promotes_to=piece))
|
||||||
|
else:
|
||||||
|
ret.append(Move(self, capturable_piece.pos, is_capturing = True))
|
||||||
|
|
||||||
|
# can we capture to the right?
|
||||||
|
if self.pos.x < 7 and (
|
||||||
|
(self.colour == Colour.WHITE and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y + 1)))
|
||||||
|
or
|
||||||
|
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y - 1)))
|
||||||
|
):
|
||||||
|
if capturable_piece.colour != self.colour:
|
||||||
|
if (self.colour == Colour.WHITE and capturable_piece.pos.y == 7) or (self.colour == Colour.BLACK and capturable_piece.pos.y == 0):
|
||||||
|
for piece in [Queen, Knight, Bishop, Rook]:
|
||||||
|
ret.append(Move(self, capturable_piece.pos, is_capturing=True, promotes_to=piece))
|
||||||
|
else:
|
||||||
|
ret.append(Move(self, capturable_piece.pos, is_capturing = True))
|
||||||
|
|
||||||
|
# -- Can we capture en passant?
|
||||||
|
if board._en_passant_target is not None and \
|
||||||
|
board._en_passant_target.pos.y == self.pos.y and (
|
||||||
|
board._en_passant_target.pos.x == self.pos.x - 1
|
||||||
|
or board._en_passant_target.pos.x == self.pos.x + 1
|
||||||
|
):
|
||||||
|
if board._en_passant_target.colour != self.colour:
|
||||||
|
old_pos = board._en_passant_target.pos
|
||||||
|
new_pos = Position(old_pos.x, old_pos.y + (1 if self.colour == Colour.WHITE else -1))
|
||||||
|
ret.append(Move(self, new_pos, is_capturing = True, en_passant = True))
|
||||||
|
|
||||||
|
# -- Normal moves
|
||||||
|
if self.colour == Colour.WHITE:
|
||||||
|
for dy in range(1, 3 if self.pos.y == 1 else 2):
|
||||||
|
y = self.pos.y + dy
|
||||||
|
if y > 7 or board.piece_at(self.pos.x, y):
|
||||||
|
break
|
||||||
|
pos = Position(self.pos.x, y)
|
||||||
|
if y == 7:
|
||||||
|
for piece in [Queen, Knight, Bishop, Rook]:
|
||||||
|
ret.append(Move(self, pos, promotes_to=piece))
|
||||||
|
else:
|
||||||
|
ret.append(Move(self, pos, becomes_en_passant_target=dy==2))
|
||||||
|
else:
|
||||||
|
for dy in range(1, 3 if self.pos.y == 6 else 2):
|
||||||
|
y = self.pos.y - dy
|
||||||
|
if y < 0 or board.piece_at(self.pos.x, y):
|
||||||
|
break
|
||||||
|
pos = Position(self.pos.x, y)
|
||||||
|
if y == 0:
|
||||||
|
for piece in [Queen, Knight, Bishop, Rook]:
|
||||||
|
ret.append(Move(self, pos, promotes_to=piece))
|
||||||
|
else:
|
||||||
|
ret.append(Move(self, pos, becomes_en_passant_target=dy==2))
|
||||||
|
|
||||||
|
if not looking_for_check:# and board.is_check_for(self.colour):
|
||||||
|
return self.keep_only_blocking(ret, board)
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def letter(self):
|
||||||
|
return "p"
|
@ -7,6 +7,9 @@ class Colour(Enum):
|
|||||||
WHITE = "white"
|
WHITE = "white"
|
||||||
BLACK = "black"
|
BLACK = "black"
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return self.value
|
||||||
|
|
||||||
class Piece:
|
class Piece:
|
||||||
def __init__(self, pos: Position, colour: Colour) -> None:
|
def __init__(self, pos: Position, colour: Colour) -> None:
|
||||||
self.pos = pos
|
self.pos = pos
|
||||||
@ -16,6 +19,14 @@ class Piece:
|
|||||||
def letter(self):
|
def letter(self):
|
||||||
return type(self).__name__[0].lower()
|
return type(self).__name__[0].lower()
|
||||||
|
|
||||||
|
def keep_only_blocking(self, candidates: list[Move], board: "Board") -> list[Move]:
|
||||||
|
ret = []
|
||||||
|
for move in candidates:
|
||||||
|
board_after_move = board.make_move(move)
|
||||||
|
if not board_after_move.is_check_for(self.colour):
|
||||||
|
ret.append(move)
|
||||||
|
return ret
|
||||||
|
|
||||||
def _look_direction(self, board: "Board", mult_dx: int, mult_dy: int):
|
def _look_direction(self, board: "Board", mult_dx: int, mult_dy: int):
|
||||||
ret = []
|
ret = []
|
||||||
for d in range(1, 8):
|
for d in range(1, 8):
|
||||||
@ -50,5 +61,5 @@ class Piece:
|
|||||||
ret = type(self)(pos, self.colour)
|
ret = type(self)(pos, self.colour)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def legal_moves(self, board: "Board") -> list["Move"]:
|
def legal_moves(self, board: "Board", / , looking_for_check = False) -> list["Move"]:
|
||||||
raise NotImplementedError(f"Can't say what the legal moves are for {type(self).__name__}, the method hasn't been implemented yet")
|
raise NotImplementedError(f"Can't say what the legal moves are for {type(self).__name__}, the method hasn't been implemented yet")
|
@ -2,7 +2,7 @@ from logic.move import Move
|
|||||||
from .piece import Piece
|
from .piece import Piece
|
||||||
|
|
||||||
class Queen(Piece):
|
class Queen(Piece):
|
||||||
def legal_moves(self, board: "Board") -> list[Move]:
|
def legal_moves(self, board: "Board", / , looking_for_check = False) -> list[Move]:
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
# looking north east
|
# looking north east
|
||||||
@ -29,4 +29,7 @@ class Queen(Piece):
|
|||||||
# looking north
|
# looking north
|
||||||
ret.extend(self._look_direction(board, 0, 1))
|
ret.extend(self._look_direction(board, 0, 1))
|
||||||
|
|
||||||
|
if not looking_for_check:# and board.is_check_for(self.colour):
|
||||||
|
return self.keep_only_blocking(ret, board)
|
||||||
|
|
||||||
return ret
|
return ret
|
@ -2,7 +2,7 @@ from logic.move import Move
|
|||||||
from .piece import Piece
|
from .piece import Piece
|
||||||
|
|
||||||
class Rook(Piece):
|
class Rook(Piece):
|
||||||
def legal_moves(self, board: "Board") -> list[Move]:
|
def legal_moves(self, board: "Board", / , looking_for_check = False) -> list[Move]:
|
||||||
ret = []
|
ret = []
|
||||||
|
|
||||||
# looking east
|
# looking east
|
||||||
@ -17,4 +17,7 @@ class Rook(Piece):
|
|||||||
# looking north
|
# looking north
|
||||||
ret.extend(self._look_direction(board, 0, 1))
|
ret.extend(self._look_direction(board, 0, 1))
|
||||||
|
|
||||||
|
if not looking_for_check:# and board.is_check_for(self.colour):
|
||||||
|
return self.keep_only_blocking(ret, board)
|
||||||
|
|
||||||
return ret
|
return ret
|
@ -17,6 +17,14 @@ class Position:
|
|||||||
return x >= Position._MIN_POS and x <= Position._MAX_POS \
|
return x >= Position._MIN_POS and x <= Position._MAX_POS \
|
||||||
and y >= Position._MIN_POS and y <= Position._MAX_POS
|
and y >= Position._MIN_POS and y <= Position._MAX_POS
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_algebraic(square: str) -> "Position":
|
||||||
|
assert len(square) == 2, f"'{square}' is malformed"
|
||||||
|
x = Position._FILES.index(square[0])
|
||||||
|
y = Position._RANKS.index(int(square[1]))
|
||||||
|
|
||||||
|
return Position(x, y)
|
||||||
|
|
||||||
def to_algebraic(self) -> str:
|
def to_algebraic(self) -> str:
|
||||||
return f"{Position._FILES[self.x]}{Position._RANKS[self.y]}"
|
return f"{Position._FILES[self.x]}{Position._RANKS[self.y]}"
|
||||||
|
|
27
python/src/main.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import time
|
||||||
|
from pprint import pprint
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
from ai.ai import move_generation_test
|
||||||
|
from controller.controller import Controller
|
||||||
|
from logic.board import INITIAL_BOARD, Board
|
||||||
|
from logic.position import Position
|
||||||
|
from view.gui import GUI
|
||||||
|
from view.tui import TUI
|
||||||
|
|
||||||
|
from ai.ai import peft
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
board = INITIAL_BOARD
|
||||||
|
|
||||||
|
pos = "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8"
|
||||||
|
board = Board.setup_FEN_position(pos)
|
||||||
|
|
||||||
|
view = GUI()
|
||||||
|
|
||||||
|
controller = Controller(board, view)
|
||||||
|
|
||||||
|
# view.show()
|
||||||
|
# exit()
|
||||||
|
|
||||||
|
peft(pos)
|
@ -1,4 +1,5 @@
|
|||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
|
from tkinter import messagebox
|
||||||
from typing import Type
|
from typing import Type
|
||||||
from PIL import ImageTk, Image
|
from PIL import ImageTk, Image
|
||||||
import os
|
import os
|
||||||
@ -60,6 +61,12 @@ class GUI(View):
|
|||||||
|
|
||||||
self._controller.on_tile_selected(x, y)
|
self._controller.on_tile_selected(x, y)
|
||||||
|
|
||||||
|
def notify_checkmate(self, colour: Colour) -> None:
|
||||||
|
messagebox.showinfo("Checkmate", f"{colour} is currently checkmated")
|
||||||
|
|
||||||
|
def notify_stalemate(self, colour: Colour) -> None:
|
||||||
|
messagebox.showinfo("Stalemate", f"{colour} is currently stalemated")
|
||||||
|
|
||||||
|
|
||||||
def update_board(self, board: Board, selected_piece: Piece, legal_moves: list[Move]) -> None:
|
def update_board(self, board: Board, selected_piece: Piece, legal_moves: list[Move]) -> None:
|
||||||
self.canvas.delete("all")
|
self.canvas.delete("all")
|
@ -1,6 +1,6 @@
|
|||||||
from logic.board import Board
|
from logic.board import Board
|
||||||
from logic.move import Move
|
from logic.move import Move
|
||||||
from logic.pieces.piece import Piece
|
from logic.pieces.piece import Colour, Piece
|
||||||
|
|
||||||
|
|
||||||
class View:
|
class View:
|
||||||
@ -13,6 +13,12 @@ class View:
|
|||||||
def update_board(self, board: Board, selected_piece: Piece, legal_moves: list[Move]) -> None:
|
def update_board(self, board: Board, selected_piece: Piece, legal_moves: list[Move]) -> None:
|
||||||
raise NotImplementedError(f"Can't update the board, the update_board() method of {type(self)} is not implemented")
|
raise NotImplementedError(f"Can't update the board, the update_board() method of {type(self)} is not implemented")
|
||||||
|
|
||||||
|
def notify_checkmate(self, colour: Colour) -> None:
|
||||||
|
raise NotImplementedError(f"Can't notify of the checkmate, the notify_checkmate() method of {type(self)} is not implemented")
|
||||||
|
|
||||||
|
def notify_stalemate(self, colour: Colour) -> None:
|
||||||
|
raise NotImplementedError(f"Can't notify of the stalemate, the notify_stalemate() method of {type(self)} is not implemented")
|
||||||
|
|
||||||
def set_controller(self, controller: "Controller") -> None:
|
def set_controller(self, controller: "Controller") -> None:
|
||||||
self._controller = controller
|
self._controller = controller
|
||||||
|
|
30
python/tests/fen.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
import sys
|
||||||
|
sys.path.append('src') # you must execute pytest from the stickfosh dir for this to work
|
||||||
|
|
||||||
|
from logic.board import Board
|
||||||
|
|
||||||
|
class FENTests(unittest.TestCase):
|
||||||
|
def testInitialPosition(self):
|
||||||
|
pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
||||||
|
self.assertEqual(pos, Board.setup_FEN_position(pos).to_fen_string())
|
||||||
|
|
||||||
|
def testRandomPositions(self):
|
||||||
|
pos = "r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1 b Qk - 0 1"
|
||||||
|
self.assertEqual(pos, Board.setup_FEN_position(pos).to_fen_string())
|
||||||
|
|
||||||
|
pos = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"
|
||||||
|
self.assertEqual(pos, Board.setup_FEN_position(pos).to_fen_string())
|
||||||
|
|
||||||
|
pos = "4k2r/6r1/8/8/8/8/3R4/R3K3 w Qk - 0 1"
|
||||||
|
self.assertEqual(pos, Board.setup_FEN_position(pos).to_fen_string())
|
||||||
|
|
||||||
|
pos = "8/8/8/4p1K1/2k1P3/8/8/8 b - - 0 1"
|
||||||
|
self.assertEqual(pos, Board.setup_FEN_position(pos).to_fen_string())
|
||||||
|
|
||||||
|
pos = "8/5k2/3p4/1p1Pp2p/pP2Pp1P/P4P1K/8/8 b - - 99 50"
|
||||||
|
self.assertEqual(pos, Board.setup_FEN_position(pos).to_fen_string())
|
||||||
|
|
||||||
|
|
||||||
|
|
30
python/tests/position.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
import sys
|
||||||
|
sys.path.append('src') # you must execute pytest from the stickfosh dir for this to work
|
||||||
|
|
||||||
|
from logic.position import Position
|
||||||
|
|
||||||
|
class PositionTests(unittest.TestCase):
|
||||||
|
def testXY2Algebraic(self):
|
||||||
|
self.assertEqual(Position(0, 0).to_algebraic(), "a1")
|
||||||
|
self.assertEqual(Position(1, 0).to_algebraic(), "b1")
|
||||||
|
|
||||||
|
self.assertEqual(Position(2, 1).to_algebraic(), "c2")
|
||||||
|
self.assertEqual(Position(4, 2).to_algebraic(), "e3")
|
||||||
|
|
||||||
|
self.assertEqual(Position(7, 7).to_algebraic(), "h8")
|
||||||
|
|
||||||
|
def testAlgebraic2XY(self):
|
||||||
|
self.assertEqual(Position.from_algebraic("a1"), Position(0, 0))
|
||||||
|
self.assertEqual(Position.from_algebraic("b1"), Position(1, 0))
|
||||||
|
|
||||||
|
self.assertEqual(Position.from_algebraic("c2"), Position(2, 1))
|
||||||
|
self.assertEqual(Position.from_algebraic("e3"), Position(4, 2))
|
||||||
|
|
||||||
|
self.assertEqual(Position.from_algebraic("h8"), Position(7, 7))
|
||||||
|
|
||||||
|
self.assertRaises(AssertionError, lambda : Position.from_algebraic("a11"))
|
||||||
|
|
||||||
|
self.assertRaises(ValueError, lambda : Position.from_algebraic("j1"))
|
||||||
|
self.assertRaises(ValueError, lambda : Position.from_algebraic("a9"))
|
@ -1,198 +0,0 @@
|
|||||||
from logic.move import CastleSide, Move
|
|
||||||
from logic.pieces.bishop import Bishop
|
|
||||||
from logic.pieces.king import King
|
|
||||||
from logic.pieces.knight import Knight
|
|
||||||
from logic.pieces.queen import Queen
|
|
||||||
from logic.pieces.rook import Rook
|
|
||||||
from logic.pieces.pawn import Pawn
|
|
||||||
from logic.pieces.piece import Colour, Piece
|
|
||||||
from logic.position import Position
|
|
||||||
|
|
||||||
from typing import Type
|
|
||||||
|
|
||||||
class Board:
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self._white: dict[Position, Piece] = {}
|
|
||||||
self._black: dict[Position, Piece] = {}
|
|
||||||
self._turn = None
|
|
||||||
self._white_castling_write = set()
|
|
||||||
self._black_castling_write = set()
|
|
||||||
self._en_passant_target = None
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _piece_class_from_char(c: str) -> Type[Piece]:
|
|
||||||
assert len(c) == 1, f"The piece {c} isn't denoted by 1 character"
|
|
||||||
c = c.lower()
|
|
||||||
if c == "p":
|
|
||||||
return Pawn
|
|
||||||
if c == "r":
|
|
||||||
return Rook
|
|
||||||
if c == "n":
|
|
||||||
return Knight
|
|
||||||
if c == "b":
|
|
||||||
return Bishop
|
|
||||||
if c == "q":
|
|
||||||
return Queen
|
|
||||||
if c == "k":
|
|
||||||
return King
|
|
||||||
raise ValueError(f"Unknown piece '{c}'")
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def setup_FEN_position(position: str) -> "Board":
|
|
||||||
ret = Board()
|
|
||||||
index = 0
|
|
||||||
|
|
||||||
# -- Pieces
|
|
||||||
pieces = "prnbqk" # possible pieces
|
|
||||||
numbers = "12345678" # possible number of empty squares
|
|
||||||
|
|
||||||
x = 0
|
|
||||||
y = 7 # FEN starts from the top left, so 8th rank
|
|
||||||
for c in position:
|
|
||||||
index += 1
|
|
||||||
if c == " ":
|
|
||||||
break
|
|
||||||
if c in pieces or c in pieces.upper():
|
|
||||||
pos = Position(x, y)
|
|
||||||
piece = Board._piece_class_from_char(c)
|
|
||||||
if c.isupper():
|
|
||||||
ret._white[pos] = piece(pos, Colour.WHITE)
|
|
||||||
else:
|
|
||||||
ret._black[pos] = piece(pos, Colour.BLACK)
|
|
||||||
|
|
||||||
x += 1
|
|
||||||
continue
|
|
||||||
if c in numbers:
|
|
||||||
x += int(c)
|
|
||||||
if c == '/':
|
|
||||||
x = 0
|
|
||||||
y -= 1
|
|
||||||
|
|
||||||
|
|
||||||
# -- Active colour
|
|
||||||
if position[index] == "w":
|
|
||||||
ret._turn = Colour.WHITE
|
|
||||||
elif position[index] == "b":
|
|
||||||
ret._turn = Colour.BLACK
|
|
||||||
else:
|
|
||||||
raise ValueError(f"The FEN position is malformed, the active colour should be either 'w' or 'b', but is '{position[index]}'")
|
|
||||||
index += 2
|
|
||||||
|
|
||||||
|
|
||||||
# -- Castling Rights
|
|
||||||
for c in position[index:]:
|
|
||||||
index += 1
|
|
||||||
if c == "-" or c == " ":
|
|
||||||
break
|
|
||||||
|
|
||||||
sides = "kq"
|
|
||||||
assert c in sides or c in sides.upper(), f"The FEN position is malformed, the castling rights should be either k or q (both either lower- or upper-case), instead is '{c}'"
|
|
||||||
if c == "K":
|
|
||||||
ret._white_castling_write.add(CastleSide.King)
|
|
||||||
if c == "Q":
|
|
||||||
ret._white_castling_write.add(CastleSide.Queen)
|
|
||||||
if c == "k":
|
|
||||||
ret._black_castling_write.add(CastleSide.King)
|
|
||||||
if c == "q":
|
|
||||||
ret._black_castling_write.add(CastleSide.Queen)
|
|
||||||
|
|
||||||
# -- En passant target
|
|
||||||
if position[index] != "-":
|
|
||||||
ret._en_passant_target = position[index:index+2]
|
|
||||||
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def piece_at(self, x: int, y: int) -> Piece | None:
|
|
||||||
pos = Position(x, y)
|
|
||||||
white_piece = self._white.get(pos, None)
|
|
||||||
black_piece = self._black.get(pos, None)
|
|
||||||
|
|
||||||
assert white_piece == None or black_piece == None, f"There are two pieces at the same position {pos}, this shouldn't happen!"
|
|
||||||
|
|
||||||
if white_piece != None:
|
|
||||||
return white_piece
|
|
||||||
return black_piece
|
|
||||||
|
|
||||||
def is_check_for(self, colour: Colour) -> bool:
|
|
||||||
""" Is it check for the defending colour passed as parameter """
|
|
||||||
defending_pieces, attacking_pieces = (self._white, self._black) if colour == Colour.WHITE else (self._black, self._white)
|
|
||||||
|
|
||||||
kings = [piece for piece in defending_pieces.values() if type(piece) == King]
|
|
||||||
assert len(kings) == 1, f"We have more than one king for {colour}, that is no buono..."
|
|
||||||
king = kings[0]
|
|
||||||
|
|
||||||
for piece in attacking_pieces.values():
|
|
||||||
possible_pos = []
|
|
||||||
if type(piece) == King:
|
|
||||||
# special case for the king, because it creates infinite recursion (since he looks if he's walking into a check)
|
|
||||||
for dx in [-1, 0, 1]:
|
|
||||||
for dy in [-1, 0, 1]:
|
|
||||||
x, y = piece.pos.x + dx, piece.pos.y + dy
|
|
||||||
if Position.is_within_bounds(x, y):
|
|
||||||
possible_pos.append(Position(x, y))
|
|
||||||
else:
|
|
||||||
possible_pos += [move.pos for move in piece.legal_moves(self)]
|
|
||||||
if king.pos in possible_pos:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def castling_writes_for(self, colour: Colour) -> set[CastleSide]:
|
|
||||||
return self._white_castling_write if colour == Colour.WHITE else self._black_castling_write
|
|
||||||
|
|
||||||
def make_move(self, move: Move) -> "Board":
|
|
||||||
dest_piece = self.piece_at(move.pos.x, move.pos.y)
|
|
||||||
|
|
||||||
if dest_piece:
|
|
||||||
assert dest_piece.colour != move.piece.colour, "A piece cannot cannot eat another piece of the same colour"
|
|
||||||
|
|
||||||
ret = Board()
|
|
||||||
ret._white = self._white.copy()
|
|
||||||
ret._black = self._black.copy()
|
|
||||||
ret._turn = Colour.WHITE if self._turn == Colour.BLACK else Colour.BLACK
|
|
||||||
ret._white_castling_write = self._white_castling_write.copy()
|
|
||||||
ret._black_castling_write = self._black_castling_write.copy()
|
|
||||||
ret._en_passant_target = self._en_passant_target
|
|
||||||
|
|
||||||
piece = move.piece
|
|
||||||
pieces_moving, other_pieces = (ret._white, ret._black) if piece.colour == Colour.WHITE else (ret._black, ret._white)
|
|
||||||
|
|
||||||
del pieces_moving[piece.pos]
|
|
||||||
pieces_moving[move.pos] = piece.move_to(move.pos)
|
|
||||||
if move.pos in other_pieces:
|
|
||||||
del other_pieces[move.pos]
|
|
||||||
|
|
||||||
if move.castle_side == CastleSide.King:
|
|
||||||
rook_pos = Position(7, piece.pos.y)
|
|
||||||
assert rook_pos in pieces_moving and type(pieces_moving[rook_pos]) == Rook, "Either rook is absent from the king side or you are trying to castle with something else than a rook..."
|
|
||||||
del pieces_moving[rook_pos]
|
|
||||||
new_rook_pos = Position(5, piece.pos.y)
|
|
||||||
pieces_moving[new_rook_pos] = Rook(new_rook_pos, piece.colour)
|
|
||||||
|
|
||||||
elif move.castle_side == CastleSide.Queen:
|
|
||||||
rook_pos = Position(0, piece.pos.y)
|
|
||||||
assert rook_pos in pieces_moving and type(pieces_moving[rook_pos]) == Rook, "Either rook is absent from the queen side or you are trying to castle with something else than a rook..."
|
|
||||||
del pieces_moving[rook_pos]
|
|
||||||
new_rook_pos = Position(3, piece.pos.y)
|
|
||||||
pieces_moving[new_rook_pos] = Rook(new_rook_pos, piece.colour)
|
|
||||||
|
|
||||||
if piece.colour == Colour.WHITE:
|
|
||||||
if type(piece) == King:
|
|
||||||
ret._white_castling_write = set()
|
|
||||||
|
|
||||||
if type(piece) == Rook:
|
|
||||||
if piece.pos.x == 0 and CastleSide.Queen in ret._white_castling_write:
|
|
||||||
ret._white_castling_write.remove(CastleSide.Queen)
|
|
||||||
elif piece.pos.x == 7 and CastleSide.King in ret._white_castling_write:
|
|
||||||
ret._white_castling_write.remove(CastleSide.King)
|
|
||||||
else:
|
|
||||||
if type(piece) == King:
|
|
||||||
ret._black_castling_write = set()
|
|
||||||
|
|
||||||
if type(piece) == Rook:
|
|
||||||
if piece.pos.x == 0 and CastleSide.Queen in ret._black_castling_write:
|
|
||||||
ret._black_castling_write.remove(CastleSide.Queen)
|
|
||||||
elif piece.pos.x == 7 and CastleSide.King in ret._black_castling_write:
|
|
||||||
ret._black_castling_write.remove(CastleSide.King)
|
|
||||||
|
|
||||||
|
|
||||||
return ret
|
|
@ -1,40 +0,0 @@
|
|||||||
from logic.move import Move
|
|
||||||
from logic.pieces.piece import Colour, Piece
|
|
||||||
from logic.position import Position
|
|
||||||
|
|
||||||
class Pawn(Piece):
|
|
||||||
def legal_moves(self, board) -> list[Move]:
|
|
||||||
ret = []
|
|
||||||
|
|
||||||
# can we capture to the left?
|
|
||||||
if self.pos.x > 0 and (
|
|
||||||
(self.colour == Colour.WHITE and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y + 1)))
|
|
||||||
or
|
|
||||||
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x - 1, self.pos.y - 1)))
|
|
||||||
):
|
|
||||||
if capturable_piece.colour != self.colour:
|
|
||||||
ret.append(Move(self, capturable_piece.pos, is_capturing = True))
|
|
||||||
|
|
||||||
# can we capture to the right?
|
|
||||||
if self.pos.x < 7 and (
|
|
||||||
(self.colour == Colour.WHITE and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y + 1)))
|
|
||||||
or
|
|
||||||
(self.colour == Colour.BLACK and (capturable_piece := board.piece_at(self.pos.x + 1, self.pos.y - 1)))
|
|
||||||
):
|
|
||||||
if capturable_piece.colour != self.colour:
|
|
||||||
ret.append(Move(self, capturable_piece.pos, is_capturing = True))
|
|
||||||
|
|
||||||
if self.colour == Colour.WHITE:
|
|
||||||
for dy in range(1, 3 if self.pos.y == 1 else 2):
|
|
||||||
if self.pos.y + dy > 7 or board.piece_at(self.pos.x, self.pos.y + dy):
|
|
||||||
break
|
|
||||||
pos = Position(self.pos.x, self.pos.y + dy)
|
|
||||||
ret.append(Move(self, pos))
|
|
||||||
else:
|
|
||||||
for dy in range(1, 3 if self.pos.y == 6 else 2):
|
|
||||||
if self.pos.y - dy < 0 or board.piece_at(self.pos.x, self.pos.y - dy):
|
|
||||||
break
|
|
||||||
pos = Position(self.pos.x, self.pos.y - dy)
|
|
||||||
ret.append(Move(self, pos))
|
|
||||||
|
|
||||||
return ret
|
|
14
src/main.py
@ -1,14 +0,0 @@
|
|||||||
from controller.controller import Controller
|
|
||||||
from logic.board import Board
|
|
||||||
from view.gui import GUI
|
|
||||||
from view.tui import TUI
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
initial_board_position = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
|
||||||
board = Board.setup_FEN_position(initial_board_position)
|
|
||||||
|
|
||||||
view = GUI()
|
|
||||||
|
|
||||||
controller = Controller(board, view)
|
|
||||||
|
|
||||||
view.show()
|
|