implemented promotion for white

This commit is contained in:
Karma Riuk 2025-02-07 19:31:35 +01:00
parent 3bc5b75f1e
commit 2260cd918a
5 changed files with 91 additions and 4 deletions

View File

@ -51,6 +51,14 @@ void ManualController::show_legal_moves(Coords xy) {
} }
void ManualController::make_move(Move move) { void ManualController::make_move(Move move) {
// handle promotion before making the move
Colour colour = board.white_to_play ? White : Black;
Coords source = Coords::from_index(move.source_square);
Piece promotion_piece = Piece::None;
if (board.piece_at(move.source_square) == Piece::Pawn
&& board.colour_at(move.source_square) == White && source.y == 6)
promotion_piece = (Piece) (colour | view.ask_about_promotion());
move.promoting_to = promotion_piece;
board = board.make_move(move); board = board.make_move(move);
reset_selection(); reset_selection();

View File

@ -11,11 +11,9 @@
#include <chrono> #include <chrono>
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::string pos =
"r2qkb1r/2p1pppp/p1n1b3/1p6/B2P4/2P1P3/P4PPP/R1BQK1NR w KQkq - 0 9 ";
// std::string pos = // std::string pos =
// "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 4 3 // "r2qkb1r/2p1pppp/p1n1b3/1p6/B2P4/2P1P3/P4PPP/R1BQK1NR w KQkq - 0 9 ";
// "; std::string pos = "8/6K1/5P2/8/1k6/8/8/8 w - - 0 1";
// pos for ai timing< // pos for ai timing<
// std::string pos = // std::string pos =

View File

@ -134,6 +134,81 @@ void GUI::draw_pieces(const Board& board) {
} }
} }
int GUI::show_popup(
const std::string& message, const std::vector<std::string>& options
) {
sf::RenderWindow popup(sf::VideoMode(300, 200), "Choice");
sf::Font font;
if (!font.loadFromFile("res/arial.ttf")) {
std::cerr << "Error: Could not load font!" << std::endl;
return -1;
}
sf::Text text(message, font, 20);
text.setPosition(20, 20);
text.setFillColor(sf::Color::Black);
std::vector<sf::RectangleShape> buttonShapes;
std::vector<sf::Text> buttonTexts;
for (size_t i = 0; i < options.size(); ++i) {
sf::RectangleShape button(sf::Vector2f(200, 30));
button.setPosition(50, 70 + i * 40);
button.setFillColor(sf::Color(150, 150, 150));
buttonShapes.push_back(button);
sf::Text buttonText(options[i], font, 18);
buttonText.setPosition(60, 75 + i * 40);
buttonText.setFillColor(sf::Color::Black);
buttonTexts.push_back(buttonText);
}
while (popup.isOpen()) {
sf::Event event;
while (popup.pollEvent(event)) {
if (event.type == sf::Event::Closed)
popup.close();
else if (event.type == sf::Event::MouseButtonPressed) {
for (size_t i = 0; i < buttonShapes.size(); ++i) {
if (buttonShapes[i].getGlobalBounds().contains(
event.mouseButton.x,
event.mouseButton.y
)) {
popup.close();
return i;
}
}
}
}
popup.clear(sf::Color::White);
popup.draw(text);
for (size_t i = 0; i < buttonShapes.size(); ++i) {
popup.draw(buttonShapes[i]);
popup.draw(buttonTexts[i]);
}
popup.display();
}
return -1;
}
Piece GUI::ask_about_promotion() {
std::vector<std::string> options = {"Queen", "Rook", "Bishop", "Knight"};
int idx = show_popup("Please choose a promotion for your pawn", options);
switch (idx) {
case 0:
return Queen;
case 1:
return Rook;
case 2:
return Bishop;
case 3:
return Knigt;
};
return Piece::None;
}
void GUI::show() { void GUI::show() {
while (window.isOpen()) while (window.isOpen())
handle_events(); handle_events();

View File

@ -15,6 +15,7 @@ class GUI : public View {
GUI(); GUI();
void show() override; void show() override;
Piece ask_about_promotion();
void update_board(const Board&, int8_t, std::vector<int8_t>) override; void update_board(const Board&, int8_t, std::vector<int8_t>) override;
void notify_checkmate(Colour) override; void notify_checkmate(Colour) override;
void notify_stalemate(Colour) override; void notify_stalemate(Colour) override;
@ -27,6 +28,10 @@ class GUI : public View {
sf::Color colours[2] = {sf::Color(0xB88762FF), sf::Color(0xEDD6B0FF)}; sf::Color colours[2] = {sf::Color(0xB88762FF), sf::Color(0xEDD6B0FF)};
sf::Color alt_colours[2] = {sf::Color(0xDCC34BFF), sf::Color(0xF6EB72FF)}; sf::Color alt_colours[2] = {sf::Color(0xDCC34BFF), sf::Color(0xF6EB72FF)};
int show_popup(
const std::string& message, const std::vector<std::string>& options
);
void load_textures(); void load_textures();
void handle_events(); void handle_events();
void handle_click(int, int); void handle_click(int, int);

View File

@ -15,6 +15,7 @@ class View {
} }
virtual void show() = 0; virtual void show() = 0;
virtual Piece ask_about_promotion() = 0;
virtual void update_board(const Board&, int8_t, std::vector<int8_t>) = 0; virtual void update_board(const Board&, int8_t, std::vector<int8_t>) = 0;
virtual void notify_checkmate(Colour) = 0; virtual void notify_checkmate(Colour) = 0;
virtual void notify_stalemate(Colour) = 0; virtual void notify_stalemate(Colour) = 0;