mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-12 10:40:39 +09:00
Chess: Prevent board changes when waiting for ChessEngine to move
This commit is contained in:
parent
07badd9530
commit
172707a945
Notes:
sideshowbarker
2024-07-19 00:46:29 +09:00
Author: https://github.com/bcoles
Commit: 172707a945
Pull-request: https://github.com/SerenityOS/serenity/pull/4440
Issue: https://github.com/SerenityOS/serenity/issues/3875
3 changed files with 37 additions and 10 deletions
|
@ -273,7 +273,7 @@ void ChessWidget::mouseup_event(GUI::MouseEvent& event)
|
||||||
GUI::MessageBox::show(window(), msg, "Game Over", GUI::MessageBox::Type::Information);
|
GUI::MessageBox::show(window(), msg, "Game Over", GUI::MessageBox::Type::Information);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
maybe_input_engine_move();
|
input_engine_move();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,7 +372,7 @@ void ChessWidget::reset()
|
||||||
m_board = Chess::Board();
|
m_board = Chess::Board();
|
||||||
m_side = (arc4random() % 2) ? Chess::Colour::White : Chess::Colour::Black;
|
m_side = (arc4random() % 2) ? Chess::Colour::White : Chess::Colour::Black;
|
||||||
m_drag_enabled = true;
|
m_drag_enabled = true;
|
||||||
maybe_input_engine_move();
|
input_engine_move();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,9 +391,18 @@ void ChessWidget::set_board_theme(const StringView& name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChessWidget::maybe_input_engine_move()
|
bool ChessWidget::want_engine_move()
|
||||||
{
|
{
|
||||||
if (!m_engine || board().turn() == side())
|
if (!m_engine)
|
||||||
|
return false;
|
||||||
|
if (board().turn() == side())
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChessWidget::input_engine_move()
|
||||||
|
{
|
||||||
|
if (!want_engine_move())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
bool drag_was_enabled = drag_enabled();
|
bool drag_was_enabled = drag_enabled();
|
||||||
|
@ -401,6 +410,8 @@ void ChessWidget::maybe_input_engine_move()
|
||||||
set_drag_enabled(false);
|
set_drag_enabled(false);
|
||||||
|
|
||||||
m_engine->get_best_move(board(), 4000, [this, drag_was_enabled](Chess::Move move) {
|
m_engine->get_best_move(board(), 4000, [this, drag_was_enabled](Chess::Move move) {
|
||||||
|
if (!want_engine_move())
|
||||||
|
return;
|
||||||
set_drag_enabled(drag_was_enabled);
|
set_drag_enabled(drag_was_enabled);
|
||||||
ASSERT(board().apply_move(move));
|
ASSERT(board().apply_move(move));
|
||||||
m_playback_move_number = m_board.moves().size();
|
m_playback_move_number = m_board.moves().size();
|
||||||
|
@ -614,21 +625,32 @@ bool ChessWidget::export_pgn(const StringView& export_path) const
|
||||||
|
|
||||||
void ChessWidget::flip_board()
|
void ChessWidget::flip_board()
|
||||||
{
|
{
|
||||||
|
if (want_engine_move()) {
|
||||||
|
GUI::MessageBox::show(window(), "You can only flip the board on your turn.", "Flip Board", GUI::MessageBox::Type::Information);
|
||||||
|
return;
|
||||||
|
}
|
||||||
m_side = Chess::opposing_colour(m_side);
|
m_side = Chess::opposing_colour(m_side);
|
||||||
|
input_engine_move();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChessWidget::resign()
|
int ChessWidget::resign()
|
||||||
{
|
{
|
||||||
if (m_engine && m_board.turn() != m_side) {
|
if (want_engine_move()) {
|
||||||
GUI::MessageBox::show(window(), "You can only resign on your turn.", "Resign", GUI::MessageBox::Type::Information);
|
GUI::MessageBox::show(window(), "You can only resign on your turn.", "Resign", GUI::MessageBox::Type::Information);
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto result = GUI::MessageBox::show(window(), "Are you sure you wish to resign?", "Resign", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNo);
|
||||||
|
if (result != GUI::MessageBox::ExecYes)
|
||||||
|
return -1;
|
||||||
|
|
||||||
board().set_resigned(m_board.turn());
|
board().set_resigned(m_board.turn());
|
||||||
|
|
||||||
set_drag_enabled(false);
|
set_drag_enabled(false);
|
||||||
update();
|
update();
|
||||||
const String msg = Chess::Board::result_to_string(m_board.game_result(), m_board.turn());
|
const String msg = Chess::Board::result_to_string(m_board.game_result(), m_board.turn());
|
||||||
GUI::MessageBox::show(window(), msg, "Game Over", GUI::MessageBox::Type::Information);
|
GUI::MessageBox::show(window(), msg, "Game Over", GUI::MessageBox::Type::Information);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ public:
|
||||||
bool import_pgn(const StringView& import_path);
|
bool import_pgn(const StringView& import_path);
|
||||||
bool export_pgn(const StringView& export_path) const;
|
bool export_pgn(const StringView& export_path) const;
|
||||||
|
|
||||||
void resign();
|
int resign();
|
||||||
void flip_board();
|
void flip_board();
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
|
@ -95,7 +95,8 @@ public:
|
||||||
|
|
||||||
void set_engine(RefPtr<Engine> engine) { m_engine = engine; }
|
void set_engine(RefPtr<Engine> engine) { m_engine = engine; }
|
||||||
|
|
||||||
void maybe_input_engine_move();
|
void input_engine_move();
|
||||||
|
bool want_engine_move();
|
||||||
|
|
||||||
void set_coordinates(bool coordinates) { m_coordinates = coordinates; }
|
void set_coordinates(bool coordinates) { m_coordinates = coordinates; }
|
||||||
bool coordinates() const { return m_coordinates; }
|
bool coordinates() const { return m_coordinates; }
|
||||||
|
|
|
@ -139,6 +139,10 @@ int main(int argc, char** argv)
|
||||||
app_menu.add_separator();
|
app_menu.add_separator();
|
||||||
|
|
||||||
app_menu.add_action(GUI::Action::create("New game", { Mod_None, Key_F2 }, [&](auto&) {
|
app_menu.add_action(GUI::Action::create("New game", { Mod_None, Key_F2 }, [&](auto&) {
|
||||||
|
if (widget.board().game_result() == Chess::Board::Result::NotFinished) {
|
||||||
|
if (widget.resign() < 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
widget.reset();
|
widget.reset();
|
||||||
}));
|
}));
|
||||||
app_menu.add_separator();
|
app_menu.add_separator();
|
||||||
|
@ -206,7 +210,7 @@ int main(int argc, char** argv)
|
||||||
widget.set_engine(nullptr);
|
widget.set_engine(nullptr);
|
||||||
} else {
|
} else {
|
||||||
widget.set_engine(Engine::construct(action.text()));
|
widget.set_engine(Engine::construct(action.text()));
|
||||||
widget.maybe_input_engine_move();
|
widget.input_engine_move();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
engines_action_group.add_action(*action);
|
engines_action_group.add_action(*action);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue