Skip to content

Commit

Permalink
clean-up progress for new idea on ai
Browse files Browse the repository at this point in the history
train player ABC only; DEF makes purely random decisions; no need for bot player to do all of this; did some additional clean-up on close() and smart pointers etc.
  • Loading branch information
r3w0p committed Oct 24, 2024
1 parent 72a2ab0 commit 3733938
Show file tree
Hide file tree
Showing 31 changed files with 418 additions and 709 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(PROJECT_VERSION 1.3.0)
set(PROJECT_VERSION 2.0.0)
set(PROJECT_DESCRIPTION "A command-line version of the Caravan card game from Fallout: New Vegas.")
set(PROJECT_COPYRIGHT "Copyright (c) 2022-2024 r3w0p")
set(PROJECT_URL "https://github.com/r3w0p/caravan")
Expand Down Expand Up @@ -55,9 +55,11 @@ include_directories(include)
add_library(core
"include/caravan/core/common.h"
"include/caravan/core/exceptions.h"
"include/caravan/core/training.h"

"src/caravan/core/common.cpp"
"src/caravan/core/exceptions.cpp"
"src/caravan/core/training.cpp"
)

add_library(model
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
| (_| (_| | | | (_| |\ \/ / (_| | | | |
\___\__,_|_| \__,_| \__/ \__,_|_| |_|

| v1.3.0 | GPL-3.0 | (c) 2022-2024 r3w0p |
| v2.0.0 | GPL-3.0 | (c) 2022-2024 r3w0p |

A command-line version of the Caravan card game from Fallout: New Vegas.

Expand Down
2 changes: 2 additions & 0 deletions include/caravan/core/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@ std::string caravan_letter(CaravanName caravan_name);

uint8_t numeral_rank_value(Card c);

GameCommand generate_command(std::string input, bool confirmed);

#endif //CARAVAN_CORE_COMMON_H
13 changes: 11 additions & 2 deletions include/caravan/core/training.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
*/

const uint16_t SIZE_ACTION_SPACE = 299;
const uint16_t SIZE_GAME_STATE = 201;
const uint16_t SIZE_GAME_STATE = 200;

const uint8_t NUM_PLAYER_ABC = 1;
const uint8_t NUM_PLAYER_DEF = 2;

/*
* TYPES
*/

typedef std::array<std::string, SIZE_ACTION_SPACE> ActionSpace;
typedef std::array<uint16_t, SIZE_GAME_STATE> GameState;
typedef std::map<GameState, std::map<std::string, uint16_t>> QTable;
typedef std::map<GameState, std::map<std::string, float>> QTable;

typedef struct TrainConfig {
float discount{0.0};
Expand All @@ -33,6 +36,10 @@ typedef struct TrainConfig {
uint32_t episode{0};
} TrainConfig;

std::uniform_int_distribution<uint8_t> dist_first_player(NUM_PLAYER_ABC, NUM_PLAYER_DEF);
std::uniform_int_distribution<uint16_t> dist_action(0, SIZE_ACTION_SPACE - 1);
std::uniform_real_distribution<float> dist_explore(0, 1);

/*
* FUNCTIONS
*/
Expand All @@ -41,4 +48,6 @@ uint8_t card_to_uint8_t(Card c);
void get_game_state(GameState *gs, Game *game, PlayerName pname);
void populate_action_space(ActionSpace *as);

void train_on_game(Game *game, QTable &q_table, ActionSpace &action_space, TrainConfig &tc, std::mt19937 &gen);

#endif //CARAVAN_CORE_TRAINING_H
5 changes: 1 addition & 4 deletions include/caravan/model/caravan.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Caravan {
CaravanName name;
Track track;
uint8_t i_track;
bool closed;

static uint8_t numeral_rank_to_uint8_t(Rank rank);

Expand All @@ -32,7 +31,7 @@ class Caravan {
* @param cvname The caravan name.
*/
explicit Caravan(CaravanName cvname) :
name(cvname), track({}), i_track(0), closed(false) {};
name(cvname), track({}), i_track(0) {};

void clear();

Expand All @@ -55,8 +54,6 @@ class Caravan {
void remove_rank(Rank rank, uint8_t pos_exclude);

void remove_suit(Suit suit, uint8_t pos_exclude);

void close();
};

#endif //CARAVAN_MODEL_CARAVAN_H
7 changes: 2 additions & 5 deletions include/caravan/model/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Game {
Player *pa_ptr{};
Player *pb_ptr{};
Player *p_turn;
bool closed;

int8_t compare_bids(CaravanName cvname1, CaravanName cvname2);

Expand All @@ -33,9 +32,9 @@ class Game {
public:
explicit Game(GameConfig *gc);

static CaravanName get_opposite_caravan_name(CaravanName cvname);
~Game();

void close();
static CaravanName get_opposite_caravan_name(CaravanName cvname);

Player *get_player(PlayerName pname);

Expand All @@ -51,8 +50,6 @@ class Game {

PlayerName get_winner();

bool is_closed();

void play_option(GameCommand *command);

bool check_option(GameCommand *command); // TODO
Expand Down
3 changes: 1 addition & 2 deletions include/caravan/model/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ class Player {
Hand hand;
uint8_t i_hand;
uint16_t moves;
bool closed;

public:
explicit Player(PlayerName pn, Deck *d);

void close();
~Player();

Card get_from_hand_at(uint8_t pos);

Expand Down
6 changes: 2 additions & 4 deletions include/caravan/model/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <cstdint>
#include "caravan/model/caravan.h"


class Table {
protected:
Caravan *a = new Caravan(CARAVAN_A);
Expand All @@ -20,12 +19,11 @@ class Table {
Caravan *f = new Caravan(CARAVAN_F);

std::array<Caravan *, TABLE_CARAVANS_MAX> caravans = {a, b, c, d, e, f};
bool closed;

public:
explicit Table();
explicit Table() = default;

void close();
~Table();

Caravan *get_caravan(CaravanName cvname);

Expand Down
1 change: 1 addition & 0 deletions include/caravan/user/bot/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class BotFactory {
public:
BotFactory() = delete;

static UserBot *get(std::string name, PlayerName player_name);
};

Expand Down
1 change: 0 additions & 1 deletion include/caravan/user/bot/normal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class UserBotNormal : public UserBot {
public:
explicit UserBotNormal(PlayerName pn) : UserBot(pn){};

void close() override;
std::string request_move(Game *game) override;
};

Expand Down
21 changes: 0 additions & 21 deletions include/caravan/user/bot/train.h

This file was deleted.

14 changes: 4 additions & 10 deletions include/caravan/user/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@
class User {
protected:
PlayerName name;
bool closed;
public:
virtual ~User() = default;

explicit User(PlayerName pn) : name(pn), closed(false) {};

PlayerName get_name() { return name; }
explicit User(PlayerName pn) : name(pn) {};

virtual void close() = 0;
virtual ~User() = default;
virtual bool is_human() = 0;
virtual std::string request_move(Game *game) = 0;

PlayerName get_name() { return name; }
};

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

void close() override { closed = true; }
bool is_human() override { return true; }
std::string request_move(Game *game) override { return {}; }
};
Expand All @@ -37,9 +33,7 @@ class UserBot : public User {
public:
explicit UserBot(PlayerName pn) : User(pn) {};

void close() override { closed = true; }
bool is_human() override { return false; }
std::string request_move(Game *game) override { return {}; }
};

#endif //CARAVAN_USER_H
4 changes: 1 addition & 3 deletions include/caravan/view/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ class View {
protected:
ViewConfig *vc;
Game *game;
bool closed;
public:
explicit View(ViewConfig *vc, Game *game);
virtual ~View() = default;

virtual void run() = 0;

virtual void close() = 0;
};

#endif //CARAVAN_VIEW_H
2 changes: 0 additions & 2 deletions include/caravan/view/view_tui.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class ViewTUI : public View {
explicit ViewTUI(ViewConfig *vc, Game *game) : View(vc, game) {};

void run() override;

void close() override;
};

#endif //CARAVAN_VIEW_TUI_H
Loading

0 comments on commit 3733938

Please sign in to comment.