summaryrefslogtreecommitdiff
path: root/src/hand.cpp
blob: bc4fb7a2f1b83e59b001f9b25ab6b3d6fddac76a (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
#include "hand.h"

bool Hand::agari(const Tiles& tiles) {
	return basic_format(tiles);
}

bool Hand::tenpai(const Tiles& tiles) {
	return basic_format_tenpai(tiles);
}

List<Sets> Hand::get_breakdowns(const Tiles& tiles) {
	List<Sets> hands;
	
	basic_format(tiles, hands);
	
	return hands;
}

bool Hand::basic_format(const Tiles& tiles, bool pair_eaten) {
	if(!tiles) {
		return true; // All tiles eaten.
	}
	
	Tiles rest;
	
	if(!pair_eaten) {
		rest = tiles;
		if(eat_pair(rest) && basic_format(rest, true)) {
			return true;
		}
	}
	
	if(tiles.size() < 3) {
		return false;
	}
	
	rest = tiles;
	if(eat_pon(rest) && basic_format(rest, pair_eaten)) {
		return true;
	}
	
	rest = tiles;
	if(eat_chi(rest) && basic_format(rest, pair_eaten)) {
		return true;
	}
	
	return false;
}

void Hand::basic_format(const Tiles& tiles, List<Sets>& hands, const Sets& hand, bool pair_eaten) {
	if(!tiles) {
		hands.push_back(hand);
		return; // All tiles eaten.
	}
	
	Tiles rest;
	Sets new_hand;
	
	if(!pair_eaten) {
		rest = tiles;
		new_hand = hand;
		if(eat_pair(rest, new_hand)) {
			basic_format(rest, hands, new_hand, true);
		}
	}
	
	if(tiles.size() < 3) {
		return;
	}
	
	rest = tiles;
	new_hand = hand;
	if(eat_pon(rest, new_hand)) {
		basic_format(rest, hands, new_hand, pair_eaten);
	}
	
	rest = tiles;
	new_hand = hand;
	if(eat_chi(rest, new_hand)) {
		basic_format(rest, hands, new_hand, pair_eaten);
	}
}

bool Hand::basic_format_tenpai(const Tiles& tiles, bool pair_eaten, bool wait_eaten) {
	if(!tiles) {
		return true;
	}
	
	Tiles rest;
	
	if(!pair_eaten && !wait_eaten) {
		rest = tiles;
		if(eat_tanki(rest) && basic_format_tenpai(rest, true, true)) {
			return true;
		}
	}
	
	if(tiles.size() < 2) {
		return false;
	}
	
	if(!pair_eaten) {
		rest = tiles;
		if(eat_pair(rest) && basic_format_tenpai(rest, true, wait_eaten)) {
			return true;
		}
	}
	
	if(!wait_eaten) {
		rest = tiles;
		if(eat_chi_wait(rest) && basic_format_tenpai(rest, pair_eaten, true)) {
			return true;
		}
	}
	
	if(tiles.size() < 3) {
		return false;
	}
	
	rest = tiles;
	if(eat_pon(rest) && basic_format_tenpai(rest, pair_eaten, wait_eaten)) {
		return true;
	}
	
	rest = tiles;
	if(eat_chi(rest) && basic_format_tenpai(rest, pair_eaten, wait_eaten)) {
		return true;
	}
	
	return false;
}

bool Hand::eat_tanki(Tiles& tiles) {
	tiles.erase(tiles.begin());
	return true;
}

bool Hand::eat_chi_wait(Tiles& tiles) {
	Tile::Set set = tiles.front().get_set();
	int num = tiles.front().get_num();
	
	if(set == Tile::Honor || num > 8) {
		return false;
	}
	
	// Look for T+1.
	Tiles::iterator it = std::find(tiles.begin(), tiles.end(), Tile(set, num + 1));
	if(it != tiles.end()) {
		tiles.erase(it);
		tiles.erase(tiles.begin());
		return true;
	}
	
	if(num > 7) {
		return Tiles();
	}
	
	// Look for T+2.
	it = std::find(tiles.begin(), tiles.end(), Tile(set, num + 2));
	if(it != tiles.end()) {
		tiles.erase(it);
		tiles.erase(tiles.begin());
		return true;
	}
	
	return false;
}

bool Hand::eat_pair(Tiles& tiles) {
	if(tiles[0] == tiles[1]) {
		tiles.erase(tiles.begin(), tiles.begin() + 2);
		return true;
	}
	return false;
}

bool Hand::eat_pair(Tiles& tiles, Sets& hand) {
	if(tiles[0] == tiles[1]) {
		hand.push_back(Set(Set::Pair, Tiles(tiles.begin(), tiles.begin() + 2), false));
		tiles.erase(tiles.begin(), tiles.begin() + 2);
		return true;
	}
	return false;
}

bool Hand::eat_pon(Tiles& tiles) {
	if(tiles[0] == tiles[2]) {
		tiles.erase(tiles.begin(), tiles.begin() + 3);
		return true;
	}
	return false;
}

bool Hand::eat_pon(Tiles& tiles, Sets& hand) {
	if(tiles[0] == tiles[2]) {
		hand.push_back(Set(Set::Pon, Tiles(tiles.begin(), tiles.begin() + 3), false));
		tiles.erase(tiles.begin(), tiles.begin() + 3);
		return true;
	}
	return false;
}

bool Hand::eat_chi(Tiles& tiles) {
	Tile::Set set = tiles.front().get_set();
	int num = tiles.front().get_num();
	
	if(set == Tile::Honor || num > 7) {
		// Can't be chi-ed.
		return false;
	}
	
	Tiles::iterator it1 = std::find(tiles.begin(), tiles.end(), Tile(set, num + 1));
	if(it1 != tiles.end()) {
		Tiles::iterator it2 = std::find(tiles.begin(), tiles.end(), Tile(set, num + 2));
		if(it2 != tiles.end()) {
			// We can eat a chi of tiles.
			tiles.erase(it2);
			tiles.erase(it1);
			tiles.erase(tiles.begin());
			return true;
		}
	}
	return false;
}

bool Hand::eat_chi(Tiles& tiles, Sets& hand) {
	Tile::Set set = tiles.front().get_set();
	int num = tiles.front().get_num();
	
	if(set == Tile::Honor || num > 7) {
		// Can't be chi-ed.
		return false;
	}
	
	Tiles::iterator it1 = std::find(tiles.begin(), tiles.end(), Tile(set, num + 1));
	if(it1 != tiles.end()) {
		Tiles::iterator it2 = std::find(tiles.begin(), tiles.end(), Tile(set, num + 2));
		if(it2 != tiles.end()) {
			// We can eat a chi of tiles.
			Tiles chi;
			chi.push_back(tiles.front());
			chi.push_back(*it1);
			chi.push_back(*it2);
			hand.push_back(Set(Set::Chi, chi, false));
			tiles.erase(it2);
			tiles.erase(it1);
			tiles.erase(tiles.begin());
			return true;
		}
	}
	return false;
}