summaryrefslogtreecommitdiff
path: root/server/standard.cpp
blob: 4b54c375c05ebbc6a93c5efa80d3a3512315d538 (plain)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "standard.h"

#include <algorithm>

#ifdef DEBUG
#include <ctime>
#include <iostream>
#endif

using namespace RuleSet;
bool MyDataSortPredicate(const Tile& d1, const Tile& d2) {
  return d1.type < d2.type;
}

void Standard::round_start() {
	
	// Sett opp runden sin state
	//game_state = make_shared<State>();
	
	// Simulates drawing 4, 4 ,4 for each player
	for (int i = 0; i < 3; i++) {
		for (int player_num = 0; player_num < 4; player_num++) {
			for (int y = 0; y < 4; y++) {
				game_state.players[player_num].hand.push_back(wall.take_one());
			}
		}
	}
	
	// Simulates the second part of drawing with only 1 tile
	for (int player_num = 0; player_num < 4; player_num++) {
		game_state.players[player_num].hand.push_back(wall.take_one());
	}
	
	std::sort(game_state.players[0].hand.begin(),game_state.players[0].hand.end(), MyDataSortPredicate);
	std::sort(game_state.players[1].hand.begin(),game_state.players[1].hand.end(), MyDataSortPredicate);
	std::sort(game_state.players[2].hand.begin(),game_state.players[2].hand.end(), MyDataSortPredicate);
	std::sort(game_state.players[3].hand.begin(),game_state.players[3].hand.end(), MyDataSortPredicate);

	
	most_value_action.type  = Action::Pass;
	current_player          = 3;
	num_player_actions      = 0;
	draw_phase              = true;
	
}


