-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisttest.cpp
118 lines (95 loc) · 2.9 KB
/
disttest.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
#include "lib/GameState.h"
#include "lib/KnowableState.h"
#include "lib/math.h"
#include "lib/PossibilityAnalyzer.h"
#include "lib/RandomStrategy.h"
#include "lib/MonteCarlo.h"
#include <iostream>
#include <fstream>
#include <sstream>
std::string basePath("/Users/jim/dev/github/deck/dot/d");
static std::string debugFileName(int playNumber) {
const std::string pad = playNumber<10 ? "0" : "";
return basePath + pad + std::to_string(playNumber) + ".dot";
}
const uint32_t kNumAlternates = 1000;
const bool parallel = false;
RandomGenerator rng;
int run()
{
StrategyPtr intuition(new RandomStrategy());
AnnotatorPtr annotator(0);
StrategyPtr monte(new MonteCarlo(intuition, kNumAlternates, parallel, annotator));
GameState state;
PossibilityAnalyzer* analyzer = 0;
const uint128_t kThresh = 10000;
uint128_t possibilities = 0;
while (!state.Done())
{
KnowableState knowableState(state);
analyzer = knowableState.Analyze();
possibilities = analyzer->Possibilities();
std::string poss = asDecimalString(possibilities);
if (possibilities < kThresh) {
printf("At play %d, possibilities, %s\n", state.PlayNumber(), poss.c_str());
break;
}
Card card = monte->choosePlay(knowableState, rng);
state.PlayCard(card);
}
Distribution analyzedDistribution;
{
KnowableState knowableState(state);
CardHands hands;
knowableState.PrepareHands(hands);
analyzer->ExpectedDistribution(analyzedDistribution, hands);
analyzedDistribution.DistributeRemainingToPlayer(state.CurrentPlayersHand(), state.CurrentPlayer(), possibilities);
}
Distribution distribution;
for (uint128_t possibility=0; possibility<possibilities; ++possibility) {
KnowableState knowableState(state);
CardHands hands;
knowableState.PrepareHands(hands);
analyzer->ActualizePossibility(possibility, hands);
distribution.CountOccurrences(hands);
}
if (analyzedDistribution == distribution)
{
// distribution.Print();
// float prob[52][4];
// distribution.AsProbabilities(prob);
// Distribution::PrintProbabilities(prob);
return 0;
}
else
{
KnowableState knowableState(state);
int p = state.PlayNumber();
analyzer->RenderDotToFile(debugFileName(p), knowableState.UnknownCardsForCurrentPlayer());
for (int player=0; player<4; player++) {
const char X = player == state.CurrentPlayer() ? '*' : ' ';
putchar(X);
state.PrintHand(player);
}
// printf("Analyzed distribution\n");
// analyzedDistribution.Print();
// printf("\nOccurences distribution\n");
// distribution.Print();
return 1;
}
}
int main(int argc, char** argv)
{
const int kRuns = argc==2 ? atoi(argv[1]) : 2;
assert(kRuns > 0);
assert(kRuns < 1000000);
for (int i=0; i<kRuns; i++) {
int result = run();
if (result)
return result;
else {
printf("Ok %d of %d\n", i, kRuns);
}
}
return 0;
}