implemented full manual controller
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user