diff --git a/cpp/src/controller/manual.cpp b/cpp/src/controller/manual.cpp index 53ea751..f02fe75 100644 --- a/cpp/src/controller/manual.cpp +++ b/cpp/src/controller/manual.cpp @@ -1,17 +1,61 @@ #include "manual.hpp" +#include "../model/utils/utils.hpp" + +#include + ManualController::ManualController(Board b, View& view): view(view) { board = b; - selected_piece = Piece::None; - legal_moves = {}; - - view.update_board(b, selected_piece, legal_moves); + view.set_controller(this); + reset_selection(); } -void ManualController::on_tile_selected(int x, int y) {} +void ManualController::on_tile_selected(int x, int y) { + Coords c{x, y}; + Piece piece = board.piece_at(c); -void ManualController::reset_selection() {} + std::cout << "Clicked on " << c << std::endl; + if (selected_index == -1 + || (piece != Piece::None && piece != selected_piece + && (piece & 0b11000) != (selected_piece & 0b11000))) { + show_legal_moves(c); + } else { + auto boh = std::find(targets.begin(), targets.end(), c.to_index()); + if (boh != targets.end()) + make_move(Move{selected_index, c.to_index()}); + else + reset_selection(); + } -void ManualController::show_legal_moves(Coords) {} + Colour current_colour = board.white_to_play ? White : Black; + if (board.is_checkmate_for(current_colour)) + view.notify_checkmate(current_colour); -void ManualController::make_move(Move) {} + if (board.is_stalemate_for(current_colour)) + view.notify_stalemate(current_colour); +} + +void ManualController::reset_selection() { + selected_index = -1; + selected_piece = Piece::None; + targets = {}; + view.update_board(board, selected_index, targets); +} + +void ManualController::show_legal_moves(Coords xy) { + Colour colour = board.colour_at(xy); + if (colour) { + Colour to_play = board.white_to_play ? White : Black; + if (colour != to_play) + return; + selected_index = xy.to_index(); + selected_piece = board.piece_at(xy); + targets = to_target_square(legal_moves(selected_piece, board, xy)); + view.update_board(board, xy.to_index(), targets); + } +} + +void ManualController::make_move(Move move) { + board = board.make_move(move); + reset_selection(); +} diff --git a/cpp/src/controller/manual.hpp b/cpp/src/controller/manual.hpp index 684e4b9..2b349e2 100644 --- a/cpp/src/controller/manual.hpp +++ b/cpp/src/controller/manual.hpp @@ -8,8 +8,9 @@ class ManualController : public Controller { private: View& view; + int8_t selected_index; Piece selected_piece; - std::vector legal_moves; + std::vector targets; void reset_selection(); void show_legal_moves(Coords); diff --git a/cpp/src/view/gui.cpp b/cpp/src/view/gui.cpp index d5ddb67..a80ddd1 100644 --- a/cpp/src/view/gui.cpp +++ b/cpp/src/view/gui.cpp @@ -13,17 +13,21 @@ GUI::GUI() { } void GUI::update_board( - const Board& b, int8_t selected_square, std::vector legal_moves + const Board& b, int8_t selected_square, std::vector targets ) { window.clear(); - draw_board(selected_square, legal_moves); + draw_board(selected_square, targets); draw_pieces(b); window.display(); } -void GUI::notify_stalemate() {} +void GUI::notify_stalemate(Colour col) { + std::cout << "Stalemate for " << to_string(col) << std::endl; +} -void GUI::notify_checkmate() {} +void GUI::notify_checkmate(Colour col) { + std::cout << "Checkmate for " << to_string(col) << std::endl; +} void GUI::handle_events() { sf::Event event; @@ -77,7 +81,7 @@ void GUI::draw_annotation(int file, int rank) { } } -void GUI::draw_board(int selected_square, std::vector moves) { +void GUI::draw_board(int selected_square, std::vector targets) { sf::RectangleShape square(sf::Vector2f(TILE_SIZE, TILE_SIZE)); for (int rank = 0; rank < 8; ++rank) { for (int file = 0; file < 8; ++file) { @@ -94,7 +98,6 @@ void GUI::draw_board(int selected_square, std::vector moves) { window.draw(square); draw_annotation(file, rank); - std::vector targets = to_target_square(moves); if (std::find(targets.begin(), targets.end(), index) != targets.end()) { float r = .15 * TILE_SIZE; diff --git a/cpp/src/view/gui.hpp b/cpp/src/view/gui.hpp index 718b0a2..d134084 100644 --- a/cpp/src/view/gui.hpp +++ b/cpp/src/view/gui.hpp @@ -15,9 +15,9 @@ class GUI : public View { GUI(); void show() override; - void update_board(const Board&, int8_t, std::vector) override; - void notify_checkmate() override; - void notify_stalemate() override; + void update_board(const Board&, int8_t, std::vector) override; + void notify_checkmate(Colour) override; + void notify_stalemate(Colour) override; private: sf::RenderWindow window; @@ -30,7 +30,7 @@ class GUI : public View { void load_textures(); void handle_events(); void handle_click(int, int); - void draw_board(int, std::vector); + void draw_board(int, std::vector); void draw_pieces(const Board&); void draw_annotation(int, int); }; diff --git a/cpp/src/view/view.hpp b/cpp/src/view/view.hpp index 6d03782..3713d2b 100644 --- a/cpp/src/view/view.hpp +++ b/cpp/src/view/view.hpp @@ -15,7 +15,7 @@ class View { } virtual void show() = 0; - virtual void update_board(const Board&, int8_t, std::vector) = 0; - virtual void notify_checkmate() = 0; - virtual void notify_stalemate() = 0; + virtual void update_board(const Board&, int8_t, std::vector) = 0; + virtual void notify_checkmate(Colour) = 0; + virtual void notify_stalemate(Colour) = 0; };