implemented full manual controller

This commit is contained in:
Karma Riuk 2025-02-06 19:42:41 +01:00
parent e376d67c83
commit 96f0ec5399
5 changed files with 70 additions and 22 deletions

View File

@ -1,17 +1,61 @@
#include "manual.hpp"
#include "../model/utils/utils.hpp"
#include <algorithm>
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();
}

View File

@ -8,8 +8,9 @@
class ManualController : public Controller {
private:
View& view;
int8_t selected_index;
Piece selected_piece;
std::vector<Move> legal_moves;
std::vector<int8_t> targets;
void reset_selection();
void show_legal_moves(Coords);

View File

@ -13,17 +13,21 @@ GUI::GUI() {
}
void GUI::update_board(
const Board& b, int8_t selected_square, std::vector<Move> legal_moves
const Board& b, int8_t selected_square, std::vector<int8_t> 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<Move> moves) {
void GUI::draw_board(int selected_square, std::vector<int8_t> 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<Move> moves) {
window.draw(square);
draw_annotation(file, rank);
std::vector<int8_t> targets = to_target_square(moves);
if (std::find(targets.begin(), targets.end(), index)
!= targets.end()) {
float r = .15 * TILE_SIZE;

View File

@ -15,9 +15,9 @@ class GUI : public View {
GUI();
void show() override;
void update_board(const Board&, int8_t, std::vector<Move>) override;
void notify_checkmate() override;
void notify_stalemate() override;
void update_board(const Board&, int8_t, std::vector<int8_t>) 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<Move>);
void draw_board(int, std::vector<int8_t>);
void draw_pieces(const Board&);
void draw_annotation(int, int);
};

View File

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