commit d371ebb71667dea1dd1b63a8b249eef28ae3d417 Author: anon Date: Fri Jun 28 17:08:57 2024 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..763e3cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.out +*.o +.gdb_history +game_theory_tournament diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d40fb79 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +main: + g++ source/main.cpp -o game_theory_tournament + +clean: + -rm game_theory_tournament diff --git a/source/Player.hpp b/source/Player.hpp new file mode 100644 index 0000000..e26d6bd --- /dev/null +++ b/source/Player.hpp @@ -0,0 +1,18 @@ +#ifndef PLAYER_H +#define PLAYER_H + +typedef enum { + COOPERATE, + CONFLICT, +} reaction_t; + +class Player { + public: + long points = 0; + const char * name; + virtual void new_game() = 0; + virtual reaction_t play() = 0; + virtual void push_result(reaction_t opponents_last_reaction) = 0; +}; + +#endif diff --git a/source/RandomPlayer.hpp b/source/RandomPlayer.hpp new file mode 100644 index 0000000..4a766ad --- /dev/null +++ b/source/RandomPlayer.hpp @@ -0,0 +1,17 @@ +#include "Player.hpp" + +#include + +class RandomPlayer : public Player { + public: + RandomPlayer() { + name = "Randy"; + } + + void new_game() override { ; } + void push_result(reaction_t opponents_last_reaction) override { ; } + + reaction_t play() override { + return (rand() % 2) ? COOPERATE : CONFLICT; + } +}; diff --git a/source/main.cpp b/source/main.cpp new file mode 100644 index 0000000..45507b8 --- /dev/null +++ b/source/main.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +#include "Player.hpp" +#include "RandomPlayer.hpp" + +using namespace std; + +vector players; + +const int ROUND_COUNT = 200; + +inline +void print_round(reaction_t r1, reaction_t r2) { + const char * const color_table[] = { + [COOPERATE] = "\033[32m", + [CONFLICT] = "\033[31m", + }; + + printf(" %sO\033[0m %sO\033[0m\n", color_table[r1], color_table[r2]); +} + +inline +void play_round(Player * const p1, Player * const p2) { + reaction_t p1_play = p1->play(); + reaction_t p2_play = p2->play(); + + print_round(p1_play, p2_play); + + if (p1_play == COOPERATE && p2_play == COOPERATE) { + p1->points += 3; + p2->points += 3; + } else if (p1_play == CONFLICT && p2_play == CONFLICT) { + p1->points += 1; + p2->points += 1; + } else { + Player * const pw = (p1_play == CONFLICT) ? p1 : p2; + pw->points += 5; + } +} + +signed main() { + srand(time(NULL)); + + players.push_back(new RandomPlayer()); + players.push_back(new RandomPlayer()); + players.push_back(new RandomPlayer()); + + for (int i = 0; i < players.size() - 1; i++) { + for (int h = i + 1; h < players.size(); h++) { + printf("%s VS %s\n", players[i]->name, players[h]->name); + players[i]->new_game(); + players[h]->new_game(); + for (int c = 0; c < ROUND_COUNT; c++) { + play_round(players[i], players[h]); + } + } + } + + for (const auto &p : players) { + printf("%s: %ld\n", p->name, p->points); + } +}