66 lines
1.2 KiB
C++
66 lines
1.2 KiB
C++
|
|
#include "goal.hpp"
|
|
|
|
namespace solitaire {
|
|
auto
|
|
Goal::getEmptyId() -> std::optional<int> {
|
|
int counter = 0;
|
|
for (const auto& slot : goal) {
|
|
if (!slot) {
|
|
return counter;
|
|
}
|
|
++counter;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
[[nodiscard]] auto
|
|
Goal::getId(CardType suit) const noexcept -> std::optional<int> {
|
|
int counter = 0;
|
|
for (const auto& slot : goal) {
|
|
if (slot && slot->type == suit) {
|
|
return counter;
|
|
}
|
|
++counter;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
[[nodiscard]] auto
|
|
Goal::get(CardType suit) const noexcept -> std::optional<int> {
|
|
if (auto index = getId(suit); index) {
|
|
return goal[*index]->value;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
void
|
|
Goal::set(CardType suit, int value) noexcept {
|
|
assert(value >= 0);
|
|
assert(value <= 9);
|
|
|
|
const auto card = [&]() -> std::optional<Card> {
|
|
if (value == 0) {
|
|
return std::nullopt;
|
|
}
|
|
return Card{suit, value};
|
|
}();
|
|
|
|
const int goal_index = [&]() -> int {
|
|
if (auto index = getId(suit); index) {
|
|
return *index;
|
|
}
|
|
return *getEmptyId();
|
|
}();
|
|
|
|
goal[goal_index] = card;
|
|
}
|
|
|
|
void
|
|
Goal::inc(CardType suit) noexcept {
|
|
auto get_value = get(suit);
|
|
int new_value = get_value ? (*get_value) + 1 : 1;
|
|
set(suit, new_value);
|
|
}
|
|
|
|
} // namespace solitaire
|