/nix/store/i1aar97n7b4yf8rk94p66if25brfvvdx-gqvzl8a5pvrg3xj44q0msrndcripi8dk-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: " << e.piece << std::endl; |
45 | 0 | hand_.emplace_back(e.piece); |
46 | 0 | } |
47 | 0 | break; |
48 | 0 | case Event::kTsumo: |
49 | 0 | default: |
50 | 0 | if (e.decision) { |
51 | 0 | decisionToTake_ = e; |
52 | 0 | } |
53 | 0 | break; |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | 0 | Event AlphabeticalBot::RetrieveDecision() { |
58 | 0 | if (hand_.empty()) { |
59 | 0 | return decisionToTake_; |
60 | 0 | } |
61 | 0 | if (decisionToTake_.type == Event::kDiscard) { |
62 | 0 | auto index_to_discard = getDiscardPiece(); |
63 | 0 | decisionToTake_.piece = hand_[index_to_discard].raw_value(); |
64 | | // std::cout << "Removing piece "<< indexToDiscard <<std::endl; |
65 | 0 | hand_.erase(hand_.begin() + index_to_discard); |
66 | 0 | } |
67 | 0 | auto final = decisionToTake_; |
68 | 0 | decisionToTake_.type = Event::kDiscard; |
69 | | // std::cout << "Sending decision "<< final <<std::endl; |
70 | 0 | return final; |
71 | 0 | } |
72 | | |
73 | | // Choose a piece to discard ( |
74 | 0 | int AlphabeticalBot::getDiscardPiece() { |
75 | 0 | std::vector<std::string> string_board; |
76 | 0 | string_board.reserve(hand_.size()); |
77 | 0 | for (const auto& i : hand_) { |
78 | 0 | string_board.push_back(i.toStr()); |
79 | 0 | } |
80 | |
|
81 | 0 | int index = 0; |
82 | 0 | for (size_t i = 1; i < string_board.size(); |
83 | 0 | i++) // find index of first string (alphabetically) |
84 | 0 | { |
85 | 0 | if (string_board.at(i) < string_board[index]) { |
86 | 0 | index = i; |
87 | 0 | } |
88 | 0 | } |
89 | | // std::cout << "getDiscardPiece(): " << index << std::endl; |
90 | 0 | return index; |
91 | 0 | } |
92 | | |
93 | | } // namespace mahjong |