/nix/store/rr0r40cw3mzzbz0sxrnkvnrjpzb82wpc-j27cjzgh5nmq3y8k6hwa6zpi3qgcjia8-source/src/statefunctions/gamestates/playerhand.cc
Line | Count | Source |
1 | | #include <array> |
2 | | #include <cstdint> |
3 | | #include <iostream> |
4 | | #include <memory> |
5 | | #include <vector> |
6 | | |
7 | | #include "statefunctions/decisionfunction.h" |
8 | | #include "statefunctions/router.h" |
9 | | #include "statefunctions/stateutilities.h" |
10 | | #include "types/event.h" |
11 | | #include "types/gamestate.h" |
12 | | #include "types/hand.h" |
13 | | #include "types/piecetype.h" |
14 | | #include "types/statefunction.h" |
15 | | #include "types/typeprinter.h" |
16 | | |
17 | | namespace mahjong { |
18 | | |
19 | | namespace { |
20 | 10 | std::unique_ptr<GameState> PlayerHand(std::unique_ptr<GameState> state) { |
21 | 10 | const std::vector<PossibleDecision> decisions = { |
22 | 10 | PossibleDecision{.type = Event::kTsumo, .func = CanTsumo}, |
23 | 10 | PossibleDecision{.type = Event::kConcealedKan, .func = CanConcealedKan}, |
24 | 10 | PossibleDecision{.type = Event::kConvertedKan, .func = CanConvertedKan}, |
25 | 10 | PossibleDecision{.type = Event::kRiichi, .func = CanRiichi}, |
26 | 10 | PossibleDecision{.type = Event::kDiscard, |
27 | 10 | .func = [](const GameState&, const Hand& hand) { |
28 | 10 | return !hand.riichi; |
29 | 10 | }}}; |
30 | | |
31 | 10 | const Hand& hand = state->hands[state->currentPlayer]; |
32 | 10 | bool decision_asked = false; |
33 | 50 | for (const auto& [decision, decisionIsPossible] : decisions) { |
34 | 50 | if (decisionIsPossible(*state, hand)) { |
35 | 10 | decision_asked = true; |
36 | 10 | state->controllers.at(state->currentPlayer) |
37 | 10 | ->ReceiveEvent(Event{ |
38 | 10 | .type = decision, // type |
39 | 10 | .player = state->currentPlayer, // player |
40 | 10 | .piece = static_cast<int16_t>( |
41 | 10 | state->pendingPiece.toUint8_t()), // piece |
42 | 10 | .decision = true, // decision |
43 | 10 | }); |
44 | 10 | } |
45 | 50 | } |
46 | | |
47 | 10 | Event decision; |
48 | 10 | if (!decision_asked) { |
49 | 0 | decision.type = Event::kDiscard; |
50 | 0 | decision.piece = static_cast<uint16_t>(state->pendingPiece.toUint8_t()); |
51 | 0 | decision.player = state->currentPlayer; |
52 | 0 | decision.decision = true; |
53 | 0 | state->nextState = StateFunctionType::kDiscard; |
54 | 10 | } else { |
55 | 10 | decision = GetValidDecisionOrThrow(*state, hand, |
56 | 10 | /*inHand=*/true); |
57 | 10 | } |
58 | | |
59 | | // note riichi handling is a lil borked on the player agency side |
60 | | // checkout riichi.cpp for more info |
61 | 10 | if (decision.type == Event::kDiscard) { |
62 | 10 | state->pendingPiece = Piece(decision.piece); |
63 | 10 | } |
64 | | |
65 | 10 | switch (decision.type) { |
66 | 0 | case Event::kTsumo: |
67 | 0 | state->nextState = StateFunctionType::kTsumo; |
68 | 0 | break; |
69 | 0 | case Event::kConcealedKan: |
70 | 0 | state->nextState = StateFunctionType::kConcealedKan; |
71 | 0 | break; |
72 | 0 | case Event::kConvertedKan: |
73 | 0 | state->nextState = StateFunctionType::kConvertedKan; |
74 | 0 | break; |
75 | 0 | case Event::kRiichi: |
76 | 0 | state->nextState = StateFunctionType::kRiichi; |
77 | 0 | break; |
78 | 10 | case Event::kDiscard: |
79 | 10 | state->nextState = StateFunctionType::kDiscard; |
80 | 10 | break; |
81 | 0 | default: |
82 | 0 | std::cerr << "Invalid Decision Type in playerhand: " << decision.type |
83 | 0 | << '\n'; |
84 | 0 | state->nextState = StateFunctionType::kError; |
85 | 0 | break; |
86 | 10 | } |
87 | | |
88 | 10 | return state; |
89 | 10 | } |
90 | | } // namespace |
91 | | |
92 | | REGISTER_ROUTE(PlayerHand, StateFunctionType::kPlayerHand); |
93 | | } // namespace mahjong |