fixed board and pawn moves
This commit is contained in:
parent
fea3f6c98a
commit
e08dbb913e
@ -1,7 +1,11 @@
|
||||
#include "board.hpp"
|
||||
|
||||
#include "pieces/piece.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
|
||||
Board Board::setup_fen_position(std::string fen) {
|
||||
Board board;
|
||||
@ -37,7 +41,7 @@ Board Board::setup_fen_position(std::string fen) {
|
||||
return board;
|
||||
}
|
||||
|
||||
std::string Board::to_fen() {
|
||||
std::string Board::to_fen() const {
|
||||
std::map<int, char> p2c{
|
||||
{Piece::King, 'k'},
|
||||
{Piece::Pawn, 'p'},
|
||||
@ -76,7 +80,7 @@ std::string Board::to_fen() {
|
||||
return ret;
|
||||
}
|
||||
|
||||
Board Board::make_move(Move move) {
|
||||
Board Board::make_move(Move move) const {
|
||||
int8_t dest_piece = this->squares[move.target_square];
|
||||
|
||||
Board ret;
|
||||
@ -96,3 +100,33 @@ Board Board::make_move(Move move) {
|
||||
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;
|
||||
}
|
||||
|
@ -17,12 +17,18 @@ struct Coords {
|
||||
};
|
||||
|
||||
struct Board {
|
||||
private:
|
||||
int8_t get_king_of(int8_t colour) const;
|
||||
|
||||
public:
|
||||
int8_t squares[64] = {Piece::None};
|
||||
bool white_to_play;
|
||||
int8_t w_castle_rights;
|
||||
int8_t b_castle_rights;
|
||||
|
||||
static Board setup_fen_position(std::string fen);
|
||||
|
||||
Board make_move(Move);
|
||||
std::string to_fen();
|
||||
Board make_move(Move) const;
|
||||
std::string to_fen() const;
|
||||
bool is_check_for(int8_t) const;
|
||||
};
|
||||
|
@ -2,11 +2,23 @@
|
||||
|
||||
#include "../board.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.squares[candidates[0].source_square] & 0b11000;
|
||||
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(
|
||||
const Piece p,
|
||||
const Board& b,
|
||||
const Coords xy,
|
||||
bool looking_for_check = false
|
||||
int8_t p, const Board& b, const Coords xy, bool looking_for_check = false
|
||||
) {
|
||||
std::vector<Move> ret;
|
||||
switch (p) {
|
||||
|
@ -23,7 +23,8 @@ enum Colour : int8_t {
|
||||
class Board;
|
||||
struct Coords;
|
||||
|
||||
std::vector<Move> legal_moves(const Piece, const Board&, const Coords, bool);
|
||||
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::vector<Move> pawn_moves(const Board&, const Coords);
|
||||
std::vector<Move> rook_moves(const Board&, const Coords);
|
||||
|
Loading…
x
Reference in New Issue
Block a user