Worked on c++
This commit is contained in:
@@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.16)
|
|||||||
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
|
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
|
||||||
include( "common" )
|
include( "common" )
|
||||||
|
|
||||||
add_library(board STATIC lib/board.cpp)
|
add_library(board STATIC lib/board.cpp lib/card.cpp lib/goal.cpp)
|
||||||
set_property(TARGET board PROPERTY CXX_STANDARD 17)
|
set_property(TARGET board PROPERTY CXX_STANDARD 17)
|
||||||
target_include_directories(board PUBLIC include)
|
target_include_directories(board PUBLIC include)
|
||||||
|
add_subdirectory(auxiliary)
|
||||||
12
shenzhen_solitaire/c++/auxiliary/CMakeLists.txt
Normal file
12
shenzhen_solitaire/c++/auxiliary/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
find_package(SWIG REQUIRED)
|
||||||
|
include(${SWIG_USE_FILE})
|
||||||
|
|
||||||
|
find_package(PythonLibs REQUIRED)
|
||||||
|
|
||||||
|
set_property(SOURCE swig.i PROPERTY CPLUSPLUS ON)
|
||||||
|
swig_add_library(shenzhen_python TYPE SHARED LANGUAGE PYTHON SOURCES swig.i)
|
||||||
|
|
||||||
|
target_include_directories(shenzhen_python PUBLIC ${PYTHON_INCLUDE_PATH})
|
||||||
|
target_link_libraries(shenzhen_python PUBLIC board)
|
||||||
|
|
||||||
|
set_property(TARGET shenzhen_python PROPERTY CXX_STANDARD 17)
|
||||||
54
shenzhen_solitaire/c++/auxiliary/swig.i
Normal file
54
shenzhen_solitaire/c++/auxiliary/swig.i
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
%module shenzhen
|
||||||
|
%{
|
||||||
|
#include "board.hpp"
|
||||||
|
#include "card.hpp"
|
||||||
|
#include "goal.hpp"
|
||||||
|
%}
|
||||||
|
|
||||||
|
namespace solitaire {
|
||||||
|
enum class CardType : int { Zhong, Bai, Fa, Hua, Red, Green, Black };
|
||||||
|
|
||||||
|
auto isNormalCardType(CardType type) -> bool;
|
||||||
|
|
||||||
|
struct Card {
|
||||||
|
CardType type;
|
||||||
|
int value;
|
||||||
|
auto toString() const noexcept -> std::string;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Goal {
|
||||||
|
std::array<std::optional<Card>, 3> goal{};
|
||||||
|
auto getEmptyId() -> std::optional<int>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
auto getId(CardType suit) const noexcept -> std::optional<int>;
|
||||||
|
auto get(CardType suit) const noexcept -> std::optional<int>;
|
||||||
|
|
||||||
|
void set(CardType suit, int value) noexcept;
|
||||||
|
void inc(CardType suit) noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct BunkerField {
|
||||||
|
std::optional<Card> card;
|
||||||
|
bool empty{};
|
||||||
|
bool closed{};
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr int MAX_ROW_SIZE = 13;
|
||||||
|
inline constexpr int MAX_COLUMN_SIZE = 8;
|
||||||
|
|
||||||
|
class Stack {
|
||||||
|
std::array<std::optional<Card>, MAX_COLUMN_SIZE> values{};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Board {
|
||||||
|
std::array<Stack, MAX_ROW_SIZE> field{};
|
||||||
|
std::array<BunkerField, 3> bunker{};
|
||||||
|
Goal goal{};
|
||||||
|
bool flower_gone{};
|
||||||
|
|
||||||
|
auto solved() const noexcept -> bool;
|
||||||
|
auto hash() const noexcept -> std::size_t;
|
||||||
|
auto correct() const noexcept -> bool;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,27 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "card.hpp"
|
||||||
|
#include "goal.hpp"
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cassert>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
namespace solitaire {
|
namespace solitaire {
|
||||||
class Card {
|
|
||||||
bool isNormalCard();
|
struct BunkerField {
|
||||||
bool isSpecialCard();
|
std::optional<Card> card;
|
||||||
|
bool empty{};
|
||||||
|
bool closed{};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BunkerField{
|
inline constexpr int MAX_ROW_SIZE = 13;
|
||||||
Card card;
|
inline constexpr int MAX_COLUMN_SIZE = 8;
|
||||||
bool empty;
|
|
||||||
bool closed;
|
class Stack {
|
||||||
|
std::array<std::optional<Card>, MAX_COLUMN_SIZE> values{};
|
||||||
};
|
};
|
||||||
|
|
||||||
class Board {
|
struct Board {
|
||||||
public:
|
std::array<Stack, MAX_ROW_SIZE> field{};
|
||||||
static constexpr int MAX_ROW_SIZE = 13;
|
std::array<BunkerField, 3> bunker{};
|
||||||
static constexpr int MAX_COLUMN_SIZE = 8;
|
Goal goal{};
|
||||||
|
bool flower_gone{};
|
||||||
|
|
||||||
private:
|
[[nodiscard]] auto solved() const noexcept -> bool;
|
||||||
std::array<std::array<Card, MAX_COLUMN_SIZE>, MAX_ROW_SIZE> field;
|
[[nodiscard]] auto hash() const noexcept -> std::size_t;
|
||||||
std::array<BunkerField, 3> bunker;
|
[[nodiscard]] auto correct() const noexcept -> bool;
|
||||||
std::array<std::optional<Card>, 3> goal;
|
|
||||||
bool flower_gone;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace solitaire
|
} // namespace solitaire
|
||||||
15
shenzhen_solitaire/c++/include/card.hpp
Normal file
15
shenzhen_solitaire/c++/include/card.hpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cassert>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace solitaire {
|
||||||
|
enum class CardType : int { Zhong, Bai, Fa, Hua, Red, Green, Black };
|
||||||
|
|
||||||
|
auto isNormalCardType(CardType type) -> bool;
|
||||||
|
|
||||||
|
struct Card {
|
||||||
|
CardType type;
|
||||||
|
int value;
|
||||||
|
[[nodiscard]] auto toString() const noexcept -> std::string;
|
||||||
|
};
|
||||||
|
} // namespace solitaire
|
||||||
20
shenzhen_solitaire/c++/include/goal.hpp
Normal file
20
shenzhen_solitaire/c++/include/goal.hpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "card.hpp"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
namespace solitaire {
|
||||||
|
class Goal {
|
||||||
|
std::array<std::optional<Card>, 3> goal{};
|
||||||
|
auto getEmptyId() -> std::optional<int>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
[[nodiscard]] auto getId(CardType suit) const noexcept -> std::optional<int>;
|
||||||
|
[[nodiscard]] auto get(CardType suit) const noexcept -> std::optional<int>;
|
||||||
|
|
||||||
|
void set(CardType suit, int value) noexcept;
|
||||||
|
void inc(CardType suit) noexcept;
|
||||||
|
};
|
||||||
|
} // namespace solitaire
|
||||||
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