-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmahjong.cpp
More file actions
356 lines (342 loc) · 14.1 KB
/
mahjong.cpp
File metadata and controls
356 lines (342 loc) · 14.1 KB
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
// A simple singleplayer mahjong simulator where the player draws from the wall and tries to form four groups and a pair, or seven pairs.
#include <bits/stdc++.h>
using namespace std;
// Source of randomness
mt19937_64 rand_gen(time(0));
const int COUNT = 14;
const vector<pair<int, int>> POSSIBLE_CNTS {{4, 1}, {0, 7}}; // (# groups, # pairs)
class Tile {
public:
static const vector<string> num_suits;
static const vector<string> num_ranks;
static const vector<string> honor_suits;
static const vector<string> honor_ranks;
static const map<string, int> suit_comp;
static const map<string, int> rank_comp;
// returns "X" if there is no next rank
static string next_rank(string rank) {
char num = rank[0];
if (num >= '1' && num <= '8') {
num++;
return string(1, num);
} else {
return "X";
}
}
static Tile to_tile(string str_rep) {
// string must be non-empty
if (str_rep.length() == 0) {
return Tile();
} else {
string first_char = str_rep.substr(0, 1);
if (find(num_ranks.begin(), num_ranks.end(), first_char) == num_ranks.end()) {
// honor tile
if (find(honor_ranks.begin(), honor_ranks.end(), str_rep) == honor_ranks.end()) {
return Tile();
} else {
return Tile("h", str_rep);
}
} else {
// numeric tile
string rest_of_str = str_rep.substr(1);
if (find(num_suits.begin(), num_suits.end(), rest_of_str) == num_suits.end()) {
return Tile();
} else {
return Tile(rest_of_str, first_char);
}
}
}
}
string suit; // m for man, p for pin, s for sou, h for honor. x = invalid suit
string rank; // 1-9 or N E S W Re Gr Wh. X = invalid rank
// returns invalid tile by default
Tile() {
suit = "x";
rank = "X";
}
Tile(string suit, string rank) {
this->suit = suit;
this->rank = rank;
}
bool operator==(const Tile& y) {
return (suit == y.suit) && (rank == y.rank);
}
bool operator<(const Tile& y) {
pair<int, int> cur_id {suit_comp.at(suit), rank_comp.at(rank)};
pair<int, int> other_id {suit_comp.at(y.suit), rank_comp.at(y.rank)};
return cur_id < other_id;
}
string to_str() {
if (suit == "h") {
return rank;
} else {
return rank + suit;
}
}
};
const vector<string> Tile::num_suits {"m", "p", "s"};
const vector<string> Tile::num_ranks {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
const vector<string> Tile::honor_suits {"h"};
const vector<string> Tile::honor_ranks {"N", "E", "S", "W", "Wh", "Gr", "Re"};
const map<string, int> Tile::suit_comp {
{"m", 1},
{"p", 2},
{"s", 3},
{"h", 4}
};
const map<string, int> Tile::rank_comp {
{"1", 1},
{"2", 2},
{"3", 3},
{"4", 4},
{"5", 5},
{"6", 6},
{"7", 7},
{"8", 8},
{"9", 9},
{"N", 10},
{"E", 11},
{"S", 12},
{"W", 13},
{"Wh", 14},
{"Gr", 15},
{"Re", 16}
};
class Hand {
public:
vector<Tile> tiles;
Hand() {
this->tiles = vector<Tile>();
}
Hand(vector<Tile>& tiles) {
this->tiles = vector<Tile>(tiles);
}
void shuffle() {
std::shuffle(tiles.begin(), tiles.end(), rand_gen);
}
void insert(Tile t) {
tiles.push_back(t);
sort(tiles.begin(), tiles.end());
}
bool contains(Tile t) {
return find(tiles.begin(), tiles.end(), t) != tiles.end();
}
bool remove(Tile t) {
if (contains(t)) {
tiles.erase(find(tiles.begin(), tiles.end(), t));
return true;
} else {
return false;
}
}
bool complete() {
if (tiles.size() != COUNT) return false;
// Recursive search to find all possibilities for (group count, pair count)
set<pair<int, int>> cnt_possible;
// DFS stack
vector<pair<Hand, pair<int, int>>> dfs;
dfs.push_back({*this, {0, 0}});
while (!dfs.empty()) {
auto cur = dfs.back();
dfs.pop_back();
Hand cur_hand = cur.first;
int cur_g_cnt, cur_p_cnt;
tie(cur_g_cnt, cur_p_cnt) = cur.second;
// If we processed the entire hand
if (cur_hand.tiles.empty()) {
cnt_possible.insert({cur_g_cnt, cur_p_cnt});
} else {
// Make a group or pair with the first tile
Tile first = cur_hand.tiles[0];
cur_hand.remove(first);
Hand hand_copy = cur_hand;
// Check for a pair
if (hand_copy.remove(first)) {
dfs.push_back({hand_copy, {cur_g_cnt, cur_p_cnt + 1}});
// Check for a triplet
if (hand_copy.remove(first)) {
dfs.push_back({hand_copy, {cur_g_cnt + 1, cur_p_cnt}});
}
}
// Check for a sequence
string second_rank = Tile::next_rank(first.rank);
string third_rank = Tile::next_rank(second_rank);
Tile second = Tile(first.suit, second_rank);
Tile third = Tile(first.suit, third_rank);
if (cur_hand.remove(second) && cur_hand.remove(third)) {
dfs.push_back({cur_hand, {cur_g_cnt + 1, cur_p_cnt}});
}
}
}
// Check all cases in POSSIBLE_CNTS
for (auto good_cnt : POSSIBLE_CNTS) {
if (cnt_possible.count(good_cnt)) return true;
}
return false;
}
string to_str() {
string output = "";
for (Tile t : tiles) {
output += t.to_str() + " ";
}
return output;
}
};
int main() {
// make the wall
Hand wall = Hand();
for (string s : Tile::num_suits) {
for (string r : Tile::num_ranks) {
for (int i = 0; i < 4; i++) {
wall.insert(Tile(s, r));
}
}
}
for (string s : Tile::honor_suits) {
for (string r : Tile::honor_ranks) {
for (int i = 0; i < 4; i++) {
wall.insert(Tile(s, r));
}
}
}
wall.shuffle();
// Choose the dora indicator
Tile dora_ind = wall.tiles[0];
auto next_tile = wall.tiles.begin() + 1;
// Make the player's starting hand
Hand player_hand = Hand();
for (int i = 0; i < 13; i++) {
player_hand.insert(*next_tile);
next_tile++;
}
// Game Loop
bool hand_completed = false;
bool riichi = false;
int turn = 0;
Tile draw;
for (; next_tile != wall.tiles.end(); turn++) {
draw = *next_tile;
next_tile++;
cout << "Turn " << turn << endl;
cout << "Dora Indicator: " << dora_ind.to_str() << endl;
cout << "Your Hand:" << endl;
cout << player_hand.to_str() << "| " << draw.to_str() << "\n";
player_hand.insert(draw);
while (true) {
cout << "Enter move: ";
string action;
cin >> action;
// Riichi action
if (action == ">>") {
cin >> action;
Tile discard = Tile::to_tile(action);
if (player_hand.contains(discard)) {
player_hand.remove(discard);
riichi = true;
break;
} else {
cout << "Invalid move!" << endl;
}
}
// Check if hand is complete
else if (action == "!") {
if (player_hand.complete()) {
hand_completed = true;
break;
} else {
cout << "Invalid move!" << endl;
}
}
// Regular discard
else {
Tile discard = Tile::to_tile(action);
if (player_hand.contains(discard)) {
player_hand.remove(discard);
break;
} else {
cout << "Invalid move!" << endl;
}
}
}
cout << endl;
if (riichi || hand_completed) break;
}
// Riichi fast-forward
if (riichi) {
turn++;
for (; next_tile != wall.tiles.end(); turn++) {
draw = *next_tile;
next_tile++;
player_hand.insert(draw);
if (player_hand.complete()) {
hand_completed = true;
break;
} else {
player_hand.remove(draw);
}
}
}
if (hand_completed) {
player_hand.remove(draw);
cout << "| " << player_hand.to_str() << "| " << draw.to_str() << " |" << endl;
cout << "You completed your hand in " << turn << " turns. Congratulations!" << endl;
cout << endl;
cout << " :-::::::: ::: \n"
" --*@@@%%#*%@@@@*: ::%@%%@- \n"
" :-%@%****#@@#=:::::: :+@%+###@+ \n"
" :*@#***#@@+:: :*@**#####@+ \n"
" :*@****%@=: :=@#**+#####@+ \n"
" *-*# #** :@%***#@-: :@%*+..%#####@@=: \n"
" *:..::..# :%@***%@: :%@......%#######@%: # \n"
" #=.....=# :@****@: :=@-.:::::.%#######%@= # #:+ \n"
" -#*-: #-........:# .@#***@%: --::::::- :%#..:::::::%##+*####@@: #-:-*-..*# \n"
" -*@+#@@#::*###:...:--=* :@%***#@#:: -:=#@@@@@@@@@@@@@@@:..::::::::==::#####@@: #:.....:## \n"
" +@#**++%@@+::#:.* :+@@#*+*%@@@@%*****************#%%=:::::::::::#####@@@@@#-: #-........-* \n"
" -@%####****%@%=* ::-#@@#**************************#%+:::::::-###*%==+++*@+*:......*## \n"
" :@@##########*%@*. :-@@#*********************==+*********@::::::::-:-%++++++*%@** #*..# \n"
" -@@####++*###*=-@@-: :%@**************************+====+*******@=:=#-::::::=#+**++@= *=+ \n"
" :@%######::::::...*@@%@*************************##**=+=====+*****#*=+++==--:::-#+++@+- \n"
" :@#######=:::::::...@****************************+===========+**#===+++**###****%++@- \n"
" :@%######-::::::::-#************@***********+++===++*##******+*#+=*#=------------@@+: \n"
" :#@####=::::::::-#**************@*******++**********#####******##-------+##------+@- \n"
" :@@####*::::::*+***************%@*******************#####*****%--------#**+-----*@:::::::: \n"
" :%@@###::::-*=+++**++++********@@%******************####%****#----------#------@%%%%%%@#@@@- \n"
" :#@-=-:::*====++**************#@@@###***************#####****%---------%-----%#######.=-.@: \n"
" -@=::::#===+******************@.=@@#####******##****###%*****##=------#---=#.:%###+.+-.@= \n"
" ::#@+:-*==*******#****###*******@:..:%@#####****####**####=:......*=---#*%%%....-%.:*..@= \n"
" -+@+..+@*=+#*******%#***######*****#=....:@%###############%........+***#####+......%-.#@: \n"
" :@=.:++*#***#*******@*%@%*#########***@......:#@%%@@#########%:......**#######........=%:: \n"
" -@++++++##***##*****#+@*%.-#%@###########@............:#@%####@#......*%*++++*:.........:* \n"
" :#@*++++###**###*****@..%#%.........:-+**+...................+@@##.....#*****+*.........*# \n"
" :%#++**+%##**####****@...:@#*..................................@%**....#==-:.#........** \n"
" *@+***+###**####%**#@==++++@@=................................@@**+...#....+.......#@%: \n"
" :@#+*++####**###%***%.............................:+%%*++*#:..-@**#-..###+==....:#%#@@: \n"
" :%@+++##########%***%@@@@@@*..................=@@@@@@@@@@+...:@*#####*****#=.:#*%%%@@: * \n"
" :@@+#########%..*@@@@%*#*#@#..............+@@@%-...........:%*######@*..:%@#***%%@@: ##+:..+ \n"
" :*@%%#######%..#@%....####@#............:+................-%#####%*.......%@**##=.........:* \n"
" :=@@##########%:.@@..%-=*#**=:..................:::::::::::.*######@-......%@%#.............:* \n"
"= :@@..#####%*####@.@#..#*::=:..@................:::-::+-::::..@*#@@%#@=:-...@@%%%:............:# \n"
"=-=@%.-+..+#%*******@@#...#+:..:%...............==::::::::::...:@=.....=%@=::=@#%%%***#%#+-:....* \n"
"===-@@-.==.@**#*****@@#..:::---:........#******=...............@..........:@=*%%%%%#****@@: *##** \n"
"==== :@@+:@**%%*****%@::::----::::..:+*=======%...............+@.............@%%%%%#****%@- \n"
"====== :*@%*%%#*****%%:=-:--:::::......#*==+#:................@-.............%%#%%%#*@%**@+ \n"
"======= -@#%#%******%%::::::::::.............................@-..............@%%%%%%@@-@#@%: \n"
"=======-#@%#%@******#%*..::.................................+@...............@@###%@@: -*%: \n"
"=======:@@#@@@%******%@-....................................%-..............%-@%#@@%: \n"
"=======:@@@@+:@******%%%@...................................@:.............*=.*@@@-: ==\n"
"=======:@@@*--+@*****#@%%%@=............................:*#%@@:...........-#..=@+: ====\n"
"=======-@@*-==-#@****@@%%%%%%@#:....................+%*-:::@@%*#..:::....*#..-=@%: ======\n"
"========--=====-%@**%@@@%%%%%%%%%@@@@%+-:....:=%@%=--=%@@@@*....@@:::::%%...-==%@: ========\n"
"================-+@#@@-@%%%%%%%%%%@@%%%%%%%%%#:=%*:.....+#..::::..-#*@=....:===+@: ===========\n"
"==================--*:-:@@%%%%%%@@-@@%%%%%%@@*:...=*#%%%:.:::::::.:@:....:::-==+@- ==============\n"
"========================:#@@%%%@+.::+@@@@%...*%%%%%%%%%..:::::::.%- ....:::::===@+ =================\n"
"===========================:#@@@@@@%**@:.+%%%%%%%%%%%@..::::::..@. ...::::::-==@+==================\n"
"========================-:%@@@+===-#@.*%######%#%%+:%.... ....%:.. .::::::::=+@===================\n"
"======================:#@@*======+@:%######%%+:::::#=. ..:@... .::::::::-+@:==================\n"
"====================:%@#-=======@*%#####%=:::::::::@.. . .@... .::::::::-%@:==================\n"
"===================:@@:-======#@%#####%%-:::::::::*+.. ...@... ..::::::::@@:==================\n"
"==================:@#::-=====@%###########%#=:::::#... .....=+... ..:::::::-@====================\n";
} else {
cout << "You ran out of tiles!" << endl;
}
}