Skip to content

Commit

Permalink
Merge pull request #21 from Duzzuti/20-add-human-player
Browse files Browse the repository at this point in the history
Add human player
  • Loading branch information
Duzzuti authored Dec 30, 2023
2 parents b8168c1 + 06ebca3 commit 7689fa1
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(TEST_DIR ${PROJECT_SOURCE_DIR}/tests)
set(PLAYER_DIR ${SRC_DIR}/players)
set(CHECK_PLAYER ${PLAYER_DIR}/check_player/check_player.cpp)
set(RAND_PLAYER ${PLAYER_DIR}/rand_player/rand_player.cpp)
set(HUMAN_PLAYER ${PLAYER_DIR}/human_player/human_player.cpp)

include(FetchContent)

Expand Down
3 changes: 2 additions & 1 deletion docs/players.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Here is a list of all currently available player types:
|Name|Strength|Description|
|----|--------|-----------|
|RandomPlayer|?|Randomly chooses a legal action|
|CheckPlayer|?|Always checks/calls|
|CheckPlayer|?|Always checks/calls|
|HumanPlayer|?|Asks the user which action to perform|
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ class Config {

class BaseConfig : public Config {
public:
BaseConfig() : Config(10, 4, 1000, 10, 1) {} // Default values
BaseConfig() : Config(10, 5, 1000, 10, 1) {} // Default values
};
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Add the executable target
add_executable(PokerWorkshop main.cpp deck.cpp game.cpp player.cpp hand_strengths.cpp ${CHECK_PLAYER} ${RAND_PLAYER})
add_executable(PokerWorkshop main.cpp deck.cpp game.cpp player.cpp hand_strengths.cpp ${CHECK_PLAYER} ${RAND_PLAYER} ${HUMAN_PLAYER})
# Include headers
target_include_directories(PokerWorkshop PUBLIC ${INCLUDE_DIR} ${PLAYER_DIR})

Expand Down
2 changes: 2 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "check_player/check_player.h"
#include "deck.h"
#include "hand_strengths.h"
#include "human_player/human_player.h"
#include "logger.h"
#include "rand_player/rand_player.h"

Expand All @@ -15,6 +16,7 @@ void Game::run() {
this->players[1] = std::move(std::make_unique<RandPlayer>(2));
this->players[2] = std::move(std::make_unique<CheckPlayer>(3));
this->players[3] = std::move(std::make_unique<RandPlayer>(4));
this->players[4] = std::move(std::make_unique<HumanPlayer>(5));

this->data.numPlayers = this->m_config.numPlayers;

Expand Down
42 changes: 42 additions & 0 deletions src/players/human_player/human_player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "human_player.h"

Action HumanPlayer::turn(const Data& data, const bool onlyRaise) const noexcept {
std::cout << "Your hand: " << this->getHand().first.toString() << " " << this->getHand().second.toString();
std::cout << " | Community cards: ";
for (u_int8_t i = 0; i < 5; i++) {
if (data.roundData.betRoundState == BetRoundState::PREFLOP)
break;
else if (data.roundData.betRoundState == BetRoundState::FLOP && i == 3)
break;
else if (data.roundData.betRoundState == BetRoundState::TURN && i == 4)
break;

std::cout << data.roundData.communityCards[i].toString() << " ";
}
std::cout << "| Pot: " << data.roundData.pot << " | Current bet: " << data.betRoundData.currentBet;
std::cout << " | Your chips: " << data.getChips() << std::endl;
while (true) {
if (onlyRaise)
std::cout << "Please enter an action ('c' for call, 'r <bet>' for raise): ";
else
std::cout << "Please enter an action ('f' for fold, 'c' for call, 'chk' for check, 'r <bet>' for raise, 'b <bet>' for bet): ";
std::string input;
std::getline(std::cin, input);

if (input == "f" && !onlyRaise)
return {Actions::FOLD};
else if (input == "c")
return {Actions::CALL};
else if (input == "chk" && !onlyRaise)
return {Actions::CHECK};
else if (input[0] == 'r') {
u_int64_t bet = std::stoull(input.substr(2));
return {Actions::RAISE, bet};
} else if (input[0] == 'b' && !onlyRaise) {
u_int64_t bet = std::stoull(input.substr(2));
return {Actions::BET, bet};
} else {
std::cout << "Invalid input! Try again" << std::endl;
}
}
}
10 changes: 10 additions & 0 deletions src/players/human_player/human_player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include "player.h"

class HumanPlayer : public Player {
public:
HumanPlayer(const std::string& name) noexcept : Player(name){};
HumanPlayer(u_int8_t playerNum = 0) noexcept : Player(!playerNum ? "HumanPlayer" : "HumanPlayer" + std::to_string(playerNum)){};

Action turn(const Data& data, const bool onlyRaise = false) const noexcept override;
};
2 changes: 2 additions & 0 deletions src/players/human_player/human_player.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This player is a human player. It is used to play the game manually.
You will be asked which action you want to perform at each turn.
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ add_executable(poker_test_deck main_test.cpp deck_unittest.cpp ${COMMON_SRC})
target_link_libraries(poker_test_deck gtest_main)
target_include_directories(poker_test_deck PUBLIC ${INCLUDE_DIR})

add_executable(poker_test_utils main_test.cpp utils_unittest.cpp ${SRC_DIR}/game.cpp ${COMMON_SRC} ${CHECK_PLAYER} ${RAND_PLAYER})
add_executable(poker_test_utils main_test.cpp utils_unittest.cpp ${SRC_DIR}/game.cpp ${COMMON_SRC} ${CHECK_PLAYER} ${RAND_PLAYER} ${HUMAN_PLAYER})
target_link_libraries(poker_test_utils gtest_main)
target_include_directories(poker_test_utils PUBLIC ${INCLUDE_DIR} ${PLAYER_DIR} ${TEST_DIR})

Expand Down

0 comments on commit 7689fa1

Please sign in to comment.