-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameState.cpp
executable file
·208 lines (183 loc) · 3.65 KB
/
GameState.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
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
#include <stdio.h>
//#include <sys/resource.h>
#include <time.h>
#include <math.h>
#include "GameState.h"
#include "Player.h"
namespace hearts {
//#define min(x, y) (((x)>(y))?(y):(x))
//#define max(x, y) (((x)<(y))?(y):(x))
//#define VERIFYHASH 0
//#define USEHASH 1
//#define RANDOMIZEMOVE
//#define RANDOMIZEMAXN
//Move *lastbest;
//mt_random Move::rand;
GameState::GameState()
{
Reset();
for (unsigned int x = 0; x < MAXPLAYERS; x++)
{ men[x] = 0; teams[x] = -1; }
moveList = 0;
game = 0;
//r.srand(time(0));
}
GameState::~GameState()
{
// Move *tmp = moveList;
delete moveList;
for (unsigned int x = 0; x < MAXPLAYERS; x++)
{ delete men[x]; men[x] = 0; }
}
void GameState::randomlySwitchPlayers()
{
for (int x = 0; x < (int)numPlayers; x++)
{
int swap = r.rand_long()%(numPlayers-x)+x;
Player *p = men[swap];
men[swap] = men[x];
men[x] = p;
}
}
Move *GameState::getRandomMove()
{
double cnt = 1;
Move *m = this->getMoves();
// printf("%d random moves available\n", m->length());
Move *ret = m;
m = m->next;
ret->next = 0;
while (m)
{
cnt++;
double ran = r.rand_double();
// printf("Comparing %1.2f to %1.2f\n", ran, 1.0/cnt);
if (ran < 1.0/cnt)
{
freeMove(ret);
ret = m;
m = m->next;
ret->next = 0;
}
else {
Move *tmp = m;
m = m->next;
tmp->next = 0;
freeMove(tmp);
}
}
return ret;
}
bool GameState::IsLegalMove(Move *m)
{
bool result = false;
Move *n = getAllMoves();
for (Move *t = n; t; t = t->next)
if (m->equals(t))
result = true;
freeMove(n);
return result;
}
Move *GameState::getNewMove()
{
if (moveList == 0)
{
moveList = allocateMoreMoves(100);
//printf("100 moves allocated\n");
}
Move *ret = moveList;
moveList = moveList->next;
ret->next = 0;
return ret;
}
void GameState::freeMove(Move *m)
{
if (m == 0)
return;
while (m->next)
{
Move *tmp = m->next;
m->next = moveList;
moveList = m;
m = tmp;
}
m->next = moveList;
moveList = m;
}
void GameState::copyMoveList(GameState *g)
{
// static int cnt = 0;
if (moveList == 0)
{
moveList = g->moveList;
g->moveList = 0;
}
else {
//printf("opps Movelist: %d\n", moveList->length());
}
// if (moveList && (((++cnt)%5000) == 0))
// printf("Movelist: %d\n", moveList->length());
}
void GameState::addPlayer(Player *p)
{
gameScore[numPlayers] = 0.0;
men[numPlayers] = p;
teams[numPlayers] = numPlayers;
p->setGameState(this);
numPlayers++;
}
void GameState::deletePlayers()
{
for (unsigned int x = 0; x < numPlayers; x++)
{
delete men[x];
men[x] = 0;
}
numPlayers = 0;
}
void GameState::setTeam(Player *p, int team)
{
setTeam(getPlayerNum(p), team);
}
void GameState::setTeam(int who, int team)
{
teams[who] = team;
}
int GameState::getTeam(int who)
{
return teams[who];
}
void GameState::Reset(int NEWSEED)
{
numPlayers = 0;
}
int GameState::getPlayerNum(Player *p) const
{
for (unsigned int x = 0; x < numPlayers; x++)
if (men[x] == p)
return x;
return -1;
}
Player *GameState::getPlayer(int which) const
{
if (which < (int)numPlayers)
return men[which];
return 0;
}
/*
void GameState::Print(int val) const
{ printf("Can't call virtual GS::Print\n"); exit(1); }
Player *GameState::getNextPlayer()
{ printf("Can't call virtual GS::getNPlayer\n"); exit(1); return 0; }
int GameState::getNextPlayerNum()
{ printf("Can't call virtual GS::getNPlayerNum\n"); exit(1); return 0; }
int GameState::getCurrPlayerNum()
{ printf("Can't call virtual GS::getCurrPlayerNum\n"); exit(1); return 0; }
void GameState::ApplyMove(Move *move)
{ movesSoFar++; }
void GameState::UndoMove(Move *move)
{ movesSoFar--; }
*/
bool GameState::Done() const
{ printf("Can't call virtual GS::Done\n"); exit(1); return 0; }
} // namespace hearts