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

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

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

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;
}

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_pon(Tiles& tiles) {
	if(tiles[0] == tiles[2]) {
		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;
}