Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
refactor: extract functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenMian committed Jul 24, 2023
1 parent 892b0d6 commit 8d8e831
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/gomoku.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class Gomoku
window_.clear(sf::Color(242, 208, 75));
board_.draw(window_);
window_.display();

sf::Clock clock;
while(clock.getElapsedTime() < sf::seconds(5.f))
handle_window_event();
Expand Down Expand Up @@ -275,23 +275,29 @@ class Gomoku
reset();
}

uint8_t get_actions() const
uint8_t get_actions() const { return get_mouse_actions() | get_keyboard_actions() | get_controller_actions(); }

uint8_t get_mouse_actions() const
{
uint8_t actions = 0;

// handle mouse input
if(board_.window_to_board_position(sf::Mouse::getPosition(window_)).has_value() &&
sf::Mouse::isButtonPressed(sf::Mouse::Left))
actions |= Action::PlaceChess;
return actions;
}

// handle keyboard input
uint8_t get_keyboard_actions() const
{
uint8_t actions = 0;
for(const auto& [key, action] : keyboard_actions)
{
if(sf::Keyboard::isKeyPressed(key))
actions |= action;
}
return actions;
}

// handle controller input
uint8_t get_controller_actions() const
{
uint8_t actions = 0;
for(int id = 0; id < 8; id++)
{
if(!sf::Joystick::isConnected(id))
Expand Down Expand Up @@ -320,7 +326,6 @@ class Gomoku
actions |= action;
}
}

return actions;
}

Expand Down

0 comments on commit 8d8e831

Please sign in to comment.