mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 months ago
6 changed files with 223 additions and 0 deletions
@ -0,0 +1,71 @@ |
|||
/// Klotski Engine Python FFI by Dnomd343 @2024
|
|||
|
|||
#pragma once |
|||
|
|||
#include <vector> |
|||
#include <cstdint> |
|||
|
|||
#include "group/group.h" |
|||
|
|||
#include "py_cases.h" |
|||
|
|||
#include "py_codec.h" |
|||
|
|||
namespace klotski::ffi { |
|||
|
|||
using klotski::cases::Group; |
|||
using klotski::cases::GroupUnion; |
|||
|
|||
class PyGroup { |
|||
public: |
|||
// [[nodiscard]] Toward toward() const;
|
|||
|
|||
[[nodiscard]] uint32_t type_id() const; |
|||
|
|||
[[nodiscard]] uint32_t pattern_id() const; |
|||
|
|||
[[nodiscard]] std::string to_string() const; |
|||
|
|||
[[nodiscard]] PyCases cases() const; |
|||
|
|||
[[nodiscard]] uint32_t size() const; |
|||
|
|||
// [[nodiscard]] MirrorType mirror_type() const;
|
|||
|
|||
[[nodiscard]] bool is_vertical_mirror() const; |
|||
|
|||
[[nodiscard]] bool is_horizontal_mirror() const; |
|||
|
|||
[[nodiscard]] PyGroup to_vertical_mirror() const; |
|||
|
|||
[[nodiscard]] PyGroup to_horizontal_mirror() const; |
|||
|
|||
private: |
|||
Group group_; |
|||
}; |
|||
|
|||
class PyGroupUnion { |
|||
public: |
|||
PyGroupUnion(uint8_t type_id); |
|||
PyGroupUnion(PyShortCode short_code); |
|||
PyGroupUnion(PyCommonCode common_code); |
|||
|
|||
[[nodiscard]] uint32_t value() const; |
|||
|
|||
[[nodiscard]] uint32_t size() const; |
|||
|
|||
[[nodiscard]] uint32_t group_num() const; |
|||
|
|||
[[nodiscard]] uint32_t pattern_num() const; |
|||
|
|||
[[nodiscard]] uint32_t max_group_size() const; |
|||
|
|||
[[nodiscard]] PyCases cases() const; |
|||
|
|||
[[nodiscard]] std::vector<PyGroup> groups() const; |
|||
|
|||
private: |
|||
GroupUnion group_union_; |
|||
}; |
|||
|
|||
} // namespace klotski
|
@ -0,0 +1,39 @@ |
|||
#include "include/py_group.h" |
|||
|
|||
using namespace klotski::ffi; |
|||
|
|||
uint32_t PyGroup::type_id() const { |
|||
return group_.type_id(); |
|||
} |
|||
|
|||
uint32_t PyGroup::pattern_id() const { |
|||
return group_.pattern_id(); |
|||
} |
|||
|
|||
std::string PyGroup::to_string() const { |
|||
return group_.to_string(); |
|||
} |
|||
|
|||
PyCases PyGroup::cases() const { |
|||
return PyCases::from(group_.cases()); |
|||
} |
|||
|
|||
uint32_t PyGroup::size() const { |
|||
return group_.size(); |
|||
} |
|||
|
|||
bool PyGroup::is_vertical_mirror() const { |
|||
return group_.is_vertical_mirror(); |
|||
} |
|||
|
|||
bool PyGroup::is_horizontal_mirror() const { |
|||
return group_.is_horizontal_mirror(); |
|||
} |
|||
|
|||
PyGroup PyGroup::to_vertical_mirror() const { |
|||
return std::bit_cast<PyGroup>(group_.to_vertical_mirror()); |
|||
} |
|||
|
|||
PyGroup PyGroup::to_horizontal_mirror() const { |
|||
return std::bit_cast<PyGroup>(group_.to_horizontal_mirror()); |
|||
} |
@ -0,0 +1,58 @@ |
|||
#include "include/py_group.h" |
|||
|
|||
#include "include/py_exps.h" |
|||
|
|||
using klotski::ffi::PyGroupExp; |
|||
|
|||
using klotski::ffi::PyGroupUnion; |
|||
|
|||
static klotski::cases::GroupUnion convert(uint8_t type_id) { |
|||
auto group_union = klotski::cases::GroupUnion::create(type_id); |
|||
if (!group_union.has_value()) { |
|||
throw PyGroupExp(std::format("invalid type id -> {}", type_id)); |
|||
} |
|||
return group_union.value(); |
|||
} |
|||
|
|||
PyGroupUnion::PyGroupUnion(uint8_t type_id) : |
|||
group_union_(convert(type_id)) {} |
|||
|
|||
PyGroupUnion::PyGroupUnion(PyShortCode short_code) : |
|||
group_union_(GroupUnion::from_short_code(std::bit_cast<ShortCode>(short_code))) {} |
|||
|
|||
PyGroupUnion::PyGroupUnion(PyCommonCode common_code) : |
|||
group_union_(GroupUnion::from_common_code(std::bit_cast<CommonCode>(common_code))) {} |
|||
|
|||
uint32_t PyGroupUnion::value() const { |
|||
return group_union_.unwrap(); |
|||
} |
|||
|
|||
uint32_t PyGroupUnion::size() const { |
|||
return group_union_.size(); |
|||
} |
|||
|
|||
uint32_t PyGroupUnion::group_num() const { |
|||
return group_union_.group_num(); |
|||
} |
|||
|
|||
uint32_t PyGroupUnion::pattern_num() const { |
|||
return group_union_.pattern_num(); |
|||
} |
|||
|
|||
uint32_t PyGroupUnion::max_group_size() const { |
|||
return group_union_.max_group_size(); |
|||
} |
|||
|
|||
klotski::ffi::PyCases PyGroupUnion::cases() const { |
|||
auto cases = PyCases::from(group_union_.cases()); |
|||
return cases; |
|||
} |
|||
|
|||
std::vector<klotski::ffi::PyGroup> PyGroupUnion::groups() const { |
|||
std::vector<PyGroup> groups; |
|||
groups.reserve(group_num()); |
|||
for (auto group : group_union_.groups()) { |
|||
groups.emplace_back(std::bit_cast<PyGroup>(group)); |
|||
} |
|||
return groups; |
|||
} |
Loading…
Reference in new issue