game_theory_tournament/source/main.cpp

78 lines
1.9 KiB
C++
Raw Normal View History

2024-06-28 11:08:57 -04:00
#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;
}
2024-06-28 18:29:47 -04:00
2024-06-28 18:29:35 -04:00
p1->push_result(p2_play);
p2->push_result(p1_play);
2024-06-28 11:08:57 -04:00
}
signed main() {
2024-06-28 18:29:47 -04:00
/* we seed, rand for you, in case you wish to use it,
* please do not re seed it
*/
2024-06-28 11:08:57 -04:00
srand(time(NULL));
2024-06-28 18:29:47 -04:00
// YYY: Every player is added here
2024-06-28 11:08:57 -04:00
players.push_back(new RandomPlayer());
players.push_back(new RandomPlayer());
players.push_back(new RandomPlayer());
2024-06-28 18:29:47 -04:00
// --
2024-06-28 11:08:57 -04:00
2024-06-28 18:29:47 -04:00
/* Every players playes with every player exactly once
*/
2024-06-28 11:08:57 -04:00
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();
2024-06-28 18:29:47 -04:00
/* Each game takes some number of turns
*/
2024-06-28 11:08:57 -04:00
for (int c = 0; c < ROUND_COUNT; c++) {
play_round(players[i], players[h]);
}
}
}
2024-06-28 18:29:47 -04:00
// Results
2024-06-28 11:08:57 -04:00
for (const auto &p : players) {
printf("%s: %ld\n", p->name, p->points);
}
}