forked from rjtobin/HanSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrec_player.cpp
More file actions
280 lines (240 loc) · 7.85 KB
/
rec_player.cpp
File metadata and controls
280 lines (240 loc) · 7.85 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
This file is part of HanSim (copyright GRWC Han Group 2014).
HanSim is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
HanSim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HanSim. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rec_player.hpp"
#include <iostream>
using namespace std;
//==============================================================================
// Initiates Player
//==============================================================================
void Player::init()
{
for(int p = 0; p <5 ; p++)
info[p]=8;
info[5]=0;
info[6]=0;
info[7]=0;
}
//==============================================================================
// Start-of-turn Computation
//==============================================================================
void Player::preturnUpdate()
{
}
//==============================================================================
// Play Command
//==============================================================================
void Player::act(int& action, int& card, int& hint_to, int& hint_type, int& hint_value)
{
//-------------PLAY (if told to play and no one has played)-----------------
if(info[mPlayerIndex] < 4 && info[5] < 1){
action = ACT_PLAY;
card = info[mPlayerIndex];
return;
}
//----------PLAY (if told to play and not yet 2 plays and strikes left)-----
if(info[mPlayerIndex] < 4 && info[5] < 2 && mGame->getNumStrikes()>0){
action = ACT_PLAY;
card = info[mPlayerIndex];
return;
}
//------------HINT (if a play has not yet been made)-------------------------
if (mGame->getTime() > 0){
int sum = 0;
for(int p = 0; p < 5; p++){
if(mPlayerIndex != p)
{
sum += getNumber(p);
}
}
sum %= 8;
action = ACT_HINT;
hint_to = (sum%4+mPlayerIndex+1)%5;
if (sum < 4)
hint_type = RANK;
else
hint_type = SUIT;
//no info passed through hint_value
if (hint_type == RANK)
hint_value = mGame->getCard(hint_to,0).rank;
if (hint_type == SUIT)
hint_value = mGame->getCard(hint_to,0).suit;
return;
}
//-------------Discard (if told to discard)-------------------------------
if(3 < info[mPlayerIndex] && info[mPlayerIndex] < 8){
action = ACT_DISCARD;
card = info[mPlayerIndex]-4;
return;
}
//--------DISCARD RANDOM (if nothing better)----------------------------
action = ACT_DISCARD;
card = 3;
return;
}
//==============================================================================
// DeclarationUpdate
//==============================================================================
void Player::declarationUpdate()
{
int turnNumber = mGame->getTurnNumber();
Turn turn = mGame->getTurnHistory(turnNumber);
int p_turn = turn.player;
int action = turn.action;
int card = turn.card;
int hint_to = turn.hint_to;
int hint_type = turn.hint_type;
int hint_value = turn.hint_value;
// This all the information we use, although players also have access to
// the get methods in hansim (as well as previous turn history)
if (action == OUT_HINT){
int sumPassed = 0;
sumPassed = ((25+hint_to - p_turn)%5)-1;
if (hint_type == SUIT)
sumPassed += 4;
int obsSum = 0;
for (int p=0; p<5; p++){
if (p != p_turn && p != mPlayerIndex){
info[p]=getNumber(p);
obsSum += getNumber(p);
}
}
obsSum %= 8;
info[p_turn]=8;
if (p_turn != mPlayerIndex)
info[mPlayerIndex]=(64 + sumPassed - obsSum)%8;
info[5]=0;
info[6]=0;
}
}
//==============================================================================
// ResolutionUpdate
//==============================================================================
void Player::resolutionUpdate()
{
int turnNumber = mGame->getTurnNumber();
Turn turn = mGame->getTurnHistory(turnNumber);
int p_turn = turn.player;
int action = turn.action;
int card = turn.card;
int hint_to = turn.hint_to;
int hint_type = turn.hint_type;
int hint_value = turn.hint_value;
int outcome = turn.outcome;
int hint_info[4];
for (int i = 0; i<4; i++){
hint_info[i] = turn.hint_info[i];
}
// This all the information we use, although players also have access to
// the get methods in hansim (as well as previous turn history)
if (outcome == OUT_PLAY ){
info[5]+= 1;
}
if (outcome == OUT_DISCARD || outcome == OUT_STRIKE){
info[6]+= 1;
info[7]+= 1;
}
}
//==============================================================================
// Prints the State
//==============================================================================
void Player::printState(ostream& out)
{
out << ' ';
for(int i=0; i<8; i++)
{
cerr << info[i] << " ";
}
}
//==============================================================================
// Auxiliary functions
//==============================================================================
int Player::getNumber(int p)
{
// index stores the hint directed towards player p, initialized to 9
// to indicate we have not decided on the hint yet
int index = 9;
int lowest_playable_number = 26;
for(int c = 0; c<4; c++)
{
int rank = mGame->getCard(p,c).rank;
int suit = mGame->getCard(p,c).suit;
int number = (5*rank)+suit;
if(mGame->getBoardPos(suit) == rank-1)
{
if (number < lowest_playable_number)
{
lowest_playable_number = number;
index = c;
}
if (mGame->getCard(p,c).rank == 4)
{
lowest_playable_number = -1;
index = c;
}
}
}
if (index < 4)
{
return index;
}
int highest_dead_number = -1;
for(int c = 0; c<4; c++)
{
int rank = mGame->getCard(p,c).rank;
int suit = mGame->getCard(p,c).suit;
int number = (5*rank)+suit;
if(mGame->getBoardPos(suit) > rank-1)
{
if (number > highest_dead_number)
{
highest_dead_number = number;
index = c;
}
}
}
if (index < 4)
{
index += 4;
return (index);
}
int highest_noncrit_number = -1;
for(int c = 0; c<4; c++)
{
int rank = mGame->getCard(p,c).rank;
int suit = mGame->getCard(p,c).suit;
if(rank < 4)
{
int danger = 0;
for(int i=0; i<mGame->getDiscardSize(); i++)
{
Card tmp = mGame->getDiscard(i);
if (tmp.suit == suit && tmp.rank == rank)
{
danger = 1;
}
}
if (danger == 0 && 5*rank+suit > highest_noncrit_number)
{
highest_noncrit_number = 5*rank+suit;
index = c;
}
}
}
if (index < 4)
{
index += 4;
return index;
}
return 7;
}