diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-05-23 15:48:42 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-23 18:15:36 +0200 |
commit | 6a194869cf449bd62f661fb425ae3b7fc59df0de (patch) | |
tree | 006f172c276b2197ef4cb75264c2f89363f6425b /Userland | |
parent | 2dfced2501d5635c3da3904409190e90364982d5 (diff) | |
download | serenity-6a194869cf449bd62f661fb425ae3b7fc59df0de.zip |
Hearts: Add key combinations to letting the AI play for you
A single card can be played with F10 while Shift-F10 toggles the AI
for the current as well as all future tricks.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Games/Hearts/Game.cpp | 13 | ||||
-rw-r--r-- | Userland/Games/Hearts/Game.h | 1 | ||||
-rw-r--r-- | Userland/Games/Hearts/Player.h | 1 |
3 files changed, 11 insertions, 4 deletions
diff --git a/Userland/Games/Hearts/Game.cpp b/Userland/Games/Hearts/Game.cpp index 2b661630dd..ca6f8ba51d 100644 --- a/Userland/Games/Hearts/Game.cpp +++ b/Userland/Games/Hearts/Game.cpp @@ -41,6 +41,7 @@ Game::Game() }; m_players[0].name_alignment = Gfx::TextAlignment::BottomRight; m_players[0].name = "Gunnar"; + m_players[0].is_human = true; m_players[0].taken_cards_target = { width / 2 - Card::width / 2, height }; m_players[1].first_card_position = { outer_border_size, (height - player_deck_height) / 2 }; @@ -252,7 +253,7 @@ void Game::let_player_play_card() else on_status_change(String::formatted("Waiting for {} to play a card...", player)); - if (is_human(player)) { + if (player.is_human) { m_human_can_play = true; update(); return; @@ -351,13 +352,19 @@ void Game::advance_game() void Game::keydown_event(GUI::KeyEvent& event) { - if (event.shift() && event.key() == KeyCode::Key_F11) + if (event.shift() && event.key() == KeyCode::Key_F10) { + m_players[0].is_human = !m_players[0].is_human; + advance_game(); + } else if (event.key() == KeyCode::Key_F10) { + if (m_human_can_play) + play_card(m_players[0], pick_card(m_players[0])); + } else if (event.shift() && event.key() == KeyCode::Key_F11) dump_state(); } void Game::play_card(Player& player, size_t card_index) { - if (is_human(player)) + if (player.is_human) m_human_can_play = false; VERIFY(player.hand[card_index]); VERIFY(m_trick.size() < 4); diff --git a/Userland/Games/Hearts/Game.h b/Userland/Games/Hearts/Game.h index 79c323d71a..bda75ec04f 100644 --- a/Userland/Games/Hearts/Game.h +++ b/Userland/Games/Hearts/Game.h @@ -40,7 +40,6 @@ private: void continue_game_after_delay(int interval_ms = 750); void advance_game(); size_t pick_card(Player& player); - bool is_human(Player& player) const { return &player == &m_players[0]; } size_t player_index(Player& player); Player& current_player(); bool game_ended() const { return m_trick_number == 13; } diff --git a/Userland/Games/Hearts/Player.h b/Userland/Games/Hearts/Player.h index 6f143f34a9..3688bcb626 100644 --- a/Userland/Games/Hearts/Player.h +++ b/Userland/Games/Hearts/Player.h @@ -38,6 +38,7 @@ public: Gfx::TextAlignment name_alignment; Gfx::IntPoint taken_cards_target; String name; + bool is_human { false }; }; } |