summaryrefslogtreecommitdiff
path: root/server/game.cpp
blob: bd4314ef516986e13c4b38fb33557648107e627f (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
#include "game.h"

#include <boost/bind.hpp>

#include <iostream>

Game::p Game::create(Player::p player_1, Player::p player_2, Player::p player_3, Player::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(Player::p player_1, Player::p player_2, Player::p player_3, Player::p player_4) {
	players.push_back(player_1);
	players.push_back(player_2);
	players.push_back(player_3);
	players.push_back(player_4);
}

void Game::handle_ready() {
	std::cout << "Still waiting for " << waiting_players << "." << std::endl;
	
	if(--waiting_players) {
		return;
	}
	
	std::cout << "All ready!" << std::endl;
	
	round_start();
}

void Game::round_start() {
	players[0]->round_start();
	players[0]->id = 0;
	players[1]->round_start();
	players[1]->id = 1;
	players[2]->round_start();
	players[2]->id = 2;
	players[3]->round_start();
	players[3]->id = 3;
	
	// Sett opp runden sin state
	game_state = make_shared<State>();
	
	// Simulates drawing 4, 4 ,4 for each player
	for (int player_num = 0; player_num < 4; player_num++) {
		for (int i = 0; i < 3; i++) {
			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());
	}
	
	current_player     = 0;
	num_player_actions = 0;
	draw_phase         = true;
	
	round_update();
}

void Game::round_update() {
	
	if ( draw_phase ) {
		game_state->players[current_player].hand.push_back(wall.take_one());
		
		Action discard_action;
		discard_action.type = Action::Discard;
		discard_action.player = current_player;
		
		game_state->possible_actions.push_back(discard_action);
		
		// TODO
		// Add code for calculation of possible riichi, tsumo, kan and draw
		
		num_player_actions++;
		
		draw_phase = false;
		
	} else { // Run if we're in the discard phase
		
		
		
		
		// Update who the current player is.
		if ( current_player == 3) {
			current_player = 0;
		} else {
			current_player++;
		}
	
		draw_phase = true;
	}
	
	
	
	// Send RoundState, inkl. liste av mulige actions.
	players[0]->round_state(game_state);
	players[1]->round_state(game_state);
	players[2]->round_state(game_state);
	players[3]->round_state(game_state);
	
	
	// Kall player->get_action(handle_action) for hver spiller som har actions, om ingen kan gjøre noe så blir
	// det neste spiller sin tur.
	if(num_player_actions) {
		players[0]->get_action(boost::bind(&Game::handle_action, shared_from_this(), _1));
	} else {
		round_update();
	}

}

void Game::handle_action(Action action) {
	switch ( action.type ) {
		
		case Action::Pass: {
		
		} break;
		
		case Action::Discard: {
			game_state->players[current_player].pond.push_back(game_state->players[current_player].hand[action.target]);
			game_state->players[current_player].hand.erase(game_state->players[current_player].hand.begin() + action.target);
		} 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--;
	
	// Sjekk action, sjekk om endelig action er avgjort
	// Oppdater state
	// Evt. round_end()
	if (num_player_actions == 0) {
		round_update();
	}
		
	
}

void Game::round_end() {
	// Flere runder? round_start()
	// Ferdig? game_end()
}

void Game::start() {
	std::cout << "Started a game with "
		<< players[0]->nick() << ", "
		<< players[1]->nick() << ", "
		<< players[2]->nick() << " and "
		<< players[3]->nick() << "." << std::endl;
	
	waiting_players = 4;
	
	players[0]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players);
	players[1]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players);
	players[2]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players);
	players[3]->game_start(boost::bind(&Game::handle_ready, shared_from_this()), players);
}