-
Notifications
You must be signed in to change notification settings - Fork 0
/
84.cpp
156 lines (146 loc) · 3.89 KB
/
84.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
#include <iostream>
#include <random>
#include <vector>
#include <algorithm>
#include <iomanip>
#include "utils.h"
constexpr int limit = 10'000'000;
constexpr int die_sides = 4;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> rand1_16(1, 16);
std::uniform_int_distribution<int> die(1, die_sides);
// 00 01 02 03 04 05 06 07 08 09 10
// GO A1 CC1 A2 T1 R1 B1 CH1 B2 B3 JAIL
//39 H2 C1 11
//38 T2 U1 12
//37 H1 C2 13
//36 CH3 C3 14
//35 R4 R2 15
//34 G3 D1 16
//33 CC3 CC2 17
//32 G2 D2 18
//31 G1 D3 19
// G2J F3 U2 F2 F1 R3 E3 E2 CH2 E1 FP 20
// 30 29 28 27 26 25 24 23 22 21
enum Square {
GO, A1, CC1, A2, T1, R1, B1, CH1, B2, B3,
JAIL, C1, U1, C2, C3, R2, D1, CC2, D2, D3,
FP, E1, CH2, E2, E3, R3, F1, F2, U2, F3,
G2J, G1, G2, CC3, G3, R4, CH3, H1, T2, H2
};
const std::vector<std::string> names = {
"GO", "A1", "CC1", "A2", "T1", "R1", "B1", "CH1", "B2", "B3",
"JAIL", "C1", "U1", "C2", "C3", "R2", "D1", "CC2", "D2", "D3",
"FP", "E1", "CH2", "E2", "E3", "R3", "F1", "F2", "U2", "F3",
"G2J", "G1", "G2", "CC3", "G3", "R4", "CH3", "H1", "T2", "H2"
};
auto is_CC = [](Square s) {return (s == CC1) || (s == CC2) || (s == CC3);};
auto is_CH = [](Square s) {return (s == CH1) || (s == CH2) || (s == CH3);};
// Community Chest (2/16 cards):
// Advance to GO
// Go to JAIL
// Chance (10/16 cards):
// Advance to GO
// Go to JAIL
// Go to C1
// Go to E3
// Go to H2
// Go to R1
// Go to next R (railway company)
// Go to next R
// Go to next U (utility company)
// Go back 3 squares.
struct Board {
Square pos = GO;
std::vector<std::uint64_t> cnt;
bool pprev_doule = false, prev_double = false;
Board() {
cnt = std::vector<std::uint64_t>(40, 0);
cnt[0] = 1;
}
void rand_CC() {
int k = rand1_16(gen);
if (k == 1) {
pos = GO;
} else if (k == 2) {
pos = JAIL;
}
}
void rand_CH() {
int k = rand1_16(gen);
if (k == 1) {
pos = GO;
} else if (k == 2) {
pos = JAIL;
} else if (k == 3) {
pos = C1;
} else if (k == 4) {
pos = E3;
} else if (k == 5) {
pos = H2;
} else if (k == 6) {
pos = R1;
} else if (k == 7 || k == 8) {
if (pos == CH1) pos = R2;
else if (pos == CH2) pos = R3;
else pos = R1;
} else if (k == 9) {
if (pos == CH1) pos = U1;
else if (pos == CH2) pos = U2;
else pos = U1;
} else if (k == 10) {
if (pos == CH3) {
pos = CC3;
rand_CC();
}
else pos = static_cast<Square>(pos - 3);
}
}
void go() {
int roll1 = die(gen);
int roll2 = die(gen);
if (roll1 == roll2) {
if (pprev_doule && prev_double) {
pos = JAIL;
++cnt[pos];
pprev_doule = false;
prev_double = false;
return;
}
}
pos = static_cast<Square>((pos + roll1 + roll2) % 40);
if (pos == G2J) pos = JAIL;
else if (is_CC(pos)) rand_CC();
else if (is_CH(pos)) rand_CH();
++cnt[pos];
pprev_doule = prev_double;
prev_double = roll1 == roll2;
}
std::vector<double> get_stats() {
double total = std::accumulate(cnt.begin(), cnt.end(), 0);
std::vector<double> ans; ans.reserve(40);
// print_vector(cnt);
for (auto &c : cnt) ans.push_back(c / total);
return ans;
}
};
void print_stats(std::vector<double> &stats) {
std::vector<int> idx;
for (int i = 0; i < stats.size(); ++i) idx.push_back(i);
std::sort(idx.begin(), idx.end(), [&](auto i, auto j) {
return stats[i] < stats[j];
});
std::cout << std::fixed;
std::cout << std::setprecision(2);
for (int i = idx.size() - 1; i >= 0; --i) {
std::cout << names[idx[i]] << ": " << (stats[idx[i]] * 100) << "%" << " ";
}
std::cout << std::endl;
}
int main() {
Board board;
for (int i = 0; i < limit; ++i) board.go();
auto stats = board.get_stats();
print_stats(stats);
}