Coverage Report

Created: 2026-11-14 03:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/nix/store/rr0r40cw3mzzbz0sxrnkvnrjpzb82wpc-j27cjzgh5nmq3y8k6hwa6zpi3qgcjia8-source/src/statefunctions/stateutilities.cc
Line
Count
Source
1
#include "statefunctions/stateutilities.h"
2
3
#include <algorithm>
4
#include <array>
5
#include <cstdint>
6
#include <iostream>
7
#include <memory>
8
#include <ranges>
9
10
#include "controllers/playercontroller.h"
11
#include "statefunctions/decisionfunction.h"
12
#include "types/event.h"
13
#include "types/gamestate.h"
14
#include "types/hand.h"
15
#include "types/piecetype.h"
16
#include "types/winds.h"
17
18
namespace mahjong {
19
20
38
Wind GetSeat(int round, int player) {
21
38
  return static_cast<Wind>((player + 3 * (round % 4)) % 4);
22
38
}
23
24
// Push Event to Player Queue
25
23
void AlertPlayers(const GameState& state, Event e) {
26
23
  e.decision = false;
27
92
  for (const auto& controller : state.controllers) {
28
92
    controller->ReceiveEvent(e);
29
92
  }
30
23
}
31
32
// Count number of piece p that are in given players hands
33
251
uint8_t CountPieces(const Hand& hand, Piece p) {
34
251
  return std::ranges::count(hand.live_range(), p);
35
251
}
36
37
// Remove an instance of piece p from given players hand
38
18
uint8_t RemovePieces(Hand& hand, Piece p, uint8_t count) {
39
18
  uint8_t removed = 0;
40
18
  count = std::min(CountPieces(hand, p), count);
41
252
  const auto match_piece = [&](Piece _p) {
42
252
    if (count > removed && p == _p) {
43
17
      removed++;
44
17
      return true;
45
17
    }
46
235
    return false;
47
252
  };
48
18
  std::ranges::remove_if(hand.live, match_piece);
49
18
  hand.live_count -= removed;
50
18
  return removed;
51
18
}
52
53
// Discard an instance of piece p from given players hand
54
13
void DiscardPiece(Hand& hand, Piece p) {
55
13
  RemovePieces(hand, p, /*count=*/1);
56
13
  hand.discards[hand.discards_count++] = p;
57
13
}
58
59
1
Piece AskForDiscard(const GameState& state) {
60
1
  state.controllers[state.currentPlayer]->ReceiveEvent(Event{
61
1
      .type = Event::kDiscard,        // type
62
1
      .player = state.currentPlayer,  // player
63
1
      .piece = 0,                     // piece
64
1
      .decision = true,               // decision
65
1
  });
66
67
1
  return Piece(GetValidDecisionOrThrow(state, state.hands[state.currentPlayer],
68
1
                                       /*inHand=*/true)
69
1
                   .piece);
70
1
}
71
72
Event GetValidDecisionOrThrow(const GameState& state, const Hand& hand,
73
15
                              bool inHand) {
74
15
  Event decision;
75
15
  bool valid = false;
76
15
  int i = 0;
77
130
  while (!valid) {
78
116
    if (i > 100) {
79
1
      Event replacement_decision = decision;
80
1
      replacement_decision.type = inHand ? Event::kDiscard : Event::kDecline;
81
1
      if (inHand) {
82
1
        replacement_decision.piece = hand.live_range().back().toUint8_t();
83
1
      }
84
1
      if (ValidateDecision(state, hand, replacement_decision, inHand)) {
85
1
        return replacement_decision;
86
1
      }
87
0
      std::cerr
88
0
          << "WARNING: Player Controller sent invalid event too many times."
89
0
          << '\n';
90
0
      std::cerr << "Decision.type: " << decision.type << " Decision.piece "
91
0
                << decision.piece << " player: " << hand.id
92
0
                << " inHand: " << (inHand ? "true" : "false") << '\n';
93
0
      std::cerr << "ERROR: was not able to recover from invalid event." << '\n';
94
0
      throw 0xBAD22222;
95
1
    }
96
115
    i++;
97
115
    decision = state.controllers[hand.id]->RetrieveDecision();
98
115
    valid = ValidateDecision(state, hand, decision, inHand);
99
115
  }
100
14
  return decision;
101
15
}
102
103
bool ValidateDecision(const GameState& state, const Hand& hand, Event decision,
104
116
                      bool inHand) {
105
116
  if (decision.type > Event::kDiscard) {
106
0
    return false;
107
0
  }
108
116
  if (decision.type > Event::kDecline && !inHand) {
109
0
    return false;
110
0
  }
111
116
  if (decision.type < Event::kTsumo && inHand) {
112
0
    return false;
113
0
  }
114
116
  switch (decision.type) {
115
0
    case Event::kRon:
116
0
      return CanRon(state, hand);
117
0
    case Event::kKan:
118
0
      return CanKan(state, hand);
119
1
    case Event::kPon:
120
1
      return CanPon(state, hand);
121
0
    case Event::kChi:
122
0
      return CanChi(state, hand);
123
0
    case Event::kTsumo:
124
0
      return CanTsumo(state, hand);
125
0
    case Event::kConcealedKan:
126
0
      return CanConcealedKan(state, hand);
127
0
    case Event::kConvertedKan:
128
0
      return CanConvertedKan(state, hand);
129
0
    case Event::kRiichi:
130
0
      return CanRiichi(state, hand);
131
112
    case Event::kDiscard:
132
112
      return CountPieces(hand, Piece(decision.piece)) > 0;
133
3
    case Event::kDecline:
134
3
      return true;
135
0
    default:
136
0
      return false;
137
116
  }
138
116
}
139
140
}  // namespace mahjong