From 703dcef59f18a8cf7b8c696b7738c416a06c0cc4 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sun, 2 Feb 2025 20:24:42 +0100 Subject: [PATCH] implemented knight legal moves --- cpp/src/pieces/knight.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cpp/src/pieces/knight.cpp diff --git a/cpp/src/pieces/knight.cpp b/cpp/src/pieces/knight.cpp new file mode 100644 index 0000000..e38c0c5 --- /dev/null +++ b/cpp/src/pieces/knight.cpp @@ -0,0 +1,28 @@ +#include "../board.hpp" +#include "../coords.hpp" +#include "../move.hpp" +#include "piece.hpp" + +#include + +std::vector knight_moves(const Board& b, const Coords xy) { + std::vector ret; + std::vector> 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_for_position(b, xy, Coords{xy.x + dx, xy.y + dy}); + if (move.has_value()) + ret.push_back(move.value()); + } + return ret; +}