Worked on c++

This commit is contained in:
Lukas Wölfer
2020-03-20 00:12:32 +01:00
parent de72585989
commit 5af5282aab
8 changed files with 209 additions and 18 deletions

View File

@@ -0,0 +1,15 @@
#include "card.hpp"
namespace solitaire {
auto
isNormalCardType(CardType type) -> bool {
switch (type) {
case CardType::Red:
case CardType::Green:
case CardType::Black:
return true;
break;
default:
return false;
}
}
} // namespace solitaire

View File

@@ -0,0 +1,66 @@
#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