forked from nathansttt/hearts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
States.cpp
executable file
·108 lines (96 loc) · 1.46 KB
/
States.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
#include "States.h"
namespace hearts {
//#define RANDOMIZEMOVE
Move::~Move()
{
while (next)
{
Move *nn = next;
next = nn->next;
nn->next = 0;
delete nn;
}
}
void Move::append(Move *mm)
{
if (mm == 0)
return;
if (next == 0)
next = mm;
else
next->append(mm);
}
void Move::insert(Move *m)
{
if (next == 0)
{
next = m;
return;
}
else if (m->dist > next->dist)
{
m->next = this->next;
this->next = m;
return;
}
#ifdef RANDOMIZEMOVE
else if (m->dist == next->dist)
{
int cnt = 1;
Move *where = this;
while ((where->next != 0) && (where->next->dist == m->dist))
{
double f = random();//rand.Random();
cnt++;
//printf("TIE! %1.2f %1.2f\n", f, ((double)1/cnt));
if (f < ((double)1/cnt))
{
m->next = where->next;
where->next = m;
return;
}
where = where->next;
}
where->insert(m);
}
#endif
else
next->insert(m);
}
HashState::HashState()
{
ret = 0; as = 0; ghs = 0;
}
void HashState::Print(int val) const
{
if (ret)
{
printf("Return value:\n");
ret->Print(0);
}
if (as)
{
printf("Algorithm State:\n");
as->Print();
}
if (ghs)
{
printf("Game Hash State:\n");
ghs->Print();
}
}
unsigned long HashState::hash_key()
{
return ghs->hash_key()^as->hash_key();
}
bool HashState::equals(State *val)
{
HashState *hs = (HashState*)val;
if ((as == 0) || (ghs == 0))
return false;
if (as->equals(hs->as) &&
ghs->equals(hs->ghs))
return true;
return false;
}
} // namespace hearts