-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.cpp
More file actions
executable file
·131 lines (116 loc) · 2.61 KB
/
Agent.cpp
File metadata and controls
executable file
·131 lines (116 loc) · 2.61 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "Agent.h"
#include "rnorrexp.c"
// Default Agent Constructor
Agent::Agent(){
}
Agent::Agent(int numb,int avgAge,int dropS, Strategy * s1, Strategy * s2, Strategy * s3){
strats =new Strategy * [3];
strats[0]=new Strategy(s1);
strats[1]=new Strategy(s2);
strats[2]=new Strategy(s3);
numbars=numb;
age=0;
zigset(time(NULL));
float a=RNOR;
a=a*5;
a=a+avgAge;
if(avgAge<0)
{
death=-1;
}
else
{
death=(int) a;
}
drop=dropS;
bar=0;
}
/* Precondition: 0 <= numb <= 256
Postcondition: An Agents Strategies has been initialized and
Description: Initializes an Agents Strategy
*/
Agent::Agent(int numb,int avgAge,int dropS){
strats =new Strategy*[3];
strats[0]=new Strategy(numb);
strats[1]=new Strategy(numb);
strats[2]=new Strategy(numb);
numbars=numb;
age=0;
zigset(time(NULL));
float a=RNOR;
a=a*10;
a=a+avgAge;
if(avgAge<0)
{
death=-1;
}
else
{
death=(int) a;
}
bar=0;
drop=dropS;
}
/* Precondition: bars have decided if they won the current round
Postcondition: Strategies score of agent has been updated appropriately and strategies have been replaced of necessary
Description: this function takes in an array of currents turn bar winners and the current STM, it then uses this information to update
strategy scores and if score drops below predefined threshold the strategy is replaced with another. It then returns needed variables
to the GUI
*/
Strategy ** Agent::tellWins(int winners[],int STM) {
int i;
double t;
double max=0;
double sum=0;
for (i=0;i<3;i++) {
t=strats[i]->updateScore(winners[strats[i]->getBar(STM)]);
if(t<drop)
{
delete strats[i];
strats[i]= new Strategy(numbars);
if(max<10)
{
max=10;
}
}
}
return strats;
}
/* Precondition: A turn has just begun
Postcondition: The Agent successfully decided where they are going for the current turn
Description: This function, using a random number generator, picks a strategy to utilize where he/she is going to bar for current turn
*/
int Agent::isGoingToBar(int STM)
{
int i;
double f = (double)rand() / RAND_MAX;
f*=(strats[0]->getScore()+strats[1]->getScore()+strats[2]->getScore());
for(i=0;i<3;i++)
{
if((f-=strats[i]->getScore())<=0)
{
bar=strats[i]->getBar(STM);
return strats[i]->getBar(STM);
}
}
age++;
return -1;
}
bool Agent::isDead()
{
if(death<0)
{
return false;
}
else if(age>=death)
{
return true;
}
return false;
}
//Agent Class Destructor
Agent::~Agent(){
delete strats[0];
delete strats[1];
delete strats[2];
}