-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge_deck.cc
54 lines (54 loc) · 1.61 KB
/
bridge_deck.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// Created by qzz on 2023/10/23.
//
#include "bridge_deck.h"
namespace bridge_learning_env {
BridgeCard BridgeDeck::DealCard(Suit suit,
int rank) {
const int index = CardIndex(suit, rank);
REQUIRE(card_in_deck_[index] == true);
card_in_deck_[index] = false;
--total_count_;
return {suit, rank};
}
BridgeCard BridgeDeck::DealCard(const int card_index) {
REQUIRE(card_in_deck_[card_index] == true);
card_in_deck_[card_index] = false;
--total_count_;
return {CardSuit(card_index), CardRank(card_index)};
}
BridgeCard BridgeDeck::DealCard(std::mt19937 &rng) {
if (Empty()) {
return {};
}
std::discrete_distribution<std::mt19937::result_type> dist(card_in_deck_.begin(), card_in_deck_.end());
const int index = static_cast<int>(dist(rng));
REQUIRE(card_in_deck_[index] == true);
card_in_deck_[index] = false;
--total_count_;
return {CardSuit(index), CardRank(index)};
}
std::vector<BridgeCard> BridgeDeck::Cards() const {
std::vector<BridgeCard> cards;
cards.reserve(total_count_);
for (const Suit suit : kAllSuits) {
for (int rank = 0; rank < kNumCardsPerSuit; ++rank) {
if (CardInDeck(suit, rank)) {
cards.emplace_back(suit, rank);
}
}
}
return cards;
}
std::array<std::vector<BridgeCard>, kNumSuits> BridgeDeck::CardsBySuits() const {
std::array<std::vector<BridgeCard>, kNumSuits> cards_by_suits{};
for (const Suit suit : kAllSuits) {
for (int rank = 0; rank < kNumCardsPerSuit; ++rank) {
if (CardInDeck(suit, rank)) {
cards_by_suits[suit].emplace_back(suit, rank);
}
}
}
return cards_by_suits;
}
}