State& Standard::round_update() {
	char smart = 0;
	// We're in the draw_phase whenever a player draws a tile from the wall
	if(draw_phase) {
		// If the wall is empty (Contains only 14 tiles) when we enter draw phase the round is over.

		// Since we've entered the draw-phase it's the next players turn
		if (current_player == 3) {
			current_player = 0;
		} else {
			current_player++;	
		}
		
		#ifdef DEBUG
		
		time_t current_time = std::time(0);
		std::cout << std::ctime(&current_time) << " - Waiting for action from player: " << current_player << std::endl;
		
		#endif
	
		// Let's take a tile
		Tile from_wall = wall.take_one();
		
		// We then add the tile to the current players hand
		game_state.players[current_player].hand.push_back(from_wall);
		
		// Need to sort again.
		std::sort(game_state.players[current_player].hand.begin(),game_state.players[current_player].hand.end(), MyDataSortPredicate);
		
		// Construct the discard action that the player can do.
		Action discard;
		discard.type = Action::Discard;
		discard.player = current_player;
		
		
		game_state.possible_actions.push_back(discard);
		
		//num_player_actions++;
		
		// Enter the discard phase next loop;
		draw_phase = false;
		smart = smart | (1 << current_player);
	} else {
		//We chwck to see if player can chi from last discard

		
		int temp_next_player;
		if (current_player == 3) {
			temp_next_player = 0;
		} else {
			temp_next_player = current_player + 1;	
		}
		
		Tiles* pond = &game_state.players[current_player].pond;
		Tile temp_tile = pond->back();
		Tiles::iterator it;
		Tile* tile_2u = NULL;
		Tile* tile_1u = NULL;
		Tile* tile_1o = NULL;
		Tile* tile_2o = NULL;
		unsigned int count = 0, tile_2u_id,tile_1u_id,tile_1o_id,tile_2o_id;
		Tile::Type check_tile;
		for(it = game_state.players[temp_next_player].hand.begin(); it != game_state.players[temp_next_player].hand.end(); ++it) {
			#ifdef DEBUG
			Tile debug = *it;
			#endif
			
			check_tile = Tile::Type(temp_tile.type - 2);
			if(it->type == check_tile) {
				tile_2u = &(*it);
				tile_2u_id = count;
			}
			check_tile = Tile::Type(temp_tile.type - 1);
			if(it->type == check_tile) {
				tile_1u = &(*it);
				tile_1u_id = count;
			}
			check_tile = Tile::Type(temp_tile.type + 1);
			if(it->type == check_tile) {
				tile_1o = &(*it);
				tile_1o_id = count;
			}
			check_tile = Tile::Type(temp_tile.type + 2);
			if(it->type == check_tile) {
				tile_2o = &(*it);
				tile_2o_id = count;
			}
			count++;
		}
		
		bool chi;
		if(tile_2u && tile_1u) {
			Action temp_action;
			chi = false;
			//Make sure we have a chi within the same series.
			if(tile_2u->type <= Tile::Man_7 && tile_2u->type >= Tile::Man_1) {
				chi = true;
			} else if(tile_2u->type <= Tile::Pin_7 && tile_2u->type >= Tile::Pin_1) {
				chi = true;
			} else if(tile_2u->type <= Tile::Sou_7 && tile_2u->type >= Tile::Sou_1) {
				chi = true;
			}
			if(chi) {
				temp_action.player  = temp_next_player;
				temp_action.target.push_back(tile_2u_id);
				temp_action.target.push_back(tile_1u_id);
				temp_action.type    = Action::Chi;
				game_state.possible_actions.push_back(temp_action);
				#ifdef DEBUG
	
				time_t current_time = std::time(0);
				std::cout << std::ctime(&current_time) << " Player: " << temp_action.player << " Can do action: " << temp_action.type << " On target: " << temp_action.target[0] << std::endl;

				#endif
			}
		}
		
		if(tile_1u && tile_1o) {
			Action temp_action;
			chi = false;
			//Make sure we have a chi within the same series.
			if(tile_1u->type <= Tile::Man_7 && tile_1u->type >= Tile::Man_1) {
				chi = true;
			} else if(tile_1u->type <= Tile::Pin_7 && tile_1u->type >= Tile::Pin_1) {
				chi = true;
			} else if(tile_1u->type <= Tile::Sou_7 && tile_1u->type >= Tile::Sou_1) {
				chi = true;
			}
			if(chi) {
				temp_action.player  = temp_next_player;
				temp_action.target.push_back(tile_1u_id);
				temp_action.target.push_back(tile_1o_id);
				temp_action.type    = Action::Chi;
				game_state.possible_actions.push_back(temp_action);
				
				#ifdef DEBUG
	
				time_t current_time = std::time(0);
				std::cout << std::ctime(&current_time) << " Player: " << temp_action.player << " Can do action: " << temp_action.type << " On target: " << temp_action.target[0] << std::endl;

				#endif
			}
		}
		
		if(tile_1o && tile_2o) {
			Action temp_action;
			chi = false;
			//Make sure we have a chi within the same series.
			if(temp_tile.type <= Tile::Man_7 && temp_tile.type >= Tile::Man_1) {
				chi = true;
			} else if(temp_tile.type <= Tile::Pin_7 && temp_tile.type >= Tile::Pin_1) {
				chi = true;
			} else if(temp_tile.type <= Tile::Sou_7 && temp_tile.type >= Tile::Sou_1) {
				chi = true;
			}
			if(chi) {
				temp_action.player  = temp_next_player;
				temp_action.target.push_back(tile_1o_id);
				temp_action.target.push_back(tile_2o_id);
				temp_action.type    = Action::Chi;
				game_state.possible_actions.push_back(temp_action);
				
				#ifdef DEBUG
	
				time_t current_time = std::time(0);
				std::cout << std::ctime(&current_time) << " Player: " << temp_action.player << " Can do action: " << temp_action.type << " On target: " << temp_action.target[0] << std::endl;

				#endif
			}
		}
		
		//Go back into draw_phase0
		draw_phase = true;
		// Not implemented yet

		for(Actions::iterator it = game_state.possible_actions.begin(); it != game_state.possible_actions.end(); ++it) {
		smart = smart | (0x0001 << it->player);
		}
		
		if(smart & 1) {
			Action pass;
			pass.type = Action::Pass;
			pass.player = 0;
			game_state.possible_actions.push_back(pass);
		}
		if(smart & 2) {
			Action pass;
			pass.type = Action::Pass;
			pass.player = 1;
			game_state.possible_actions.push_back(pass);
		}
		if(smart & 4) {
			Action pass;
			pass.type = Action::Pass;
			pass.player = 2;
			game_state.possible_actions.push_back(pass);
		}
		if(smart & 8) {
			Action pass;
			pass.type = Action::Pass;
			pass.player = 3;
			game_state.possible_actions.push_back(pass);
		}
	}
	
	for(Actions::iterator it = game_state.possible_actions.begin(); it != game_state.possible_actions.end(); ++it) {
		smart = smart | (1 << it->player);
	}
	
	if(smart & 1) {
		num_player_actions++;
	}
	if(smart & 2) {
		num_player_actions++;
	}
	if(smart & 4) {
		num_player_actions++;
	}
	if(smart & 8) {
		num_player_actions++;
	}
	
	if(num_player_actions == 0) {
		draw_phase = true;
	}
	
	return game_state;
}

