/nix/store/rr0r40cw3mzzbz0sxrnkvnrjpzb82wpc-j27cjzgh5nmq3y8k6hwa6zpi3qgcjia8-source/src/statefunctions/gamestates/chi.cc
Line | Count | Source |
1 | | #include <array> |
2 | | #include <cstdint> |
3 | | #include <iostream> |
4 | | #include <memory> |
5 | | |
6 | | #include "statefunctions/router.h" |
7 | | #include "statefunctions/stateutilities.h" |
8 | | #include "types/event.h" |
9 | | #include "types/gamestate.h" |
10 | | #include "types/hand.h" |
11 | | #include "types/meld.h" |
12 | | #include "types/pieces.h" |
13 | | #include "types/piecetype.h" |
14 | | #include "types/sets.h" |
15 | | #include "types/statefunction.h" |
16 | | |
17 | | namespace mahjong { |
18 | | namespace { |
19 | 0 | Piece GetChiStart(const Hand& hand, Piece piece) { |
20 | 0 | if (CountPieces(hand, piece - 2) > 0 && CountPieces(hand, piece - 1) > 0) { |
21 | 0 | return piece - 2; |
22 | 0 | } |
23 | 0 | if (CountPieces(hand, piece - 1) > 0 && CountPieces(hand, piece + 1) > 0) { |
24 | 0 | return piece - 1; |
25 | 0 | } |
26 | 0 | if (CountPieces(hand, piece + 1) > 0 && CountPieces(hand, piece + 2) > 0) { |
27 | 0 | return piece; |
28 | 0 | } |
29 | 0 | return kError; |
30 | 0 | } |
31 | | |
32 | 0 | std::unique_ptr<GameState> Chi(std::unique_ptr<GameState> state) { |
33 | 0 | if (Hand& hand = state->hands[state->currentPlayer]; |
34 | 0 | hand.riichi && hand.discards_count == hand.riichiPieceDiscard) { |
35 | 0 | hand.riichiPieceDiscard++; |
36 | 0 | } |
37 | |
|
38 | 0 | state->currentPlayer = state->lastCaller; |
39 | 0 | state->lastCall = state->turnNum; |
40 | 0 | state->concealedKan = false; |
41 | 0 | state->turnNum++; |
42 | |
|
43 | 0 | Hand& hand = state->hands[state->currentPlayer]; |
44 | 0 | hand.open = true; |
45 | | // only gives a single one of the chis |
46 | | // ui oof |
47 | 0 | const Piece chi_start = GetChiStart(hand, state->pendingPiece); |
48 | 0 | if (chi_start == kError) { |
49 | 0 | std::cerr << "Failed to get start of Chi" << '\n'; |
50 | 0 | state->nextState = StateFunctionType::kError; |
51 | 0 | return state; |
52 | 0 | } |
53 | | |
54 | 0 | for (int i = 0; i < 3; ++i) { |
55 | 0 | if (chi_start + i == state->pendingPiece) { |
56 | 0 | continue; |
57 | 0 | } |
58 | 0 | if (RemovePieces(hand, chi_start + i, |
59 | 0 | /*count=*/1) != 1) { |
60 | 0 | std::cerr << "Not Enough Pieces to remove in Chi" << '\n'; |
61 | 0 | state->nextState = StateFunctionType::kError; |
62 | 0 | return state; |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | 0 | hand.melds[hand.meld_count++] = Meld{ |
67 | 0 | .type = SetType::kChi, |
68 | 0 | .start = chi_start, |
69 | 0 | }; |
70 | |
|
71 | 0 | AlertPlayers( |
72 | 0 | *state, |
73 | 0 | Event{ |
74 | 0 | .type = Event::kChi, // type |
75 | 0 | .player = state->lastCaller, // player |
76 | 0 | .piece = static_cast<int16_t>(chi_start.toUint8_t()), // piece |
77 | 0 | .decision = false, // decision |
78 | 0 | }); |
79 | |
|
80 | 0 | state->pendingPiece = AskForDiscard(*state); |
81 | |
|
82 | 0 | state->nextState = StateFunctionType::kDiscard; |
83 | 0 | return state; |
84 | 0 | } |
85 | | } // namespace |
86 | | |
87 | | REGISTER_ROUTE(Chi, StateFunctionType::kChi); |
88 | | } // namespace mahjong |