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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
#include "game.h"
#include "hand.h"
void Game::Player::round_start(int w) {
// Reset contents.
hand.clear();
open.clear();
pond.clear();
tenpai_indexes.clear();
riichi = false;
score = 25000;
wind = w;
// Notify client of round start.
client->round_start();
}
PlayerState Game::Player::get_state() {
Tilegroups h;
h.push_back(hand);
for(Sets::iterator it = open.begin(); it != open.end(); it++) {
h.push_back(it->tiles);
}
PlayerState state = {h, pond, riichi, score, wind};
return state;
}
PlayerState Game::Player::get_state_filtered() {
Tilegroups h;
h.push_back(hand);
for(Sets::iterator it = open.begin(); it != open.end(); it++) {
h.push_back(it->tiles);
}
PlayerState state = {h, pond, riichi, score, wind};
return state;
}
Actions Game::Player::get_actions_draw() {
Actions possible_actions;
// Check if player can force a draw.
// TODO: Implementation. (Low priority)
// Check if player can tsumo.
if(can_tsumo()) {
possible_actions.push_back(Action(Action::Tsumo));
}
// Check if player can declare a concealed kan.
Targets targets = can_kan();
for(Targets::iterator it = targets.begin(); it != targets.end(); it++) {
possible_actions.push_back(Action(Action::Kan, Action::Index, *it + 2));
}
// Check if player can extend an open pon.
targets = can_kan_extend();
for(Targets::iterator it = targets.begin(); it != targets.end(); it++) {
possible_actions.push_back(Action(Action::Kan, Action::Group, *it));
}
if(!riichi) {
// Check if player can riichi.
if(can_riichi()) {
possible_actions.push_back(Action(Action::Riichi));
}
// List all tiles that can be discarded.
for(std::size_t i = 0; i < hand.size(); i++) {
// Skip tiles already used to call kan.
if(possible_actions.contains(Action(Action::Kan, Action::Index, i))) {
continue;
}
possible_actions.push_back(Action(Action::Discard, Action::Index, i));
}
} else {
if(tenpai_indexes) {
for(List<int>::iterator it = tenpai_indexes.begin(); it != tenpai_indexes.end(); it++) {
possible_actions.push_back(Action(Action::Discard, Action::Index, *it));
}
tenpai_indexes.clear();
} else {
// Only tile that can be discarded is the last drawn one.
possible_actions.push_back(Action(Action::Discard, Action::Index, hand.size() - 1));
}
}
return possible_actions;
}
Actions Game::Player::get_actions_discard(Tile tile, PlayerNum discarder) {
std::cout << "Fisk:" << std::endl;
Actions possible_actions;
if(!riichi) {
// Check if tile can be called for a chi. Enumerate all combinations.
if(discarder == 3) {
Targets targets = can_chi(tile);
if(!targets.empty()) {
for(Targets::iterator it = targets.begin(); it != targets.end(); it++) {
possible_actions.push_back(Action(Action::Chi, Action::Index, *it));
}
}
}
// Check if tile can be called for a pon.
int target = can_pon(tile);
if(target >= 0) {
possible_actions.push_back(Action(Action::Pon, Action::Index, target + 1));
// Check if tile can be called for a kan.
if(can_kan(tile, target)) {
std::cout << "Can kan!" << std::endl;
possible_actions.push_back(Action(Action::Kan, Action::Index, target + 2));
}
}
}
// Check if tile can be ron-ed.
if(can_ron(tile)) {
possible_actions.push_back(Action(Action::Ron));
}
// If any action is possible, add option to pass.
if(possible_actions) {
possible_actions.push_back(Action::Pass);
}
return possible_actions;
}
bool Game::Player::can_riichi() {
if(open) {
return false;
}
// Iterate over the hand, testing to remove each tile and see if it gives tenpai.
for(Tiles::iterator it = hand.begin(); it != hand.end(); it++) {
Tiles tiles = hand;
tiles.del(it - hand.begin());
tiles.sort();
if(Hand::tenpai(tiles)) {
tenpai_indexes.push_back(it - hand.begin());
}
}
return bool(tenpai_indexes);
}
Game::Player::Targets Game::Player::can_chi(Tile tile) {
Targets targets;
Tile::Set set = tile.get_set();
int num = tile.get_num();
// Check if tile actually can be chi-ed.
if(set == Tile::Honor) {
return targets;
}
bool have_above = false;
// Check if we have tile below.
Tiles::iterator it = std::find(hand.begin(), hand.end(), Tile(set, num - 1));
if(num > 1 && it != hand.end()) {
// Check if we also have tile below tile below.
Tiles::iterator it2 = std::find(hand.begin(), hand.end(), Tile(set, num - 2));
if(num > 2 && it2 != hand.end()) {
targets.push_back(it2 - hand.begin()); // (T-2 T-1 T)
}
// Check if we have tile above.
it2 = std::find(hand.begin(), hand.end(), Tile(set, num + 1));
if(num < 9 && it2 != hand.end()) {
targets.push_back(it - hand.begin()); // (T-1 T T+1)
have_above = true;
it = it2;
}
}
// Check if we have tile above.
if(have_above || (it = std::find(hand.begin(), hand.end(), Tile(set, num + 1))) != hand.end()) {
// Check if we have tile above tile above.
Tiles::iterator it2 = std::find(hand.begin(), hand.end(), Tile(set, num + 2));
if(num < 8 && it2 != hand.end()) {
targets.push_back(it - hand.begin()); // (T T+1 T+2)
}
}
return targets;
}
int Game::Player::can_pon(Tile tile) {
Tiles::iterator it = std::find(hand.begin(), hand.end(), tile);
if(it + 1 < hand.end() && it[1] == tile) {
return it - hand.begin();
}
return -1;
}
Game::Player::Targets Game::Player::can_kan() {
Targets targets;
for(Tiles::iterator it = hand.begin(); it != hand.end(); it++) {
Tiles::iterator it_s = it;
int i = 1;
do {
it_s = std::find(it_s + 1, hand.end(), *it);
} while(it_s != hand.end() && i++);
if(i >= 4) {
targets.push_back(it - hand.begin());
}
}
return targets;
}
Game::Player::Targets Game::Player::can_kan_extend() {
Targets targets;
for(Sets::iterator it = open.begin(); it != open.end(); it++) {
if(it->type == Set::Pon && hand.contains(it->tiles.front())) {
targets.push_back(it - open.begin() + 1);
}
}
return targets;
}
bool Game::Player::can_kan(Tile tile, int target) {
if(std::size_t(target + 2) < hand.size() && hand[target + 2] == tile) {
return true;
}
return false;
}
bool Game::Player::can_tsumo() {
Tiles tiles = hand;
tiles.sort();
return Hand::agari(tiles);
}
bool Game::Player::can_ron(Tile tile) {
Tiles tiles = hand;
tiles.push_back(tile);
tiles.sort();
return Hand::agari(tiles);
}
void Game::Player::draw(Tile tile) {
hand.push_back(tile);
}
void Game::Player::discard(int target) {
Tile tile = hand[target];
hand.erase(hand.begin() + target);
hand.sort();
pond.push_back(tile);
}
Tile Game::Player::last_discard() {
return pond.back();
}
Tile Game::Player::claim() {
Tile& t = pond.back();
t.invisible = true;
return t;
}
void Game::Player::declare_riichi() {
riichi = true;
}
void Game::Player::make_chi(Tile tile, int target) {
Tiles chi;
tile.rotated = true;
chi.push_back(tile);
Tile::Set set = tile.get_set();
int num = tile.get_num();
Tiles::iterator it = hand.begin();
switch(hand[target].get_num() - num) {
case -2:
it = std::find(it, hand.end(), Tile(set, num - 2));
chi.push_back(*it);
it = hand.erase(it);
case -1:
it = std::find(it, hand.end(), Tile(set, num - 1));
chi.push_back(*it);
it = hand.erase(it);
case 1:
if(chi.size() == 3) {
break;
}
it = std::find(it, hand.end(), Tile(set, num + 1));
chi.push_back(*it);
it = hand.erase(it);
if(chi.size() == 3) {
break;
}
it = std::find(it, hand.end(), Tile(set, num + 2));
chi.push_back(*it);
hand.erase(it);
}
open.push_back(Set(Set::Chi, chi, true));
}
void Game::Player::make_pon(Tile tile, int target, PlayerNum discarder) {
Tiles pon;
tile.rotated = true;
if(discarder == 3) {
pon.push_back(tile);
}
pon.push_back(hand[target]);
hand.del(target);
if(discarder == 2) {
pon.push_back(tile);
}
pon.push_back(hand[target]);
hand.del(target);
if(discarder == 1) {
pon.push_back(tile);
}
open.push_back(Set(Set::Pon, pon, true));
}
void Game::Player::make_kan(Tile tile, int target, PlayerNum discarder) {
Tiles kan;
tile.rotated = true;
if(discarder == 3) {
kan.push_back(tile);
}
kan.push_back(hand[target]);
hand.del(target);
if(discarder == 2) {
kan.push_back(tile);
}
kan.push_back(hand[target]);
hand.del(target);
kan.push_back(hand[target]);
hand.del(target);
if(discarder == 1) {
kan.push_back(tile);
}
open.push_back(Set(Set::Kan, kan, true));
}
|