summaryrefslogtreecommitdiff
path: root/Games/Chess
diff options
context:
space:
mode:
authorPeter Elliott <pelliott@ualberta.ca>2020-08-11 15:03:55 -0600
committerAndreas Kling <kling@serenityos.org>2020-08-15 20:54:02 +0200
commit7b71f4759f94df15af9cd8cd1fc65f94076352c9 (patch)
tree99f7aeac0f909e6999cd223e5ca8a1cfbe2c157f /Games/Chess
parentf2c1782d86b831b03624ff7158b91a97b6a6fc4a (diff)
downloadserenity-7b71f4759f94df15af9cd8cd1fc65f94076352c9.zip
Chess: Highlight last move
Diffstat (limited to 'Games/Chess')
-rw-r--r--Games/Chess/ChessWidget.cpp85
-rw-r--r--Games/Chess/ChessWidget.h3
2 files changed, 49 insertions, 39 deletions
diff --git a/Games/Chess/ChessWidget.cpp b/Games/Chess/ChessWidget.cpp
index f1352851d3..3002b26434 100644
--- a/Games/Chess/ChessWidget.cpp
+++ b/Games/Chess/ChessWidget.cpp
@@ -53,26 +53,29 @@ void ChessWidget::paint_event(GUI::PaintEvent& event)
size_t tile_width = width() / 8;
size_t tile_height = height() / 8;
- for (unsigned rank = 0; rank < 8; ++rank) {
- for (unsigned file = 0; file < 8; ++file) {
- Gfx::IntRect tile_rect;
- if (side() == Chess::Colour::White) {
- tile_rect = { file * tile_width, (7 - rank) * tile_height, tile_width, tile_height };
- } else {
- tile_rect = { (7 - file) * tile_width, rank * tile_height, tile_width, tile_height };
- }
+ Chess::Square::for_each([&](Chess::Square sq) {
+ Gfx::IntRect tile_rect;
+ if (side() == Chess::Colour::White) {
+ tile_rect = { sq.file * tile_width, (7 - sq.rank) * tile_height, tile_width, tile_height };
+ } else {
+ tile_rect = { (7 - sq.file) * tile_width, sq.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_dragging_piece && square == m_moving_square)) {
- 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_last_move.has_value() && (m_last_move.value().to == sq || m_last_move.value().from == sq)) {
+ painter.fill_rect(tile_rect, m_move_highlight_color);
+ }
+
+ 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) {
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);
- if (board().apply_move({ m_moving_square, target_square }) && board().game_result() != Chess::Result::NotFinished) {
- set_drag_enabled(false);
- update();
+ if (board().apply_move({ m_moving_square, target_square })) {
+ m_last_move = Chess::Move(m_moving_square, target_square);
- String msg;
- switch (board().game_result()) {
- case Chess::Result::CheckMate:
- if (board().turn() == Chess::Colour::White) {
- msg = "Black wins by Checkmate.";
- } else {
- msg = "White wins by Checkmate.";
+ if (board().game_result() != Chess::Result::NotFinished) {
+ set_drag_enabled(false);
+ update();
+
+ String msg;
+ switch (board().game_result()) {
+ 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;
- 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", GUI::MessageBox::Type::Information);
}
- GUI::MessageBox::show(window(), msg, "Game Over");
}
update();
diff --git a/Games/Chess/ChessWidget.h b/Games/Chess/ChessWidget.h
index f5ad69b017..e4e77c0991 100644
--- a/Games/Chess/ChessWidget.h
+++ b/Games/Chess/ChessWidget.h
@@ -29,6 +29,7 @@
#include "Chess.h"
#include <AK/HashMap.h>
#include <AK/NonnullRefPtr.h>
+#include <AK/Optional.h>
#include <AK/StringView.h>
#include <LibGUI/Widget.h>
#include <LibGfx/Bitmap.h>
@@ -63,6 +64,7 @@ private:
Chess m_board;
Color m_dark_square_color { Color::from_rgb(0xb58863) };
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 };
HashMap<Chess::Piece, RefPtr<Gfx::Bitmap>> m_pieces;
String m_piece_set;
@@ -70,4 +72,5 @@ private:
Gfx::IntPoint m_drag_point;
bool m_dragging_piece { false };
bool m_drag_enabled { true };
+ Optional<Chess::Move> m_last_move;
};