mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:47:35 +00:00
Chess: Highlight last move
This commit is contained in:
parent
f2c1782d86
commit
7b71f4759f
2 changed files with 49 additions and 39 deletions
|
@ -53,26 +53,29 @@ void ChessWidget::paint_event(GUI::PaintEvent& event)
|
||||||
size_t tile_width = width() / 8;
|
size_t tile_width = width() / 8;
|
||||||
size_t tile_height = height() / 8;
|
size_t tile_height = height() / 8;
|
||||||
|
|
||||||
for (unsigned rank = 0; rank < 8; ++rank) {
|
Chess::Square::for_each([&](Chess::Square sq) {
|
||||||
for (unsigned file = 0; file < 8; ++file) {
|
Gfx::IntRect tile_rect;
|
||||||
Gfx::IntRect tile_rect;
|
if (side() == Chess::Colour::White) {
|
||||||
if (side() == Chess::Colour::White) {
|
tile_rect = { sq.file * tile_width, (7 - sq.rank) * tile_height, tile_width, tile_height };
|
||||||
tile_rect = { file * tile_width, (7 - rank) * tile_height, tile_width, tile_height };
|
} else {
|
||||||
} else {
|
tile_rect = { (7 - sq.file) * tile_width, sq.rank * tile_height, tile_width, tile_height };
|
||||||
tile_rect = { (7 - file) * tile_width, rank * tile_height, tile_width, tile_height };
|
}
|
||||||
}
|
|
||||||
|
|
||||||
painter.fill_rect(tile_rect, ((rank % 2) == (file % 2)) ? m_dark_square_color : m_light_square_color);
|
painter.fill_rect(tile_rect, ((sq.rank % 2) == (sq.file % 2)) ? m_dark_square_color : m_light_square_color);
|
||||||
|
|
||||||
Chess::Square square = { rank, file };
|
if (m_last_move.has_value() && (m_last_move.value().to == sq || m_last_move.value().from == sq)) {
|
||||||
if (!(m_dragging_piece && square == m_moving_square)) {
|
painter.fill_rect(tile_rect, m_move_highlight_color);
|
||||||
auto bmp = m_pieces.get(board().get_piece(square));
|
}
|
||||||
if (bmp.has_value()) {
|
|
||||||
painter.draw_scaled_bitmap(tile_rect, *bmp.value(), bmp.value()->rect());
|
if (!(m_dragging_piece && sq == m_moving_square)) {
|
||||||
}
|
auto bmp = m_pieces.get(board().get_piece(sq));
|
||||||
|
if (bmp.has_value()) {
|
||||||
|
painter.draw_scaled_bitmap(tile_rect, *bmp.value(), bmp.value()->rect());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
});
|
||||||
|
|
||||||
if (m_dragging_piece) {
|
if (m_dragging_piece) {
|
||||||
auto bmp = m_pieces.get(board().get_piece(m_moving_square));
|
auto bmp = m_pieces.get(board().get_piece(m_moving_square));
|
||||||
|
@ -111,32 +114,36 @@ void ChessWidget::mouseup_event(GUI::MouseEvent& event)
|
||||||
|
|
||||||
auto target_square = mouse_to_square(event);
|
auto target_square = mouse_to_square(event);
|
||||||
|
|
||||||
if (board().apply_move({ m_moving_square, target_square }) && board().game_result() != Chess::Result::NotFinished) {
|
if (board().apply_move({ m_moving_square, target_square })) {
|
||||||
set_drag_enabled(false);
|
m_last_move = Chess::Move(m_moving_square, target_square);
|
||||||
update();
|
|
||||||
|
|
||||||
String msg;
|
if (board().game_result() != Chess::Result::NotFinished) {
|
||||||
switch (board().game_result()) {
|
set_drag_enabled(false);
|
||||||
case Chess::Result::CheckMate:
|
update();
|
||||||
if (board().turn() == Chess::Colour::White) {
|
|
||||||
msg = "Black wins by Checkmate.";
|
String msg;
|
||||||
} else {
|
switch (board().game_result()) {
|
||||||
msg = "White wins by Checkmate.";
|
case Chess::Result::CheckMate:
|
||||||
|
if (board().turn() == Chess::Colour::White) {
|
||||||
|
msg = "Black wins by Checkmate.";
|
||||||
|
} else {
|
||||||
|
msg = "White wins by Checkmate.";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Chess::Result::StaleMate:
|
||||||
|
msg = "Draw by Stalemate.";
|
||||||
|
break;
|
||||||
|
case Chess::Result::FiftyMoveRule:
|
||||||
|
msg = "Draw by 50 move rule.";
|
||||||
|
break;
|
||||||
|
case Chess::Result::ThreeFoldRepitition:
|
||||||
|
msg = "Draw by threefold repitition.";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
break;
|
GUI::MessageBox::show(window(), msg, "Game Over", GUI::MessageBox::Type::Information);
|
||||||
case Chess::Result::StaleMate:
|
|
||||||
msg = "Draw by Stalemate.";
|
|
||||||
break;
|
|
||||||
case Chess::Result::FiftyMoveRule:
|
|
||||||
msg = "Draw by 50 move rule.";
|
|
||||||
break;
|
|
||||||
case Chess::Result::ThreeFoldRepitition:
|
|
||||||
msg = "Draw by threefold repitition.";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ASSERT_NOT_REACHED();
|
|
||||||
}
|
}
|
||||||
GUI::MessageBox::show(window(), msg, "Game Over");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update();
|
update();
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include "Chess.h"
|
#include "Chess.h"
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
#include <AK/NonnullRefPtr.h>
|
#include <AK/NonnullRefPtr.h>
|
||||||
|
#include <AK/Optional.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <LibGUI/Widget.h>
|
#include <LibGUI/Widget.h>
|
||||||
#include <LibGfx/Bitmap.h>
|
#include <LibGfx/Bitmap.h>
|
||||||
|
@ -63,6 +64,7 @@ private:
|
||||||
Chess m_board;
|
Chess m_board;
|
||||||
Color m_dark_square_color { Color::from_rgb(0xb58863) };
|
Color m_dark_square_color { Color::from_rgb(0xb58863) };
|
||||||
Color m_light_square_color { Color::from_rgb(0xf0d9b5) };
|
Color m_light_square_color { Color::from_rgb(0xf0d9b5) };
|
||||||
|
Color m_move_highlight_color { Color::from_rgba(0x66ccee00) };
|
||||||
Chess::Colour m_side { Chess::Colour::White };
|
Chess::Colour m_side { Chess::Colour::White };
|
||||||
HashMap<Chess::Piece, RefPtr<Gfx::Bitmap>> m_pieces;
|
HashMap<Chess::Piece, RefPtr<Gfx::Bitmap>> m_pieces;
|
||||||
String m_piece_set;
|
String m_piece_set;
|
||||||
|
@ -70,4 +72,5 @@ private:
|
||||||
Gfx::IntPoint m_drag_point;
|
Gfx::IntPoint m_drag_point;
|
||||||
bool m_dragging_piece { false };
|
bool m_dragging_piece { false };
|
||||||
bool m_drag_enabled { true };
|
bool m_drag_enabled { true };
|
||||||
|
Optional<Chess::Move> m_last_move;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue