Skip to content

Commit

Permalink
bot OK
Browse files Browse the repository at this point in the history
  • Loading branch information
r3w0p committed May 24, 2024
1 parent 3458ba7 commit f7c8cf3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
5 changes: 3 additions & 2 deletions include/caravan/user/bot_easy.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

#include "caravan/user/user.h"

class UserBotEasy : public UserBot {
class UserBotEasy : public User {
public:
explicit UserBotEasy(PlayerName pn) : UserBot(pn) {};
explicit UserBotEasy(PlayerName pn) : User(pn) {};

bool is_human() override;
GameCommand generate_option(Game *g) override;
};

Expand Down
12 changes: 2 additions & 10 deletions include/caravan/user/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,16 @@ class User {
explicit User(PlayerName pn) : name(pn) {};

PlayerName get_name() { return name; }

virtual bool is_human() = 0;
virtual GameCommand generate_option(Game *g) = 0;
};

class UserHuman : public User {
public:
explicit UserHuman(PlayerName pn) : User(pn) {};

bool is_human() override;
};

class UserBot : public User {
public:
explicit UserBot(PlayerName pn) : User(pn) {};

bool is_human() override;

virtual GameCommand generate_option(Game *g) = 0;
GameCommand generate_option(Game *g) override;
};

#endif //CARAVAN_USER_H
2 changes: 1 addition & 1 deletion src/caravan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main() {

try {
user_abc = new UserHuman(PLAYER_ABC);
user_def = new UserHuman(PLAYER_DEF);
user_def = new UserBotEasy(PLAYER_DEF);
game = new Game(config); // TODO

v = new ViewTUI(user_abc, user_def, game);
Expand Down
8 changes: 8 additions & 0 deletions src/caravan/user/bot_easy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ uint8_t pos_card_numeral(Player *p) {
return 0;
}

/*
* PUBLIC
*/

bool UserBotEasy::is_human() {
return false;
}

GameCommand UserBotEasy::generate_option(Game *g) {
Player *p;
uint8_t p_hand_size;
Expand Down
4 changes: 2 additions & 2 deletions src/caravan/user/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ bool UserHuman::is_human() {
return true;
}

bool UserBot::is_human() {
return false;
GameCommand UserHuman::generate_option(Game *g) {
return {};
}
39 changes: 20 additions & 19 deletions src/caravan/view/view_tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ std::shared_ptr<ftxui::Node> suit_to_text(ViewConfig *vc, Suit suit) {
return node_suit;
case SPADES:
case CLUBS:
return node_suit | color(Color::Blue);
return node_suit | color(Color::Palette16::BlueLight);
case HEARTS:
case DIAMONDS:
return node_suit | color(Color::Red);
return node_suit | color(Color::Palette16::RedLight);
default:
throw CaravanFatalException("Invalid suit.");
}
Expand Down Expand Up @@ -860,9 +860,7 @@ void ViewTUI::run() {
// Tweak how the component tree is rendered:
auto renderer = Renderer(component, [&] {
try {
if (closed) {
return gen_closed();
}
if (closed) { return gen_closed(); }

terminal_size = Terminal::Size();
set_current_turn(&vc, game);
Expand All @@ -874,24 +872,27 @@ void ViewTUI::run() {
return gen_terminal_too_small(terminal_size);
}

// 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;

user_input = "";

if (!raw_command.empty()) {
vc.msg_notif = vc.name_turn + " entered: " + raw_command;
if(vc.user_turn->is_human()) {
// 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;
user_input = "";

if (!raw_command.empty()) {
vc.msg_notif = vc.name_turn + " entered: " + raw_command;
}
}

} else { // user with current turn is a bot
vc.command = vc.user_turn->generate_option(game); // TODO generate raw command instead
vc.msg_notif = vc.name_turn + " made its move."; // TODO display raw command
}

// Process command
try {
vc.command = parse_user_input(raw_command, confirmed);
// Parse raw command to get usable command
if (vc.user_turn->is_human()) { vc.command = parse_user_input(raw_command, confirmed); } // TODO change to allow for bot's raw command

switch (vc.command.option) {
case NO_OPTION:
Expand Down

0 comments on commit f7c8cf3

Please sign in to comment.