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
|
#include "game.h"
#include <boost/bind.hpp>
#include <iostream>
#include <algorithm>
#include <map>
#ifdef DEBUG
#include <ctime>
#endif
Game::p Game::create(Client::p player_1, Client::p player_2, Client::p player_3, Client::p player_4) {
Game::p p(new Game(player_1, player_2, player_3, player_4));
p->start();
return p;
}
Game::~Game() {
std::cout << "Game destroyed." << std::endl;
}
Game::Game(Client::p player_1, Client::p player_2, Client::p player_3, Client::p player_4) {
players[0].client = player_1;
players[1].client = player_2;
players[2].client = player_3;
players[3].client = player_4;
}
void Game::handle_ready() {
if(--awaiting_players) {
return;
}
std::cout << "All ready!" << std::endl;
round_start();
}
void Game::start() {
std::cout << "Started a game with "
<< players[0].client->nick() << ", "
<< players[1].client->nick() << ", "
<< players[2].client->nick() << " and "
<< players[3].client->nick() << "." << std::endl;
awaiting_players = 4;
std::vector<std::string> pl;
pl.push_back(players[0].client->nick());
pl.push_back(players[1].client->nick());
pl.push_back(players[2].client->nick());
pl.push_back(players[3].client->nick());
players[0].client->game_start(boost::bind(&Game::handle_ready, shared_from_this()), pl);
pl.erase(pl.begin());
pl.push_back(players[0].client->nick());
players[1].client->game_start(boost::bind(&Game::handle_ready, shared_from_this()), pl);
pl.erase(pl.begin());
pl.push_back(players[1].client->nick());
players[2].client->game_start(boost::bind(&Game::handle_ready, shared_from_this()), pl);
pl.erase(pl.begin());
pl.push_back(players[2].client->nick());
players[3].client->game_start(boost::bind(&Game::handle_ready, shared_from_this()), pl);
}
void Game::round_start() {
std::cout << "Starting a new round." << std::endl;
// Build a new wall.
wall.build();
// Clear previous dora and draw a new.
dora.clear();
dora.push_back(wall.take_one());
kan_dora_pending = false;
// Notify players of round start.
// TODO: Tell them where wall is broken.
players[0].round_start();
players[1].round_start();
players[2].round_start();
players[3].round_start();
// Draw hands.
for(int i = 0; i < 13; i++) {
players[0].draw(wall.take_one());
players[1].draw(wall.take_one());
players[2].draw(wall.take_one());
players[3].draw(wall.take_one());
}
// Sort hands.
players[0].hand.sort();
players[1].hand.sort();
players[2].hand.sort();
players[3].hand.sort();
// Give east an extra tile.
players[0].draw(wall.take_one());
current_player = 0;
round_update_draw();
}
void Game::round_update_draw() {
// Get possible actions for current player.
Actions possible_actions = players[current_player].get_actions_draw();
// Construct and send state to each client.
PlayerNum player = 0;
do {
Player::State state[4];
state[0] = players[player].get_state();
state[1] = players[player + 1].get_state_filtered();
state[2] = players[player + 2].get_state_filtered();
state[3] = players[player + 3].get_state_filtered();
Actions a;
if(player == current_player) {
a = possible_actions;
}
players[player].client->round_state(state[0], state[1], state[2], state[3], dora, a);
} while(++player);
// Await action from client.
players[current_player].client->get_action(boost::bind(&Game::handle_action_draw, shared_from_this(), _1), possible_actions);
}
void Game::round_update_discard() {
Tile discarded_tile = players[current_player].last_discard();
std::map<int, Actions> possible_actions;
// Construct and send state to each client.
PlayerNum player = 0;
do {
Player::State state[4];
state[0] = players[player].get_state();
state[1] = players[player + 1].get_state_filtered();
state[2] = players[player + 2].get_state_filtered();
state[3] = players[player + 3].get_state_filtered();
Actions a;
if(player != current_player) {
if(a = players[player].get_actions_discard(discarded_tile, current_player - player)) {
possible_actions[player] = a;
}
}
players[player].client->round_state(state[0], state[1], state[2], state[3], dora, a);
} while(++player);
preceding_action = Action::Pass;
if(possible_actions.empty()) {
awaiting_players = 1;
handle_action_discard(Action::Pass, 0);
} else {
awaiting_players = possible_actions.size();
for(std::map<int, Actions>::iterator it = possible_actions.begin(); it != possible_actions.end(); it++) {
players[it->first].client->get_action(boost::bind(&Game::handle_action_discard, shared_from_this(), _1, it->first), it->second);
}
}
}
void Game::handle_action_draw(Action action) {
switch(action.type) {
case Action::Discard: {
players[current_player].discard(action.target_offset);
if(kan_dora_pending) {
kan_dora_pending = false;
dora.push_back(wall.take_one());
}
round_update_discard();
} break;
case Action::Tsumo: {
// Score calculation or something.
round_end();
} break;
case Action::Riichi:
case Action::Kan:
case Action::Draw:
// Not implemented yet.
// Will never occur on draw:
case Action::Pass:
case Action::Chi:
case Action::Pon:
case Action::Ron:
break;
}
}
void Game::handle_action_discard(Action action, int player) {
switch(preceding_action.type) {
case Action::Pass:
case Action::Chi:
if(action.type == Action::Chi) {
preceding_action = action;
preceding_action_owner = player;
break;
}
case Action::Kan:
if(action.type == Action::Kan) {
preceding_action = action;
preceding_action_owner = player;
break;
}
case Action::Pon:
if(action.type == Action::Pon) {
preceding_action = action;
preceding_action_owner = player;
break;
}
case Action::Ron:
if(action.type == Action::Ron) {
// TODO: Handle simultanous ron.
preceding_action = action;
preceding_action_owner = player;
break;
}
break;
// Will never occur on discard:
case Action::Discard:
case Action::Riichi:
case Action::Tsumo:
case Action::Draw:
break;
}
if(--awaiting_players) {
return;
}
switch(preceding_action.type) {
case Action::Pass: {
// Tile not claimed, next player draws.
// Check if the wall has run out.
if(wall.remaining() <= 14) {
round_end();
break;
}
players[++current_player].draw(wall.take_one());
round_update_draw();
} break;
case Action::Chi: {
players[preceding_action_owner].make_chi(players[current_player].claim(), preceding_action.target_offset);
current_player = preceding_action_owner;
round_update_draw();
} break;
case Action::Pon: {
players[preceding_action_owner].make_pon(players[current_player].claim(), preceding_action.target_offset - 1, current_player - preceding_action_owner);
current_player = preceding_action_owner;
round_update_draw();
} break;
case Action::Kan: {
players[preceding_action_owner].make_kan(players[current_player].claim(), preceding_action.target_offset - 2, current_player - preceding_action_owner);
current_player = preceding_action_owner;
kan_dora_pending = true;
players[current_player].draw(wall.take_one());
round_update_draw();
} break;
case Action::Ron: {
// Score calculation or something.
round_end();
} break;
// Will never occur on discard:
case Action::Discard:
case Action::Riichi:
case Action::Tsumo:
case Action::Draw:
break;
}
}
void Game::round_end() {
awaiting_players = 4;
players[0].client->round_end(boost::bind(&Game::handle_ready, shared_from_this()));
players[1].client->round_end(boost::bind(&Game::handle_ready, shared_from_this()));
players[2].client->round_end(boost::bind(&Game::handle_ready, shared_from_this()));
players[3].client->round_end(boost::bind(&Game::handle_ready, shared_from_this()));
// Flere runder? round_start()
// Ferdig? game_end()
round_start();
}
|