Worked on c++
This commit is contained in:
15
shenzhen_solitaire/c++/lib/card.cpp
Normal file
15
shenzhen_solitaire/c++/lib/card.cpp
Normal 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
|
||||
66
shenzhen_solitaire/c++/lib/goal.cpp
Normal file
66
shenzhen_solitaire/c++/lib/goal.cpp
Normal 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
|
||||
Reference in New Issue
Block a user