bool Standard::round_action(Action action) {
	#ifdef DEBUG
	
	time_t current_time = std::time(0);
	std::cout << std::ctime(&current_time) << " Player: " << action.player << " Did action: " << action.type << " On target: " << action.target[0] << std::endl;

	#endif	
	
	// Lots of actions to test if the player doing the action is allowed to do it
	
	// Check if we're actually waiting for actions.
	if (game_state.possible_actions.empty()) {
		return false;
	}

	// Check if the player is allowed to do this action
	bool found_action = false;

	for(Actions::iterator it = game_state.possible_actions.begin(); it != game_state.possible_actions.end(); ++it) {
		if(it->player == action.player && it->type == action.type) {
			found_action = true;
		}
	}
	
	if(!found_action) {
		return false;
	}
	
	switch ( action.type ) {
		
		case Action::Pass: {
		
		} break;
		
		case Action::Discard: {
			Tile discarded_tile = game_state.players[action.player].hand[action.target[0]];
			game_state.players[action.player].pond.push_back(discarded_tile);
			game_state.players[action.player].hand.erase(game_state.players[action.player].hand.begin() + action.target[0]);
			
		} break;
		
		case Action::Riichi: {
			
		} break;
		
		case Action::Chi: {
			if(most_value_action.type != Action::Pon && most_value_action.type != Action::Kan && most_value_action.type != Action::Ron) {
				most_value_action = action;
			}
		} break;
	
		case Action::Pon: {
			if(most_value_action.type != Action::Ron) {
				most_value_action = action;
			}
		} break;
		
		case Action::Kan: {
			
		} break;
		
		case Action::Ron: {
			
		} break;
		
		case Action::Tsumo: {
			
		} break;
	
		case Action::Draw: {
			
		} break;
	
		default:
			break;
	}
		
	
	num_player_actions--;
	
	// Remove all the actions that this player could do since he now did one.
	
	std::vector<int> positions;
	int position = 0;
	for(Actions::iterator it = game_state.possible_actions.begin(); it != game_state.possible_actions.end(); ++it) {
		if (it->player == action.player) {
			positions.push_back(position);
		}
		position++;
	}
	if (!positions.empty()) {
		int found = 0;
		for(std::vector<int>::iterator it = positions.begin(); it != positions.end(); ++it) {
			game_state.possible_actions.erase(game_state.possible_actions.begin() + (*it - found));
			found++;
		}
	}
	
	//When everyone has done their action we empty the list (just to be sure) and then do a round_update()
	if(num_player_actions == 0) {
		game_state.possible_actions.empty();
		
		if(most_value_action.type != Action::Pass) {	
				switch (most_value_action.type) {
					case Action::Chi: {
						Tile left_tile = game_state.players[current_player].pond.back();
						left_tile.rotated = true;
						game_state.players[action.player].open.push_back(left_tile);
						
						int count = 0;
						for(std::vector<int>::iterator it = most_value_action.target.begin(); it != most_value_action.target.end(); ++it) {
							Tile target_tile = game_state.players[action.player].hand[(*it) - count];
							game_state.players[action.player].open.push_back(target_tile);
							game_state.players[action.player].hand.erase(game_state.players[action.player].hand.begin() + (*it) - count);

							count++;
						}
						
						Action discard;
						discard.type = Action::Discard;
						discard.player = action.player;
						game_state.possible_actions.push_back(discard);
						
						draw_phase = false;
						
					} break;
				
					case Action::Pon: {
					
					} break;
					
					case Action::Kan: {
					
					} break;
					
					case Action::Ron: {
					
					} break;
					
					default: break;
				}
			most_value_action.type = Action::Pass;
		}
		return true;
	} else {
		return false;
	}
}