-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
213 lines (173 loc) · 4.4 KB
/
Player.cpp
File metadata and controls
213 lines (173 loc) · 4.4 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
Player::Player(int num, int whichAI)
{
for (int i = 0; i < 5; i++)
{
games.push_back(0);
diff.push_back(0);
handSizes.push_back(0);
pointsPerHand.resize(25);
}
playerNumber = num;
switch (whichAI)
{
case 0:
theAI = new FireAI(num);
break;
case 1:
theAI = new TestAI(num);
break;
//... imagine there are 19 more rows here for yall's AIs
}
}
//if you desire to keep track of what cards other people played...
void Player::onOtherPlayerMove(int playerNumber, Card justPlayed, Color choosenCardColor)
{
theAI->onOtherPlayerMove(playerNumber, justPlayed, choosenCardColor);
}
void Player::onOtherPlayerDraw(int playerNumber)
{
theAI->onOtherPlayerDraw(playerNumber);
}
bool Player::validPlay(int index, Card justPlayed, Color choosenCardColor, bool justDrew, vector<int> scores, vector<int> cardAmountsByPlayer, vector<Card> cardsInHand)
{
//cout << index << " " << hand.size() << "\n";
if (index < -1 || index > hand.size() - 1)
return false;
if (getNewColor() == BLACK || getNewColor() == NONE)
{
return false;
}
if (index == -1)
{
return true;
}
Card c = hand[index];
if (choosenCardColor == c.getColor())
{
return true; //same color
}
else if (c.getColor() != BLACK && justPlayed.getNumber() == c.getNumber() && justPlayed.getColor() != BLACK)
{
return true; //same number
}
else if (c.getColor() == BLACK && c.getNumber() == 0) //wild
{
return true; //wild
}
else if (c.getColor() == BLACK && c.getNumber() == 1) //draw four
{
bool hasColor = false;
for (int j = 0; j < cardsInHand.size(); j++)
{
if (choosenCardColor == cardsInHand[j].getColor())
{
hasColor = true;
}
}
if (!hasColor) //can only play draw4 if doesn't have color.
{
return true;
}
}
return false;
}
//calls the function on player's AI.
int Player::makeMove(Card justPlayed, Color choosenCardColor, bool justDrew, vector<int> scores, vector<int> cardAmountsByPlayer,int direction) //the returned int refers to the index of which card is played in your hand.
{
int index = theAI->makeMove(justPlayed, choosenCardColor, justDrew, scores, cardAmountsByPlayer, hand, direction);
if (!validPlay(index, justPlayed, choosenCardColor, justDrew, scores, cardAmountsByPlayer, hand))
{
//cout << "bad play detected by AI; card not allowed to be played: print out on player.cpp line 83\n";
//while (true); //pause program execution!
return -1;
}
return index;
}
Color Player::getNewColor()
{
return theAI->getNewColor();
}
void Player::clearHand()
{
hand.clear();
}
void Player::addToHand(Card c)
{
hand.push_back(c);
}
void Player::removeCardFromHand(int indexOfCard)
{
hand.erase(hand.begin() + indexOfCard);
}
vector<Card> Player::getHandCopy()
{
return hand;
}
int Player::updateStats(vector<Player*> thePlayers)
{
for (int i = 0; i < 5; i++)
{
handSizes[thePlayers[i]->playerNumber] += hand.size();
}
int points = 0;
for (int i = 0; i < hand.size(); i++)
{
points += hand[i].getPoints();
}
for (int j = 0; j < 5; j++)
{
pointsPerHand[thePlayers[j]->playerNumber].push_back(points);
}
for (int i = 0; i < 5; i++)
{
games[thePlayers[i]->playerNumber] ++;
}
mostRecent = points;
return points;
}
void Player::printStats(vector<Player*> thePlayers)
{
cout << "Player: " << playerNumber << "\n";
cout << "AI: " << theAI->getName() << "\n";
cout << "handsize on game end, ";
for (int i = 0; i < 1; i++)
{
cout << handSizes[i] * 1.0f / games[i] << ",";
}
cout << "\n";
cout << "medium points per game, ";
for (int i = 0; i < 1; i++)
{
sort(pointsPerHand[i].begin(), pointsPerHand[i].end());
if (pointsPerHand[i].size() == 0)
{
cout << 0 << ",";
}
else
{
cout << pointsPerHand[i][pointsPerHand[i].size() / 2] << ",";
}
}
cout << "\n";
cout << "average points (lower is better), ";
for (int j = 0; j < 1; j++)
{
int points = 0;
for (int i = 0; i < pointsPerHand[j].size(); i++)
{
points += pointsPerHand[j][i];
}
cout << points * 1.0f / games[j] << ",";
}
cout << "\n";
cout << "avg difference between players (lower is better), ";
for (int i = 0; i < 1; i++)
{
cout << diff[i] * 1.0f / games[i] << ",";
}
cout << "\n";
}