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/controllers/alphabeticalbot.cc
Line
Count
Source
1
#include "controllers/alphabeticalbot.h"
2
3
#include <cstddef>
4
#include <string>
5
#include <vector>
6
7
#include "controllers/controllermanager.h"
8
#include "types/event.h"
9
#include "types/piecetype.h"
10
#include "types/winds.h"
11
12
namespace mahjong {
13
REGISTER_PLAYER_CONTROLLER(AlphabeticalBot);
14
15
void AlphabeticalBot::RoundStart(std::vector<Piece> hand, Wind /*seatWind*/,
16
0
                                 Wind /*prevalentWind*/) {
17
0
  hand_ = hand;
18
0
  decisionToTake_.type = Event::kDiscard;
19
0
  decisionToTake_.player = id_;
20
0
}
21
22
0
void AlphabeticalBot::ReceiveEvent(Event e) {
23
  // const Piece eventPiece = Piece(e.piece);
24
  // std::cout << "Player " << id <<" got event " << e << std::endl;
25
0
  if (e.type <= Event::kDiscard && e.decision && e.player == id_) {
26
0
    if (e.type == Event::kDiscard) {
27
0
      if (e.type < decisionToTake_.type) {
28
0
        decisionToTake_ = e;
29
0
      }
30
0
    }
31
0
  }
32
0
  switch (e.type) {
33
0
    case Event::kDora:
34
0
      break;
35
0
    case Event::kKan:
36
0
    case Event::kChi:
37
0
    case Event::kPon:
38
0
      if (e.decision) {
39
0
        decisionToTake_.type = Event::kDecline;
40
0
      }
41
0
      break;
42
0
    case Event::kDiscard:
43
0
      if (e.decision && e.player == id_) {
44
        // std::cout << "Player " << id_ << " pushing piece into hand: " <<
45
        // e.piece << std::endl;
46
0
        hand_.emplace_back(e.piece);
47
0
      }
48
0
      break;
49
0
    case Event::kTsumo:
50
0
    default:
51
0
      if (e.decision) {
52
0
        decisionToTake_ = e;
53
0
      }
54
0
      break;
55
0
  }
56
0
}
57
58
0
Event AlphabeticalBot::RetrieveDecision() {
59
0
  if (hand_.empty()) {
60
0
    return decisionToTake_;
61
0
  }
62
0
  if (decisionToTake_.type == Event::kDiscard) {
63
0
    auto index_to_discard = getDiscardPiece();
64
0
    decisionToTake_.piece = hand_[index_to_discard].raw_value();
65
    // std::cout << "Removing piece "<< indexToDiscard <<std::endl;
66
0
    hand_.erase(hand_.begin() + index_to_discard);
67
0
  }
68
0
  auto final = decisionToTake_;
69
0
  decisionToTake_.type = Event::kDiscard;
70
  // std::cout << "Sending decision "<< final <<std::endl;
71
0
  return final;
72
0
}
73
74
// Choose a piece to discard (
75
0
int AlphabeticalBot::getDiscardPiece() {
76
0
  std::vector<std::string> string_board;
77
0
  string_board.reserve(hand_.size());
78
0
  for (const auto& i : hand_) {
79
0
    string_board.push_back(i.toStr());
80
0
  }
81
82
0
  int index = 0;
83
0
  for (size_t i = 1; i < string_board.size();
84
0
       i++)  // find index of first string (alphabetically)
85
0
  {
86
0
    if (string_board.at(i) < string_board[index]) {
87
0
      index = i;
88
0
    }
89
0
  }
90
  // std::cout << "getDiscardPiece(): " << index << std::endl;
91
0
  return index;
92
0
}
93
94
}  // namespace mahjong