diff --git a/cpp/src/move.hpp b/cpp/src/move.hpp index b465e98..2f95ef6 100644 --- a/cpp/src/move.hpp +++ b/cpp/src/move.hpp @@ -1,11 +1,15 @@ +#pragma once + #include "castle_side.hpp" #include 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; }; diff --git a/cpp/src/pieces/pawn.cpp b/cpp/src/pieces/pawn.cpp index 85e9b77..044715e 100644 --- a/cpp/src/pieces/pawn.cpp +++ b/cpp/src/pieces/pawn.cpp @@ -1,6 +1,50 @@ #include "../board.hpp" +#include "../move.hpp" #include "piece.hpp" -std::vector pawn_moves(Board b) { - return {}; +std::vector pawn_moves(const Board& b, const Coords xy) { + std::vector 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; } diff --git a/cpp/src/pieces/piece.cpp b/cpp/src/pieces/piece.cpp index 1e6611f..533b641 100644 --- a/cpp/src/pieces/piece.cpp +++ b/cpp/src/pieces/piece.cpp @@ -2,10 +2,10 @@ #include "../board.hpp" -std::vector legal_moves(Piece p, Board b) { +std::vector 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: diff --git a/cpp/src/pieces/piece.hpp b/cpp/src/pieces/piece.hpp index 6faef78..54eb7fd 100644 --- a/cpp/src/pieces/piece.hpp +++ b/cpp/src/pieces/piece.hpp @@ -21,12 +21,13 @@ enum Colour : int8_t { }; class Board; +struct Coords; -std::vector legal_moves(Piece p, Board b); +std::vector legal_moves(const Piece, const Board&, const Coords); -std::vector pawn_moves(Board b); -std::vector rook_moves(Board b); -std::vector knight_moves(Board b); -std::vector bishop_moves(Board b); -std::vector queen_moves(Board b); -std::vector king_moves(Board b); +std::vector pawn_moves(const Board&, const Coords); +std::vector rook_moves(const Board&, const Coords); +std::vector knight_moves(const Board&, const Coords); +std::vector bishop_moves(const Board&, const Coords); +std::vector queen_moves(const Board&, const Coords); +std::vector king_moves(const Board&, const Coords);