This commit is contained in:
anon 2024-06-28 17:08:57 +02:00
commit d371ebb716
5 changed files with 109 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.out
*.o
.gdb_history
game_theory_tournament

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
main:
g++ source/main.cpp -o game_theory_tournament
clean:
-rm game_theory_tournament

18
source/Player.hpp Normal file
View File

@ -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

17
source/RandomPlayer.hpp Normal file
View File

@ -0,0 +1,17 @@
#include "Player.hpp"
#include <stdlib.h>
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;
}
};

65
source/main.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <vector>
#include "Player.hpp"
#include "RandomPlayer.hpp"
using namespace std;
vector<Player *> 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);
}
}