diff --git a/.github/workflows/building.yml b/.github/workflows/building.yml index 2bf818b..fa4462f 100644 --- a/.github/workflows/building.yml +++ b/.github/workflows/building.yml @@ -22,9 +22,9 @@ jobs: os: ubuntu-latest compiler: clang++ - - name: MacOS GCC - os: macos-latest - compiler: g++ + #- name: MacOS GCC + # os: macos-latest + # compiler: g++ #- name: MacOS Clang # os: macos-latest diff --git a/CMakeLists.txt b/CMakeLists.txt index 97d7afd..c6aac79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,14 +39,6 @@ FetchContent_MakeAvailable(googletest) # --- caravan libraries include_directories(include) -add_library(controller - "include/caravan/controller/controller.h" - "include/caravan/controller/controller_tui.h" - - "src/caravan/controller/controller.cpp" - "src/caravan/controller/controller_tui.cpp" -) - add_library(core "include/caravan/core/common.h" "include/caravan/core/exceptions.h" @@ -59,20 +51,18 @@ add_library(model "include/caravan/model/caravan.h" "include/caravan/model/deck.h" "include/caravan/model/game.h" - "include/caravan/model/model.h" - "include/caravan/model/model_tui.h" "include/caravan/model/player.h" "include/caravan/model/table.h" "src/caravan/model/caravan.cpp" "src/caravan/model/deck.cpp" "src/caravan/model/game.cpp" - "src/caravan/model/model.cpp" - "src/caravan/model/model_tui.cpp" "src/caravan/model/player.cpp" "src/caravan/model/table.cpp" ) +target_link_libraries(model core) + add_library(user "include/caravan/user/bot_easy.h" "include/caravan/user/user.h" @@ -85,7 +75,6 @@ add_library(view "include/caravan/view/view.h" "include/caravan/view/view_tui.h" - "src/caravan/view/view.cpp" "src/caravan/view/view_tui.cpp" ) @@ -102,7 +91,6 @@ add_executable(caravan ) target_link_libraries(caravan - PRIVATE controller PRIVATE core PRIVATE model PRIVATE user diff --git a/include/caravan/controller/controller.h b/include/caravan/controller/controller.h deleted file mode 100644 index 2f95c37..0000000 --- a/include/caravan/controller/controller.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2022-2024 r3w0p -// The following code can be redistributed and/or -// modified under the terms of the GPL-3.0 License. - -#ifndef CARAVAN_CONTROLLER_H -#define CARAVAN_CONTROLLER_H - -#include -#include "caravan/core/common.h" -#include "caravan/model/game.h" -#include "caravan/user/user.h" -#include "caravan/view/view.h" - -class ControllerSubscriber { -public: - virtual void on_controller_command(GameCommand command) = 0; // TODO -}; - -class Controller : public ViewSubscriber, public Publisher { -protected: - bool closed; -public: - virtual ~Controller() = default; - explicit Controller() : closed(false) {}; - - void subscribe(ControllerSubscriber *sub) override; - virtual void close() = 0; -}; - -#endif //CARAVAN_CONTROLLER_H diff --git a/include/caravan/controller/controller_tui.h b/include/caravan/controller/controller_tui.h deleted file mode 100644 index 7673ada..0000000 --- a/include/caravan/controller/controller_tui.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) 2022-2024 r3w0p -// The following code can be redistributed and/or -// modified under the terms of the GPL-3.0 License. - -#ifndef CARAVAN_CONTROLLER_TUI_H -#define CARAVAN_CONTROLLER_TUI_H - -#include "caravan/controller/controller.h" - -class ControllerTUI : public Controller { -public: - explicit ControllerTUI() = default; - - void on_view_user_input(std::string input, bool confirmed) override; // from ViewSubscriber - void close() override; -}; - -#endif //CARAVAN_CONTROLLER_TUI_H diff --git a/include/caravan/core/common.h b/include/caravan/core/common.h index 8a2f0c8..7368bf2 100644 --- a/include/caravan/core/common.h +++ b/include/caravan/core/common.h @@ -36,7 +36,7 @@ const uint8_t PLAYER_CARAVANS_MAX = 3; */ enum PlayerName { - NO_PLAYER, PLAYER_BOTTOM, PLAYER_TOP + NO_PLAYER, PLAYER_ABC, PLAYER_DEF }; enum Direction { ANY, ASCENDING, DESCENDING @@ -101,16 +101,8 @@ typedef struct GameConfig { PlayerName pn_first {NO_PLAYER}; } GameConfig; -typedef struct GameOption { // TODO remove - OptionType type {}; - uint8_t pos_hand {}; - CaravanName caravan_name {}; - uint8_t pos_caravan {}; - Card card {}; -} GameOption; - typedef struct GameCommand { - OptionType type {NO_OPTION}; + OptionType option {NO_OPTION}; uint8_t pos_hand {0}; CaravanName caravan_name {NO_CARAVAN}; uint8_t pos_caravan {0}; @@ -121,19 +113,6 @@ typedef struct GameCommand { */ bool is_numeral_card(Card c); - bool is_face_card(Card c); -/* - * CLASSES - */ - -template -class Publisher { -protected: - std::vector subscribers; -public: - virtual void subscribe(T *sub) = 0; -}; - #endif //CARAVAN_CORE_COMMON_H diff --git a/include/caravan/model/game.h b/include/caravan/model/game.h index d441272..c1c7e6b 100644 --- a/include/caravan/model/game.h +++ b/include/caravan/model/game.h @@ -20,12 +20,12 @@ class Game { int8_t compare_bids(CaravanName cvname1, CaravanName cvname2); bool has_sold(CaravanName cvname); - void option_clear(Player *pptr, GameOption* go); - void option_discard(Player *pptr, GameOption* go); - void option_play(Player *pptr, GameOption* go); + void option_clear(Player *pptr, GameCommand* command); + void option_discard(Player *pptr, GameCommand* command); + void option_play(Player *pptr, GameCommand* command); public: - explicit Game(GameConfig gc); + explicit Game(GameConfig config); void close(); @@ -43,7 +43,7 @@ class Game { bool is_closed(); - void play_option(GameOption* go); + void play_option(GameCommand* command); }; #endif //CARAVAN_MODEL_GAME_H diff --git a/include/caravan/model/model.h b/include/caravan/model/model.h deleted file mode 100644 index 0d14ed1..0000000 --- a/include/caravan/model/model.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2022-2024 r3w0p -// The following code can be redistributed and/or -// modified under the terms of the GPL-3.0 License. - -#ifndef CARAVAN_MODEL_H -#define CARAVAN_MODEL_H - -#include -#include "caravan/core/common.h" - -class ModelSubscriber { -public: - virtual void on_model_change_me() = 0; -}; - -class Model : public Publisher { -protected: - bool closed; -public: - virtual ~Model() = default; - explicit Model() : closed(false) {}; - - virtual void run() = 0; - void subscribe(ModelSubscriber *sub) override; - void close() { closed = true; }; -}; - - - -#endif //CARAVAN_MODEL_H diff --git a/include/caravan/model/model_tui.h b/include/caravan/model/model_tui.h deleted file mode 100644 index e4937f2..0000000 --- a/include/caravan/model/model_tui.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) 2022-2024 r3w0p -// The following code can be redistributed and/or -// modified under the terms of the GPL-3.0 License. - -#ifndef CARAVAN_MODEL_TUI_H -#define CARAVAN_MODEL_TUI_H - -#include "caravan/model/model.h" -#include "caravan/core/common.h" - -class ModelTUI : public Model { -public: - explicit ModelTUI() : Model() {}; - - void run() override; -}; - -#endif //CARAVAN_MODEL_TUI_H diff --git a/include/caravan/user/bot_easy.h b/include/caravan/user/bot_easy.h index ca8265a..97b1e35 100644 --- a/include/caravan/user/bot_easy.h +++ b/include/caravan/user/bot_easy.h @@ -12,7 +12,7 @@ class UserBotEasy : public UserBot { public: explicit UserBotEasy(PlayerName pn) : UserBot(pn) {}; - GameOption generate_option(Game *g) override; + GameCommand generate_option(Game *g) override; }; #endif //CARAVAN_USER_BOT_EASY_H diff --git a/include/caravan/user/user.h b/include/caravan/user/user.h index e5701ec..5876d37 100644 --- a/include/caravan/user/user.h +++ b/include/caravan/user/user.h @@ -32,7 +32,7 @@ class UserBot : public User { explicit UserBot(PlayerName pn) : User(pn) {}; bool is_human() override; - virtual GameOption generate_option(Game *g) = 0; + virtual GameCommand generate_option(Game *g) = 0; }; #endif //CARAVAN_USER_H diff --git a/include/caravan/view/view.h b/include/caravan/view/view.h index e68ae86..e125b4b 100644 --- a/include/caravan/view/view.h +++ b/include/caravan/view/view.h @@ -10,26 +10,22 @@ #include "caravan/user/user.h" #include "caravan/core/common.h" -class ViewSubscriber { -public: - virtual void on_view_user_input(std::string input, bool confirmed) = 0; -}; - -class View : public Publisher { +class View { protected: - User *user_top_ptr; - User *user_bottom_ptr; + User *user_abc; + User *user_def; + Game *game; bool closed; public: virtual ~View() = default; - explicit View(User *utop, User *ubottom) : - user_top_ptr(utop), user_bottom_ptr(ubottom), closed(false) {}; + explicit View(User *user_abc, User *user_def, Game *game) : + user_abc(user_abc), + user_def(user_def), + game(game), + closed(false) {}; virtual void run() = 0; - void subscribe(ViewSubscriber *sub) override; - void close() { closed = true; }; + virtual void close() = 0; }; - - #endif //CARAVAN_VIEW_H diff --git a/include/caravan/view/view_tui.h b/include/caravan/view/view_tui.h index 2bd6a3a..a19b7d1 100644 --- a/include/caravan/view/view_tui.h +++ b/include/caravan/view/view_tui.h @@ -9,10 +9,15 @@ #include "caravan/core/common.h" class ViewTUI : public View { +protected: + GameCommand parse_user_input(std::string input, bool confirmed); + public: - explicit ViewTUI(User *utop, User *ubottom) : View(utop, ubottom) {}; + explicit ViewTUI(User *user_abc, User *user_def, Game *game) : + View(user_abc, user_def, game) {}; void run() override; + void close() override; }; #endif //CARAVAN_VIEW_TUI_H diff --git a/src/caravan/controller/controller.cpp b/src/caravan/controller/controller.cpp deleted file mode 100644 index 32758bc..0000000 --- a/src/caravan/controller/controller.cpp +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) 2022-2024 r3w0p -// The following code can be redistributed and/or -// modified under the terms of the GPL-3.0 License. - -#include "caravan/controller/controller.h" - -void Controller::subscribe(ControllerSubscriber *sub) { - subscribers.push_back(sub); -} diff --git a/src/caravan/controller/controller_tui.cpp b/src/caravan/controller/controller_tui.cpp deleted file mode 100644 index 0c55ca4..0000000 --- a/src/caravan/controller/controller_tui.cpp +++ /dev/null @@ -1,270 +0,0 @@ -// Copyright (c) 2022-2024 r3w0p -// The following code can be redistributed and/or -// modified under the terms of the GPL-3.0 License. - -#include "caravan/controller/controller_tui.h" -#include "caravan/core/exceptions.h" - - -void process_first(std::string input, GameCommand *command) { - char c = input.at(0); // minimum input size already checked elsewhere - - switch (c) { - case 'P': - case 'p': - command->type = OPTION_PLAY; - /* - * P2F - * "Play numeral card at hand pos 2 onto caravan F" - * - * P4F8 - * "Play face card at hand pos 4 onto caravan F, slot 8" - */ - break; - - case 'D': - case 'd': - command->type = OPTION_DISCARD; - /* - * D3 - * "Discard card at hand pos 3" - */ - break; - - case 'C': - case 'c': - /* - * CE - * "Clear caravan E" - */ - command->type = OPTION_CLEAR; - break; - - default: - throw CaravanInputException("Invalid option '" + std::string(1, c) + "', must be one of: (P)lay, (D)iscard, (C)lear."); - } -} - -void process_second(std::string input, GameCommand *command) { - if (command->type == OPTION_PLAY or command->type == OPTION_DISCARD) { - - if (input.size() < 2) - throw CaravanInputException("A hand position has not been entered."); - - char c = input.at(1); - - switch (c) { - case '1': - command->pos_hand = 1; - break; - case '2': - command->pos_hand = 2; - break; - case '3': - command->pos_hand = 3; - break; - case '4': - command->pos_hand = 4; - break; - case '5': - command->pos_hand = 5; - break; - case '6': - command->pos_hand = 6; - break; - case '7': - command->pos_hand = 7; - break; - case '8': - command->pos_hand = 8; - break; - default: - throw CaravanInputException("Invalid hand position '" + std::string(1, c) + "'."); - } - - } else if (command->type == OPTION_CLEAR) { - - if (input.size() < 2) - throw CaravanInputException("A caravan name has not been entered."); - - char c = input.at(1); - - switch (c) { - case 'A': - case 'a': - command->caravan_name = CARAVAN_A; - break; - case 'B': - case 'b': - command->caravan_name = CARAVAN_B; - break; - case 'C': - case 'c': - command->caravan_name = CARAVAN_C; - break; - case 'D': - case 'd': - command->caravan_name = CARAVAN_D; - break; - case 'E': - case 'e': - command->caravan_name = CARAVAN_E; - break; - case 'F': - case 'f': - command->caravan_name = CARAVAN_F; - break; - default: - throw CaravanInputException("Invalid caravan name '" + std::string(1, c) + "', must be between: A-F."); - } - - } // else invalid command type, handled during parse of first character -} - -void process_third(std::string input, GameCommand *command) { - if (command->type == OPTION_PLAY) { - - if (input.size() < 3) - throw CaravanInputException("A caravan name has not been entered."); - - char c = input.at(2); - - switch (c) { - case 'A': - case 'a': - command->caravan_name = CARAVAN_A; - break; - case 'B': - case 'b': - command->caravan_name = CARAVAN_B; - break; - case 'C': - case 'c': - command->caravan_name = CARAVAN_C; - break; - case 'D': - case 'd': - command->caravan_name = CARAVAN_D; - break; - case 'E': - case 'e': - command->caravan_name = CARAVAN_E; - break; - case 'F': - case 'f': - command->caravan_name = CARAVAN_F; - break; - default: - throw CaravanInputException("Invalid caravan name '" + std::string(1, c) + "', must be between: A-F."); - } - } -} - -void process_fourth(std::string input, GameCommand *command) { - if (command->type == OPTION_PLAY) { - - if (input.size() < 4) return; // optional, not an error - - char c = input.at(3); - - switch (c) { - case '1': - command->pos_caravan = 1; - break; - case '2': - command->pos_caravan = 2; - break; - case '3': - command->pos_caravan = 3; - break; - case '4': - command->pos_caravan = 4; - break; - case '5': - command->pos_caravan = 5; - break; - case '6': - command->pos_caravan = 6; - break; - case '7': - command->pos_caravan = 7; - break; - case '8': - command->pos_caravan = 8; - break; - default: - throw CaravanInputException("Invalid caravan position '" + std::string(1, c) + "'."); - } - } -} - -bool process_exit(std::string input, GameCommand *command) { - if( - input.size() == 4 and - (input.at(0) == 'E' or input.at(0) == 'e') and - (input.at(1) == 'X' or input.at(1) == 'x') and - (input.at(2) == 'I' or input.at(2) == 'i') and - (input.at(3) == 'T' or input.at(3) == 't') - ) { - command->type = OPTION_EXIT; - return true; - } - - return false; -} - -void ControllerTUI::on_view_user_input(std::string input, bool confirmed) { - if(closed) return; - - if(input.empty()) return; - - // TODO A future feature that highlights parts of the board as a command is typed, - // so that the user has a better idea of what their command will do. - if(!confirmed) return; - - GameCommand command; - bool is_exit; - - /* - * EXIT - */ - is_exit = process_exit(input, &command); - - if(!is_exit) { - /* - * FIRST - * - COMMAND TYPE - */ - process_first(input, &command); - - /* - * SECOND - * - HAND POSITION or - * - CARAVAN NAME - */ - process_second(input, &command); - - /* - * THIRD - * - CARAVAN NAME - */ - process_third(input, &command); - - /* - * FOURTH - * - CARAVAN POSITION (used when selecting Face card only) - */ - process_fourth(input, &command); - } - - // Pass to subscribers (i.e., Model) - for(ControllerSubscriber *s : subscribers) { - s->on_controller_command(command); - } -} - -void ControllerTUI::close() { - if(!closed) { - closed = true; - } -} \ No newline at end of file diff --git a/src/caravan/core/common.cpp b/src/caravan/core/common.cpp index 76f2dd4..7851861 100644 --- a/src/caravan/core/common.cpp +++ b/src/caravan/core/common.cpp @@ -3,6 +3,7 @@ // modified under the terms of the GPL-3.0 License. #include "caravan/core/common.h" +#include "caravan/core/exceptions.h" bool is_numeral_card(Card c) { return (c.rank >= ACE and c.rank <= TEN); diff --git a/src/caravan/main.cpp b/src/caravan/main.cpp index c821b28..98d1fac 100644 --- a/src/caravan/main.cpp +++ b/src/caravan/main.cpp @@ -4,40 +4,33 @@ #include "caravan/view/view_tui.h" #include "caravan/user/bot_easy.h" -#include "caravan/controller/controller_tui.h" #include int main() { - User *utop; - User *ubottom; + User *user_abc; + User *user_def; + Game *game; ViewTUI *v; - Controller *c; - // Model *m; - try { - utop = new UserHuman(PLAYER_TOP); - ubottom = new UserHuman(PLAYER_BOTTOM); - - v = new ViewTUI(utop, ubottom); - c = new ControllerTUI(); - // m = ... + GameConfig config = { // TODO + 30, 1, true, + 30, 1, true, + PLAYER_ABC + }; - // TODO Subscribe components - v->subscribe(c); + try { + user_abc = new UserHuman(PLAYER_ABC); + user_def = new UserHuman(PLAYER_DEF); + game = new Game(config); // TODO + v = new ViewTUI(user_abc, user_def, game); v->run(); } catch (CaravanFatalException &e) { - std::cout << e.what() << std::endl; + std::cout << e.what() << std::endl; // TODO } v->close(); - c->close(); - // m->close(); - delete v; - delete c; - delete utop; - delete ubottom; } diff --git a/src/caravan/model/caravan.cpp b/src/caravan/model/caravan.cpp index 42df9e6..4d8127e 100644 --- a/src/caravan/model/caravan.cpp +++ b/src/caravan/model/caravan.cpp @@ -3,6 +3,7 @@ // modified under the terms of the GPL-3.0 License. #include "caravan/model/caravan.h" +#include "caravan/core/common.h" #include "caravan/core/exceptions.h" const std::string EXC_CLOSED = "Caravan is closed."; diff --git a/src/caravan/model/game.cpp b/src/caravan/model/game.cpp index 96cb535..8e05cb3 100644 --- a/src/caravan/model/game.cpp +++ b/src/caravan/model/game.cpp @@ -3,34 +3,35 @@ // modified under the terms of the GPL-3.0 License. #include "caravan/model/game.h" +#include "caravan/core/common.h" const std::string EXC_CLOSED = "Game is closed."; /** - * @param gc Game configuration. + * @param config Game configuration. * * @throws CaravanFatalException Invalid player names. */ -Game::Game(GameConfig gc) { - if (gc.pn_first == NO_PLAYER) +Game::Game(GameConfig config) { + if (config.pn_first == NO_PLAYER) throw CaravanFatalException("Invalid player name for first player in game configuration."); Deck *deck_top = DeckBuilder::build_caravan_deck( - gc.pa_num_cards, - gc.pa_num_sample_decks, - gc.pa_balanced_sample); + config.pa_num_cards, + config.pa_num_sample_decks, + config.pa_balanced_sample); Deck *deck_bottom = DeckBuilder::build_caravan_deck( - gc.pb_num_cards, - gc.pb_num_sample_decks, - gc.pb_balanced_sample); + config.pb_num_cards, + config.pb_num_sample_decks, + config.pb_balanced_sample); table_ptr = new Table(); - pa_ptr = new Player(PLAYER_BOTTOM, deck_bottom); - pb_ptr = new Player(PLAYER_TOP, deck_top); + pa_ptr = new Player(PLAYER_ABC, deck_bottom); + pb_ptr = new Player(PLAYER_DEF, deck_top); closed = false; - p_turn = gc.pn_first == pa_ptr->get_name() ? pa_ptr : pb_ptr; + p_turn = config.pn_first == pa_ptr->get_name() ? pa_ptr : pb_ptr; } void Game::close() { @@ -92,11 +93,11 @@ PlayerName Game::get_winner() { // The first player with an empty hand loses - if (get_player(PLAYER_BOTTOM)->get_size_hand() == 0) - return PLAYER_TOP; + if (get_player(PLAYER_ABC)->get_size_hand() == 0) + return PLAYER_DEF; - if (get_player(PLAYER_TOP)->get_size_hand() == 0) - return PLAYER_BOTTOM; + if (get_player(PLAYER_DEF)->get_size_hand() == 0) + return PLAYER_ABC; // Check bid sizes @@ -138,16 +139,16 @@ bool Game::is_closed() { return closed; } -void Game::play_option(GameOption *go) { +void Game::play_option(GameCommand* command) { if (closed) throw CaravanFatalException(EXC_CLOSED); if (get_winner() != NO_PLAYER) throw CaravanFatalException( "The game has already been won."); - switch (go->type) { + switch (command->option) { case OPTION_PLAY: - option_play(p_turn, go); + option_play(p_turn, command); break; case OPTION_DISCARD: @@ -156,7 +157,7 @@ void Game::play_option(GameOption *go) { "A player cannot discard a card during " "the Start round."); - option_discard(p_turn, go); + option_discard(p_turn, command); break; case OPTION_CLEAR: @@ -165,7 +166,7 @@ void Game::play_option(GameOption *go) { "A player cannot clear a caravan during " "the Start round."); - option_clear(p_turn, go); + option_clear(p_turn, command); break; default: @@ -229,26 +230,25 @@ bool Game::has_sold(CaravanName cvname) { return bid >= CARAVAN_SOLD_MIN and bid <= CARAVAN_SOLD_MAX; } -void Game::option_clear(Player *pptr, GameOption *go) { +void Game::option_clear(Player *pptr, GameCommand* command) { PlayerCaravanNames pcns = get_player_caravan_names(pptr->get_name()); - if (pcns[0] != go->caravan_name and - pcns[1] != go->caravan_name and - pcns[2] != go->caravan_name) + if (pcns[0] != command->caravan_name and + pcns[1] != command->caravan_name and + pcns[2] != command->caravan_name) throw CaravanGameException( "A player cannot clear their opponent's caravans."); - table_ptr->clear_caravan(go->caravan_name); + table_ptr->clear_caravan(command->caravan_name); } -void Game::option_discard(Player *pptr, GameOption *go) { +void Game::option_discard(Player *pptr, GameCommand* command) { Card c_hand; - c_hand = pptr->discard_from_hand_at(go->pos_hand); - go->card = c_hand; + c_hand = pptr->discard_from_hand_at(command->pos_hand); } -void Game::option_play(Player *pptr, GameOption *go) { - Card c_hand = pptr->get_from_hand_at(go->pos_hand); +void Game::option_play(Player *pptr, GameCommand* command) { + Card c_hand = pptr->get_from_hand_at(command->pos_hand); bool in_start_stage = pptr->get_moves_count() < MOVES_START_ROUND; bool pa_playing_num_onto_pa_caravans; @@ -257,15 +257,15 @@ void Game::option_play(Player *pptr, GameOption *go) { if (is_numeral_card(c_hand)) { pa_playing_num_onto_pa_caravans = pptr->get_name() == pa_ptr->get_name() and - (go->caravan_name == CARAVAN_A or - go->caravan_name == CARAVAN_B or - go->caravan_name == CARAVAN_C); + (command->caravan_name == CARAVAN_A or + command->caravan_name == CARAVAN_B or + command->caravan_name == CARAVAN_C); pb_playing_num_onto_pb_caravans = pptr->get_name() == pb_ptr->get_name() and - (go->caravan_name == CARAVAN_D or - go->caravan_name == CARAVAN_E or - go->caravan_name == CARAVAN_F); + (command->caravan_name == CARAVAN_D or + command->caravan_name == CARAVAN_E or + command->caravan_name == CARAVAN_F); if (!(pa_playing_num_onto_pa_caravans or pb_playing_num_onto_pb_caravans)) @@ -274,12 +274,12 @@ void Game::option_play(Player *pptr, GameOption *go) { "a player's own caravan."); if (in_start_stage and - table_ptr->get_caravan_size(go->caravan_name) > 0) + table_ptr->get_caravan_size(command->caravan_name) > 0) throw CaravanGameException( "A numeral card must be played on an empty caravan " "during the Start round."); - table_ptr->play_numeral_card(go->caravan_name, c_hand); + table_ptr->play_numeral_card(command->caravan_name, c_hand); } else { if (in_start_stage) @@ -288,11 +288,10 @@ void Game::option_play(Player *pptr, GameOption *go) { "Start round."); table_ptr->play_face_card( - go->caravan_name, + command->caravan_name, c_hand, - go->pos_caravan); + command->pos_caravan); } - pptr->discard_from_hand_at(go->pos_hand); - go->card = c_hand; + pptr->discard_from_hand_at(command->pos_hand); } diff --git a/src/caravan/model/model.cpp b/src/caravan/model/model.cpp deleted file mode 100644 index edba425..0000000 --- a/src/caravan/model/model.cpp +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) 2022-2024 r3w0p -// The following code can be redistributed and/or -// modified under the terms of the GPL-3.0 License. - -#include "caravan/model/model.h" - -void Model::subscribe(ModelSubscriber *sub) { - subscribers.push_back(sub); -} diff --git a/src/caravan/model/model_tui.cpp b/src/caravan/model/model_tui.cpp deleted file mode 100644 index 223cb82..0000000 --- a/src/caravan/model/model_tui.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright (c) 2022-2024 r3w0p -// The following code can be redistributed and/or -// modified under the terms of the GPL-3.0 License. - -#include "caravan/model/model_tui.h" diff --git a/src/caravan/user/bot_easy.cpp b/src/caravan/user/bot_easy.cpp index 2cc92a7..c771388 100644 --- a/src/caravan/user/bot_easy.cpp +++ b/src/caravan/user/bot_easy.cpp @@ -24,7 +24,7 @@ uint8_t pos_card_numeral(Player *p) { return 0; } -GameOption UserBotEasy::generate_option(Game *g) { +GameCommand UserBotEasy::generate_option(Game *g) { Player *p; uint8_t p_hand_size; Table *t; @@ -52,7 +52,7 @@ GameOption UserBotEasy::generate_option(Game *g) { pcns_me = g->get_player_caravan_names(name); pcns_opp = g->get_player_caravan_names( - name == PLAYER_BOTTOM ? PLAYER_TOP : PLAYER_BOTTOM); + name == PLAYER_ABC ? PLAYER_DEF : PLAYER_ABC); // Add numeral cards for start round if (p->get_moves_count() < MOVES_START_ROUND) { diff --git a/src/caravan/view/view.cpp b/src/caravan/view/view.cpp deleted file mode 100644 index 9606938..0000000 --- a/src/caravan/view/view.cpp +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) 2022-2024 r3w0p -// The following code can be redistributed and/or -// modified under the terms of the GPL-3.0 License. - -#include "caravan/view/view.h" - -void View::subscribe(ViewSubscriber *sub) { - subscribers.push_back(sub); -} diff --git a/src/caravan/view/view_tui.cpp b/src/caravan/view/view_tui.cpp index 32e6d63..a38e27a 100644 --- a/src/caravan/view/view_tui.cpp +++ b/src/caravan/view/view_tui.cpp @@ -35,6 +35,399 @@ const uint16_t HEIGHT_FACES = 2; const uint8_t INPUT_MAX = 4; +const std::string NAME_YOU = "YOU"; +const std::string NAME_BOT = "BOT"; + +const std::string NAME_PL1 = "PL1"; +const std::string NAME_PL2 = "PL2"; + +const std::string NAME_BOT1 = "BOT1"; +const std::string NAME_BOT2 = "BOT2"; + + +typedef struct ViewConfig { + // Messages to users + std::string msg_notif; + std::string msg_err; + + std::string msg_move_abc; + std::string msg_move_def; + + // Names of users + std::string name_abc; + std::string name_def; + std::string name_turn; + + // Most recent command + GameCommand command; +} ViewConfig; + + +std::wstring caravan_to_wstr(CaravanName caravan_name, bool letter_only) { + switch (caravan_name) { + case CARAVAN_A: + return letter_only ? L"A" : L"Caravan A"; + case CARAVAN_B: + return letter_only ? L"B" : L"Caravan B"; + case CARAVAN_C: + return letter_only ? L"C" : L"Caravan C"; + case CARAVAN_D: + return letter_only ? L"D" : L"Caravan D"; + case CARAVAN_E: + return letter_only ? L"E" : L"Caravan E"; + case CARAVAN_F: + return letter_only ? L"F" : L"Caravan F"; + default: + throw CaravanFatalException("Invalid caravan name."); + } +} + +std::wstring suit_to_wstr(Suit suit) { + switch (suit) { + case NO_SUIT: + return L" "; + case SPADES: + return L"♠"; + case CLUBS: + return L"♣"; + case HEARTS: + return L"♥"; + case DIAMONDS: + return L"♦"; + default: + throw CaravanFatalException("Invalid suit."); + } +} + +std::wstring direction_to_wstr(Direction direction) { + switch (direction) { + case ANY: + return L"ANY"; + case ASCENDING: + return L"ASC"; + case DESCENDING: + return L"DES"; + default: + throw CaravanFatalException("Invalid direction."); + } +} + +std::wstring rank_to_wstr(Rank rank, bool lead) { + switch (rank) { + case ACE: + return lead ? L" A" : L"A"; + case TWO: + return lead ? L" 2" : L"2"; + case THREE: + return lead ? L" 3" : L"3"; + case FOUR: + return lead ? L" 4" : L"4"; + case FIVE: + return lead ? L" 5" : L"5"; + case SIX: + return lead ? L" 6" : L"6"; + case SEVEN: + return lead ? L" 7" : L"7"; + case EIGHT: + return lead ? L" 8" : L"8"; + case NINE: + return lead ? L" 9" : L"9"; + case TEN: + return L"10"; + case JACK: + return lead ? L" J" : L"J"; + case QUEEN: + return lead ? L" Q" : L"Q"; + case KING: + return lead ? L" K" : L"K"; + case JOKER: + return L"JO"; + default: + throw CaravanFatalException("Invalid rank."); + } +} + +std::wstring card_num_to_wstr(uint8_t card_num, bool lead) { + switch (card_num) { + case 1: + return lead ? L" 1" : L"1"; + case 2: + return lead ? L" 2" : L"2"; + case 3: + return lead ? L" 3" : L"3"; + case 4: + return lead ? L" 4" : L"4"; + case 5: + return lead ? L" 5" : L"5"; + case 6: + return lead ? L" 6" : L"6"; + case 7: + return lead ? L" 7" : L"7"; + case 8: + return lead ? L" 8" : L"8"; + case 9: + return lead ? L" 9" : L"9"; + case 10: + return L"10"; + default: + throw CaravanFatalException("Invalid card numeral."); + } +} + +void process_first(std::string input, GameCommand *command) { + char c = input.at(0); // minimum input size already checked elsewhere + + switch (c) { + case 'P': + case 'p': + command->option = OPTION_PLAY; + /* + * P2F + * "Play numeral card at hand pos 2 onto caravan F" + * + * P4F8 + * "Play face card at hand pos 4 onto caravan F, slot 8" + */ + break; + + case 'D': + case 'd': + command->option = OPTION_DISCARD; + /* + * D3 + * "Discard card at hand pos 3" + */ + break; + + case 'C': + case 'c': + /* + * CE + * "Clear caravan E" + */ + command->option = OPTION_CLEAR; + break; + + default: + throw CaravanInputException("Invalid option '" + std::string(1, c) + "', must be one of: (P)lay, (D)iscard, (C)lear."); + } +} + +void process_second(std::string input, GameCommand *command) { + if (command->option == OPTION_PLAY or command->option == OPTION_DISCARD) { + + if (input.size() < 2) + throw CaravanInputException("A hand position has not been entered."); + + char c = input.at(1); + + switch (c) { + case '1': + command->pos_hand = 1; + break; + case '2': + command->pos_hand = 2; + break; + case '3': + command->pos_hand = 3; + break; + case '4': + command->pos_hand = 4; + break; + case '5': + command->pos_hand = 5; + break; + case '6': + command->pos_hand = 6; + break; + case '7': + command->pos_hand = 7; + break; + case '8': + command->pos_hand = 8; + break; + default: + throw CaravanInputException("Invalid hand position '" + std::string(1, c) + "'."); + } + + } else if (command->option == OPTION_CLEAR) { + + if (input.size() < 2) + throw CaravanInputException("A caravan name has not been entered."); + + char c = input.at(1); + + switch (c) { + case 'A': + case 'a': + command->caravan_name = CARAVAN_A; + break; + case 'B': + case 'b': + command->caravan_name = CARAVAN_B; + break; + case 'C': + case 'c': + command->caravan_name = CARAVAN_C; + break; + case 'D': + case 'd': + command->caravan_name = CARAVAN_D; + break; + case 'E': + case 'e': + command->caravan_name = CARAVAN_E; + break; + case 'F': + case 'f': + command->caravan_name = CARAVAN_F; + break; + default: + throw CaravanInputException("Invalid caravan name '" + std::string(1, c) + "', must be between: A-F."); + } + + } // else invalid command type, handled during parse of first character +} + +void process_third(std::string input, GameCommand *command) { + if (command->option == OPTION_PLAY) { + + if (input.size() < 3) + throw CaravanInputException("A caravan name has not been entered."); + + char c = input.at(2); + + switch (c) { + case 'A': + case 'a': + command->caravan_name = CARAVAN_A; + break; + case 'B': + case 'b': + command->caravan_name = CARAVAN_B; + break; + case 'C': + case 'c': + command->caravan_name = CARAVAN_C; + break; + case 'D': + case 'd': + command->caravan_name = CARAVAN_D; + break; + case 'E': + case 'e': + command->caravan_name = CARAVAN_E; + break; + case 'F': + case 'f': + command->caravan_name = CARAVAN_F; + break; + default: + throw CaravanInputException("Invalid caravan name '" + std::string(1, c) + "', must be between: A-F."); + } + } +} + +void process_fourth(std::string input, GameCommand *command) { + if (command->option == OPTION_PLAY) { + + if (input.size() < 4) return; // optional, not an error + + char c = input.at(3); + + switch (c) { + case '1': + command->pos_caravan = 1; + break; + case '2': + command->pos_caravan = 2; + break; + case '3': + command->pos_caravan = 3; + break; + case '4': + command->pos_caravan = 4; + break; + case '5': + command->pos_caravan = 5; + break; + case '6': + command->pos_caravan = 6; + break; + case '7': + command->pos_caravan = 7; + break; + case '8': + command->pos_caravan = 8; + break; + default: + throw CaravanInputException("Invalid caravan position '" + std::string(1, c) + "'."); + } + } +} + +bool process_exit(std::string input, GameCommand *command) { + if( + input.size() == 4 and + (input.at(0) == 'E' or input.at(0) == 'e') and + (input.at(1) == 'X' or input.at(1) == 'x') and + (input.at(2) == 'I' or input.at(2) == 'i') and + (input.at(3) == 'T' or input.at(3) == 't') + ) { + command->option = OPTION_EXIT; + return true; + } + + return false; +} + +GameCommand ViewTUI::parse_user_input(std::string input, bool confirmed) { + GameCommand command; + bool is_exit; + + if(closed) return command; + + if(input.empty()) return command; + + // TODO A future feature that highlights parts of the board as a command is typed, + // so that the user has a better idea of what their command will do. + if(!confirmed) return command; + + /* + * EXIT + */ + is_exit = process_exit(input, &command); + + if(!is_exit) { + /* + * FIRST + * - COMMAND TYPE + */ + process_first(input, &command); + + /* + * SECOND + * - HAND POSITION or + * - CARAVAN NAME + */ + process_second(input, &command); + + /* + * THIRD + * - CARAVAN NAME + */ + process_third(input, &command); + + /* + * FOURTH + * - CARAVAN POSITION (used when selecting Face card only) + */ + process_fourth(input, &command); + } + + return command; +} + std::shared_ptr gen_position(std::string position) { using namespace ftxui; return text(position) | borderEmpty | size(WIDTH, EQUAL, WIDTH_POSITION) | size(HEIGHT, EQUAL, HEIGHT_POSITION); @@ -62,7 +455,7 @@ std::shared_ptr gen_caravan_card(std::string position, std::wstring }) | size(HEIGHT, EQUAL, HEIGHT_CARAVAN_CARD); } -std::shared_ptr gen_caravan(std::string title, bool top) { +std::shared_ptr gen_caravan(Game *game, CaravanName cn, bool top) { using namespace ftxui; std::shared_ptr content; @@ -90,6 +483,19 @@ std::shared_ptr gen_caravan(std::string title, bool top) { }); } + std::wstring title = L" " + caravan_to_wstr(cn, true) + L" "; + + if (game->get_table()->get_caravan_size(cn) > 0) { + title += + L"(" + + std::to_wstring(game->get_table()->get_caravan_bid(cn)) + + L", " + + direction_to_wstr(game->get_table()->get_caravan_direction(cn)) + + L", " + + suit_to_wstr(game->get_table()->get_caravan_suit(cn)) + + L") "; + } + return window( text(title) | hcenter | bold, content @@ -104,7 +510,7 @@ std::shared_ptr gen_deck_card(std::string position, std::wstring ca }) | size(HEIGHT, EQUAL, HEIGHT_CARAVAN_CARD); } -std::shared_ptr gen_deck(std::string title, bool top, bool hide) { +std::shared_ptr gen_deck(Game *game, ViewConfig *vc, bool top, bool hide) { // TODO bool hide using namespace ftxui; std::shared_ptr content; @@ -133,6 +539,10 @@ std::shared_ptr gen_deck(std::string title, bool top, bool hide) { }); } + Player *p = game->get_player(top ? PLAYER_DEF : PLAYER_ABC); + uint8_t total_cards = p->get_size_deck() + p->get_size_hand(); + std::string title = " " + (top ? vc->name_def : vc->name_abc) + " (" + std::to_string(total_cards) + ") "; + return window( text(title) | hcenter | bold, content @@ -140,42 +550,41 @@ std::shared_ptr gen_deck(std::string title, bool top, bool hide) { } std::shared_ptr gen_input( - std::shared_ptr comp_user_input, - std::array notifs, - std::array moves) { + ViewConfig *vc, + std::shared_ptr *comp_user_input) { using namespace ftxui; Elements e; e.push_back(separatorEmpty()); - e.push_back(hbox(separatorEmpty(), text("YOU > "), comp_user_input->Render(), separatorEmpty())); // TODO + e.push_back(hbox(separatorEmpty(), text(vc->name_turn + " > "), (*comp_user_input)->Render(), separatorEmpty())); // TODO e.push_back(separatorEmpty()); - if(!notifs[0].empty() || !notifs[1].empty()) { + if(!vc->msg_notif.empty() || !vc->msg_err.empty()) { e.push_back(separator()); e.push_back(separatorEmpty()); - if(!notifs[0].empty()) { - e.push_back(hbox(separatorEmpty(), paragraph(notifs[0]), separatorEmpty())); + if(!vc->msg_notif.empty()) { + e.push_back(hbox(separatorEmpty(), paragraph(vc->msg_notif), separatorEmpty())); e.push_back(separatorEmpty()); } - if(!notifs[1].empty()) { - e.push_back(hbox(separatorEmpty(), paragraph(notifs[1]), separatorEmpty())); + if(!vc->msg_err.empty()) { + e.push_back(hbox(separatorEmpty(), paragraph(vc->msg_err), separatorEmpty())); e.push_back(separatorEmpty()); } } - if(!moves[0].empty() || !moves[1].empty()) { + if(!vc->msg_move_abc.empty() || !vc->msg_move_def.empty()) { e.push_back(separator()); e.push_back(separatorEmpty()); - if(!moves[0].empty()) { - e.push_back(hbox(separatorEmpty(), paragraph(moves[0]), separatorEmpty())); + if(!vc->msg_move_abc.empty()) { + e.push_back(hbox(separatorEmpty(), paragraph(vc->msg_move_abc), separatorEmpty())); e.push_back(separatorEmpty()); } - if(!moves[1].empty()) { - e.push_back(hbox(separatorEmpty(), paragraph(moves[1]), separatorEmpty())); + if(!vc->msg_move_def.empty()) { + e.push_back(hbox(separatorEmpty(), paragraph(vc->msg_move_def), separatorEmpty())); e.push_back(separatorEmpty()); } } @@ -184,34 +593,34 @@ std::shared_ptr gen_input( } std::shared_ptr gen_game( - std::shared_ptr comp_user_input, - std::array notifs, - std::array moves) { + Game *game, + ViewConfig *vc, + std::shared_ptr *comp_user_input) { using namespace ftxui; return hbox({ // OUTERMOST AREA vbox({ // GAME AREA hbox({ // TOP GAME AREA - gen_caravan(" D ", true), + gen_caravan(game, CARAVAN_D, true), separatorEmpty(), separatorEmpty(), - gen_caravan(" E ", true), + gen_caravan(game, CARAVAN_E, true), separatorEmpty(), separatorEmpty(), - gen_caravan(" F (100, ASC) ", true), + gen_caravan(game, CARAVAN_F, true), }), // top game area separatorEmpty(), hbox({ // BOTTOM GAME AREA - gen_caravan(" A ", false), + gen_caravan(game, CARAVAN_A, false), separatorEmpty(), separatorEmpty(), - gen_caravan(" B (100, ASC) ", false), + gen_caravan(game, CARAVAN_B, false), separatorEmpty(), separatorEmpty(), - gen_caravan(" C ", false), + gen_caravan(game, CARAVAN_C, false), }), // bottom game area }), // game area @@ -223,9 +632,9 @@ std::shared_ptr gen_game( separatorEmpty(), vbox({ // DECK AREA - gen_deck(" BOT (100) ", true, true), + gen_deck(game, vc, true, true), separatorEmpty(), - gen_deck(" YOU ", false, false), + gen_deck(game, vc, false, false), }), // deck area separatorEmpty(), @@ -237,7 +646,7 @@ std::shared_ptr gen_game( vbox({ // INPUT AREA hbox({}) | borderEmpty | size(HEIGHT, EQUAL, HEIGHT_CARAVAN), separatorEmpty(), - gen_input(comp_user_input, notifs, moves) + gen_input(vc, comp_user_input) }), // input area }) | center; // outermost area @@ -263,24 +672,48 @@ std::shared_ptr gen_closed() { }) | center; } +void set_name_turn(ViewConfig *vc, Game *game) { + vc->name_turn = game->get_player_turn() == PLAYER_ABC ? vc->name_abc : vc->name_def; +} + +/* + * PUBLIC + */ + void ViewTUI::run() { - // TODO on_model_exit, close the game using namespace ftxui; if(closed) return; // Screen config - bool exited; + bool called_exit; Dimensions terminal_size {}; // User input std::string user_input; - std::string command; + std::string raw_command; bool confirmed; - - // Messages to users - std::array notifs; - std::array moves; + + // Config for rendering game + ViewConfig vc; + + // Set names of users based on who is human or not + if(user_abc->is_human() and user_def->is_human()) { + vc.name_abc = NAME_PL1; + vc.name_def = NAME_PL2; + + } else if(user_abc->is_human() and !user_def->is_human()) { + vc.name_abc = NAME_YOU; + vc.name_def = NAME_BOT; + + } else if(!user_abc->is_human() and user_def->is_human()) { + vc.name_abc = NAME_BOT; + vc.name_def = NAME_YOU; + + } else { // both are bots + vc.name_abc = NAME_BOT1; + vc.name_def = NAME_BOT2; + } // Create screen ScreenInteractive screen = ScreenInteractive::Fullscreen(); @@ -302,21 +735,23 @@ void ViewTUI::run() { auto component = Container::Vertical({ comp_user_input }); // Initial notifications - notifs[0] = "Welcome to Caravan"; - notifs[1] = "YOU to move first."; // TODO + set_name_turn(&vc, game); + vc.msg_notif = "Welcome to Caravan"; + vc.msg_err = vc.name_turn + " to move first."; // Tweak how the component tree is rendered: auto renderer = Renderer(component, [&] { try { if(closed) { - if(!exited) { - exited = true; + if (!called_exit) { + called_exit = true; screen.Exit(); } return gen_closed(); } - terminal_size = Terminal::Size(); + set_name_turn(&vc, game); + confirmed = false; // Error screen if less than minimum terminal dimensions if (terminal_size.dimx < MIN_X || terminal_size.dimy < MIN_Y) { @@ -324,36 +759,43 @@ void ViewTUI::run() { return gen_terminal_too_small(terminal_size); } - // Handle user input - command = user_input; - if(command.ends_with('\n')) { - command.pop_back(); // removes '\n' - user_input = ""; + // TODO if current turn is bot... (also, block input) + + // Create new command if ENTER key pressed (i.e., if newline) + raw_command = user_input; + if(raw_command.ends_with('\n')) { + raw_command.pop_back(); // remove newline confirmed = true; - if(!command.empty()) - notifs[0] = "YOU entered: " + command; // TODO player name + user_input = ""; - } else { - confirmed = false; + if(!raw_command.empty()) + vc.msg_notif = vc.name_turn + " entered: " + raw_command; } - // Send input to subscribers (i.e., Controller) + // Process command try { - for (ViewSubscriber *vs: subscribers) { - vs->on_view_user_input(command, confirmed); + vc.command = parse_user_input(raw_command, confirmed); + + switch (vc.command.option) { + case NO_OPTION: + break; + case OPTION_EXIT: + closed = true; + return gen_closed(); + default: + game->play_option(&vc.command); + // TODO display what happened } + + } catch(CaravanGameException &e) { + vc.msg_err = e.what(); + } catch(CaravanInputException &e) { - // TODO on exception, show the previous command entered alongside error message - // "YOU entered P4F8. [error message]" - notifs[1] = e.what(); + vc.msg_err = e.what(); } - // TODO replace this with an 'exit early' signal from the model - // (i.e. not exciting because game has finished, but because of an exit signal from user) - //if(command == "EXIT") screen.Exit(); - - return gen_game(comp_user_input, notifs, moves); + return gen_game(game, &vc, &comp_user_input); } catch(...) { // Close gracefully on any unhandled exceptions @@ -365,3 +807,17 @@ void ViewTUI::run() { screen.Loop(renderer); screen.Clear(); } + +void ViewTUI::close() { + if(!closed) { + // TODO user_abc->close(); + // TODO user_def->close(); + game->close(); + + delete user_abc; + delete user_def; + delete game; + + closed = true; + } +} diff --git a/test/caravan/model/test_game.cpp b/test/caravan/model/test_game.cpp index ca73c61..0935cf5 100644 --- a/test/caravan/model/test_game.cpp +++ b/test/caravan/model/test_game.cpp @@ -10,7 +10,7 @@ TEST (TestGame, Close) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; Game g{gc}; @@ -23,26 +23,26 @@ TEST (TestGame, GetPlayer_Both) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; Game g{gc}; - ASSERT_EQ(g.get_player(PLAYER_BOTTOM)->get_name(), PLAYER_BOTTOM); - ASSERT_EQ(g.get_player(PLAYER_TOP)->get_name(), PLAYER_TOP); + ASSERT_EQ(g.get_player(PLAYER_ABC)->get_name(), PLAYER_ABC); + ASSERT_EQ(g.get_player(PLAYER_DEF)->get_name(), PLAYER_DEF); } TEST (TestGame, GetPlayer_Error_AlreadyClosed) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; Game g{gc}; g.close(); try { - g.get_player(PLAYER_BOTTOM); + g.get_player(PLAYER_ABC); FAIL(); } catch (CaravanFatalException &e) { @@ -56,7 +56,7 @@ TEST (TestGame, GetPlayer_Error_InvalidName) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; Game g{gc}; @@ -75,18 +75,18 @@ TEST (TestGame, GetPlayerTurn) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; Game g{gc}; - ASSERT_EQ(g.get_player_turn(), PLAYER_BOTTOM); + ASSERT_EQ(g.get_player_turn(), PLAYER_ABC); } TEST (TestGame, GetPlayerTurn_Error_AlreadyClosed) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; Game g{gc}; @@ -107,7 +107,7 @@ TEST (TestGame, GetTable_Error_AlreadyClosed) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; Game g{gc}; @@ -128,7 +128,7 @@ TEST (TestGame, GetWinner_NoMoves) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; Game g{gc}; @@ -139,7 +139,7 @@ TEST (TestGame, GetWinner_Error_AlreadyClosed) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; Game g{gc}; @@ -160,15 +160,15 @@ TEST (TestGame, PlayOption_Error_AlreadyClosed) { GameConfig gc = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; - GameOption go = {OPTION_DISCARD, 1, NO_CARAVAN, 0}; + GameCommand command = {OPTION_DISCARD, 1, NO_CARAVAN, 0}; Game g{gc}; g.close(); try { - g.play_option(&go); + g.play_option(&command); FAIL(); } catch (CaravanFatalException &e) { @@ -179,16 +179,16 @@ TEST (TestGame, PlayOption_Error_AlreadyClosed) { } TEST (TestGame, PlayOption_Error_StartRound_Remove) { - GameConfig gc = { + GameConfig config = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; - GameOption go = {OPTION_DISCARD, 1, NO_CARAVAN, 0}; - Game g{gc}; + GameCommand command = {OPTION_DISCARD, 1, NO_CARAVAN, 0}; + Game g{config}; try { - g.play_option(&go); + g.play_option(&command); FAIL(); } catch (CaravanGameException &e) { @@ -199,16 +199,16 @@ TEST (TestGame, PlayOption_Error_StartRound_Remove) { } TEST (TestGame, PlayOption_Error_StartRound_Clear) { - GameConfig gc = { + GameConfig config = { 30, 1, true, 30, 1, true, - PLAYER_BOTTOM + PLAYER_ABC }; - GameOption go = {OPTION_CLEAR, 0, CARAVAN_A, 0}; - Game g{gc}; + GameCommand command = {OPTION_CLEAR, 0, CARAVAN_A, 0}; + Game g{config}; try { - g.play_option(&go); + g.play_option(&command); FAIL(); } catch (CaravanGameException &e) { diff --git a/test/caravan/model/test_player.cpp b/test/caravan/model/test_player.cpp index 87ee656..50b9a48 100644 --- a/test/caravan/model/test_player.cpp +++ b/test/caravan/model/test_player.cpp @@ -9,7 +9,7 @@ TEST (TestPlayer, GetFromHandAt_Position1) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); Card c_get; Card c_take; Card c_getagain; @@ -29,14 +29,14 @@ TEST (TestPlayer, GetFromHandAt_Position1) { TEST (TestPlayer, GetName) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); - ASSERT_EQ(pl.get_name(), PLAYER_BOTTOM); + ASSERT_EQ(pl.get_name(), PLAYER_ABC); } TEST (TestPlayer, GetFromHandAt_Error_HandEmpty) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); for (int i = 0; i < 30; ++i) { pl.discard_from_hand_at(1); @@ -59,7 +59,7 @@ TEST (TestPlayer, GetFromHandAt_Error_HandEmpty) { TEST (TestPlayer, GetFromHandAt_Error_PositionTooLow) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); try { pl.get_from_hand_at(0); @@ -74,7 +74,7 @@ TEST (TestPlayer, GetFromHandAt_Error_PositionTooLow) { TEST (TestPlayer, GetFromHandAt_Error_PositionTooHigh) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); try { pl.get_from_hand_at(9); @@ -89,21 +89,21 @@ TEST (TestPlayer, GetFromHandAt_Error_PositionTooHigh) { TEST (TestPlayer, GetSizeDeck_Deck30) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); ASSERT_EQ(pl.get_size_deck(), 22); } TEST (TestPlayer, GetSizeHand_Deck30) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); ASSERT_EQ(pl.get_size_hand(), 8); } TEST (TestPlayer, IncrementMovesCount_ThreeTimes) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); ASSERT_EQ(pl.get_moves_count(), 0); pl.increment_moves(); @@ -116,7 +116,7 @@ TEST (TestPlayer, IncrementMovesCount_ThreeTimes) { TEST (TestPlayer, RemoveFromHandAt_Position1_StartRound) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); Card c_get; Card c_take; Card c_getagain; @@ -139,7 +139,7 @@ TEST (TestPlayer, RemoveFromHandAt_Position1_StartRound) { TEST (TestPlayer, RemoveFromHandAt_Error_HandEmpty) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); for (int i = 0; i < 30; ++i) { pl.discard_from_hand_at(1); @@ -160,7 +160,7 @@ TEST (TestPlayer, RemoveFromHandAt_Error_HandEmpty) { TEST (TestPlayer, RemoveFromHandAt_Error_PositionTooLow) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); try { pl.discard_from_hand_at(0); @@ -175,7 +175,7 @@ TEST (TestPlayer, RemoveFromHandAt_Error_PositionTooLow) { TEST (TestPlayer, RemoveFromHandAt_Error_PositionTooHigh) { Deck *d = DeckBuilder::build_caravan_deck(30, 1, true); - Player pl = Player(PLAYER_BOTTOM, d); + Player pl = Player(PLAYER_ABC, d); try { pl.discard_from_hand_at(9);