implemented (partially) the pawn move

This commit is contained in:
Karma Riuk 2025-02-02 18:17:37 +01:00
parent 95327ec653
commit da55f0085f
4 changed files with 61 additions and 12 deletions

View File

@ -1,11 +1,15 @@
#pragma once
#include "castle_side.hpp"
#include <cstdint>
struct Move {
int8_t piece;
int8_t source_square;
int8_t target_square;
bool is_capturing = false;
CastleSide castle_side = CastleSide::Neither;
bool en_passant = false;
int8_t promoting_to = 0;
};

View File

@ -1,6 +1,50 @@
#include "../board.hpp"
#include "../move.hpp"
#include "piece.hpp"
std::vector<Move> pawn_moves(Board b) {
return {};
std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
std::vector<Move> ret{};
int8_t me = b.squares[xy.to_index()];
int8_t my_colour = me & 0b11000;
// -- 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 != (capturable_piece & 0b11000))
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 != (capturable_piece & 0b11000))
ret.push_back(Move{xy.to_index(), right.to_index()});
}
}
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;
}

View File

@ -2,10 +2,10 @@
#include "../board.hpp"
std::vector<Move> legal_moves(Piece p, Board b) {
std::vector<Move> legal_moves(const Piece p, const Board& b, const Coords xy) {
switch (p) {
case Piece::Pawn:
return pawn_moves(b);
return pawn_moves(b, xy);
case Piece::Bishop:
break;
default:

View File

@ -21,12 +21,13 @@ enum Colour : int8_t {
};
class Board;
struct Coords;
std::vector<Move> legal_moves(Piece p, Board b);
std::vector<Move> legal_moves(const Piece, const Board&, const Coords);
std::vector<Move> pawn_moves(Board b);
std::vector<Move> rook_moves(Board b);
std::vector<Move> knight_moves(Board b);
std::vector<Move> bishop_moves(Board b);
std::vector<Move> queen_moves(Board b);
std::vector<Move> king_moves(Board b);
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);