-
Notifications
You must be signed in to change notification settings - Fork 0
/
Match.cpp
59 lines (48 loc) · 1.31 KB
/
Match.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include "Match.h"
Match::Match(Player *p1, Player *p2, int rounds_total, float gamma, float noise)
:p1(p1), p2(p2), rounds_total(rounds_total), gamma(gamma), noise(noise)
{
rounds_played=0;
}
MatchResults& Match::play()
{
p1->reset();
p2->reset();
double p_continue = 1;
std::vector<DecisionPair> rounds_results;
DecisionPair round_result;
MatchResults match_results;
while (p_continue > gamma && rounds_played <= rounds_total)
{
srand(time(NULL)*(rounds_played+1));
p_continue = ((double) rand()) / RAND_MAX;
round_result = play_round();
rounds_results.push_back(round_result);
rounds_played++;
}
match_results = {};
match_results.rounds_results = rounds_results;
this->match_results = match_results;
return match_results;
}
DecisionPair Match::play_round()
{
Action a1 = p1->strategy(p2);
Action a2 = p2->strategy(p1);
DecisionPair result = DecisionPair(a1, a2);
if (noise > 0)
add_noise(result);
p1->update(a1, a2);
p2->update(a2, a1);
return result;
}
void Match::add_noise(DecisionPair& dp)
{
srand(time(NULL)*(rounds_played+2));
double p_noise = ((double) rand()) / RAND_MAX;
if (p_noise < noise)
{
dp = DecisionPair(dp.second, dp.first);
}
}