-
Notifications
You must be signed in to change notification settings - Fork 5
/
simulator.cpp
208 lines (195 loc) · 5.6 KB
/
simulator.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 <assert.h>
#include <algorithm>
using namespace std;
#include "simulator.h"
//#undef assert
//#define assert(expr) do { \
// if (!(expr)) { \
// printf("assert(%s) failed: %s %d\n", #expr, __FILE__, __LINE__); \
// exit(1); \
// } \
// } while(0)
#define NEXT_CARD (this->wall[this->wall_index++])
const int Simulator::priority[12] = { 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 3 };
void Simulator::run()
{
for (int i = 0; i < NUM_PLAYER; i++)
this->game[i].reset();
for (tile t = 1; t < NUM_SYMBOL; t++) {
for (int i = 0; i < 4; i++)
this->wall[(t - 1) * 4 + i] = t;
}
for (tile t = 0; t < 8; t++)
this->wall[(NUM_SYMBOL - 1) * 4 + t] = NUM_SYMBOL + t;
random_shuffle(this->wall, this->wall + NUM_TILE);
this->turn = 0;
Message msg(ACT_QUAN);
msg.dealer = rand() % NUM_PLAYER;
this->broadcast(msg);
for (int i = 0; i < NUM_PLAYER; i++)
assert(this->response[this->turn][i].action == ACT_PASS);
this->turn++;
int flower_count[NUM_PLAYER] = { 0 };
tile card[NUM_PLAYER][13];
this->wall_index = 0;
for (int i = 0; i < NUM_PLAYER; i++) {
for (int j = 0; j < 13; j++) {
tile t;
for (t = NEXT_CARD; IS_FLOWER(t); t = NEXT_CARD)
flower_count[i]++;
card[i][j] = t;
// we do not care which flower card is received
}
}
msg.action = ACT_RECEIVE;
memcpy(msg.flower_count, flower_count, sizeof(flower_count));
for (int i = 0; i < NUM_PLAYER; i++) {
msg.player = i;
memcpy(msg.card, card[i], sizeof(card[i]));
this->game[i].play(msg);
this->history[this->turn][i] = msg;
this->response[this->turn][i] = this->player[i]->play(this->game[i]);
assert(this->response[this->turn][i].action == ACT_PASS);
}
this->turn++;
this->current_player = 0; // start from east, same as botzone implementation
while (this->wall_index < NUM_TILE) {
msg.player = this->current_player;
msg.card[0] = NEXT_CARD;
if (IS_FLOWER(msg.card[0]))
msg.action = ACT_BU_HUA;
else
msg.action = ACT_DRAW;
this->broadcast(msg);
if (IS_FLOWER(msg.card[0])) {
this->turn++;
continue;
}
// Play
for (int i = 0; i < NUM_PLAYER; i++) {
if (i == this->current_player) {
assert(this->response[this->turn][i].action == ACT_PLAY ||
this->response[this->turn][i].action == ACT_GANG ||
this->response[this->turn][i].action == ACT_BU_GANG ||
this->response[this->turn][i].action == ACT_HU);
}
else
assert(this->response[this->turn][i].action == ACT_PASS);
}
if (this->response[this->turn][this->current_player].action == ACT_HU) {
this->turn++;
return;
}
msg = this->response[this->turn++][this->current_player];
msg.player = this->current_player;
this->broadcast(msg, true);
int highest;
do { // check response
highest = 0;
for (int i = 0; i < NUM_PLAYER; i++) {
assert(this->response[this->turn][i].action == ACT_PASS ||
priority[this->response[this->turn][i].action]);
if (priority[this->response[this->turn][highest].action] <
priority[this->response[this->turn][i].action])
highest = i;
}
if (this->response[this->turn][highest].action == ACT_PASS) {
this->turn++;
break;
}
if (this->response[this->turn][highest].action == ACT_HU) {
this->turn++;
return;
}
msg = this->response[this->turn++][highest];
msg.player = this->current_player = highest;
this->broadcast(msg);
} while (true);
this->current_player = (this->current_player + 1) % 4;
}
}
void Simulator::seek(int turn)
{
assert(turn <= this->turn);
for (int i = 0; i < NUM_PLAYER; i++) {
this->game[i].reset();
for (int j = 0; j < turn; j++)
this->game[i].play(this->history[j][i]);
}
}
void Simulator::save(FILE *fout) const
{
fwrite(&this->turn, sizeof(int), 1, fout);
fwrite(this->history, sizeof(Message), this->turn * NUM_PLAYER, fout);
fwrite(this->response, sizeof(Message), this->turn * NUM_PLAYER, fout);
}
void Simulator::load(FILE *fin)
{
fread(&this->turn, sizeof(int), 1, fin);
fread(this->history, sizeof(Message), this->turn * NUM_PLAYER, fin);
fread(this->response, sizeof(Message), this->turn * NUM_PLAYER, fin);
}
void Simulator::broadcast(const Message &msg, bool is_an_gang)
{
static int last_turn;
assert(msg.action == ACT_QUAN || last_turn == 0 ||
this->turn == last_turn + 1);
last_turn = this->turn;
Message player_msg = msg;
switch (msg.action) {
case ACT_QUAN:
for (int i = 0; i < NUM_PLAYER; i++) {
player_msg.player = i;
this->game[i].play(player_msg);
this->history[this->turn][i] = player_msg;
}
break;
case ACT_DRAW:
for (int i = 0; i < NUM_PLAYER; i++) {
if (i == msg.player)
player_msg.action = ACT_DRAW;
else
player_msg.action = ACT_OTHER_DRAW;
this->game[i].play(player_msg);
this->history[this->turn][i] = player_msg;
}
break;
case ACT_GANG:
if (is_an_gang) { // An Gang
for (int i = 0; i < NUM_PLAYER; i++) {
if (i == msg.player)
player_msg.card[0] = msg.card[0];
else
player_msg.card[0] = 0;
this->game[i].play(player_msg);
this->history[this->turn][i] = player_msg;
}
break;
}
// Ming Gang fall through
default:
for (int i = 0; i < NUM_PLAYER; i++) {
this->game[i].play(msg);
this->history[this->turn][i] = msg;
}
break;
}
tile last_card;
if (msg.action == ACT_PENG || msg.action == ACT_CHI)
last_card = msg.card[1];
else
last_card = msg.card[0];
for (int i = 0; i < NUM_PLAYER; i++) {
Message response = this->player[i]->play(this->game[i]);
switch (response.action) {
case ACT_PENG:
response.card[1] = response.card[0];
response.card[0] = last_card;
break;
case ACT_GANG:
if (!response.card[0]) // Ming Gang
response.card[0] = last_card;
}
this->response[this->turn][i] = response;
}
}