From f2d270cd29596309fca601b4e56cf584cb644ac9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 26 Feb 2023 13:27:14 +0800 Subject: [PATCH] feat: demo of horizontal mirror --- src/klotski_core/ffi/tmain.cc | 5 +++ src/klotski_core/raw_code/mirror.cc | 60 +++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index 59901e6..9790602 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -14,6 +14,11 @@ void tmain() { printf("tmain start\n"); +// klotski::RawCode::from_common_code(0x1A9BF0C00).is_horizontal_mirror(); + klotski::RawCode::from_common_code(0x1A9BF0C00).to_horizontal_mirror(); + + return; + // printf("%d\n", ALL_CASES_SIZE_SUM); // std::cout << ALL_CASES_SIZE_SUM << std::endl; diff --git a/src/klotski_core/raw_code/mirror.cc b/src/klotski_core/raw_code/mirror.cc index 16e942c..64eb28f 100644 --- a/src/klotski_core/raw_code/mirror.cc +++ b/src/klotski_core/raw_code/mirror.cc @@ -1,4 +1,5 @@ #include "raw_code.h" +#include "common.h" using klotski::RawCode; @@ -40,6 +41,37 @@ bool RawCode::is_horizontal_mirror(const RawCode &raw_code) const { /// ----------------------------- Basic Functions ----------------------------- +#include + +void horizontal_fill(uint64_t &raw_code) { + for (int addr = 0; addr < 60; addr += 3) { // traverse every 3-bits + switch ((raw_code >> addr) & 0b111) { + case B_1x2: + raw_code &= ~(uint64_t(~B_1x2 & 0b111) << (addr + 3)); // fill horizontal mirror + addr += 3; // skip next address + break; + case B_2x2: + raw_code &= ~(uint64_t(~B_2x2 & 0b111) << (addr + 3)); // fill horizontal mirror + addr += 3; // skip next address + break; + } + } +} + +void horizontal_clear(uint64_t &raw_code) { + + for (int addr = 0; addr < 60; addr += 3) { // traverse every 3-bits + + switch ((raw_code >> addr) & 0b111) { + case B_1x2: + case B_2x2: + raw_code |= (uint64_t)0b111 << (addr + 3); + } + + } + +} + uint64_t RawCode::vertical_mirror(uint64_t raw_code) { // TODO: vertical mirror convert @@ -51,7 +83,17 @@ uint64_t RawCode::horizontal_mirror(uint64_t raw_code) { // TODO: horizontal mirror convert - return 0; + // fill -> mirror -> unfill + + horizontal_fill(raw_code); + + // flip raw code + + horizontal_clear(raw_code); + + std::cout << RawCode::unsafe_create(raw_code) << std::endl; + + return raw_code; } bool RawCode::vertical_mirror_check(uint64_t raw_code) { @@ -61,9 +103,19 @@ bool RawCode::vertical_mirror_check(uint64_t raw_code) { return false; } -bool RawCode::horizontal_mirror_check(uint64_t raw_code) { - // TODO: whether self horizontal mirror - return false; + +bool RawCode::horizontal_mirror_check(uint64_t raw_code) { + /// MASK_A MASK_B + /// 111 000 000 000 | 000 111 000 000 + /// 111 000 000 000 | 000 111 000 000 + /// 111 000 000 000 | 000 111 000 000 + /// 111 000 000 000 | 000 111 000 000 + /// 111 000 000 000 | 000 111 000 000 + constexpr uint64_t MASK_A = 0x0'007'007'007'007'007; + constexpr uint64_t MASK_B = 0x0'038'038'038'038'038; + + horizontal_fill(raw_code); + return ((raw_code >> 9 ^ raw_code) & MASK_A) && ((raw_code >> 3 ^ raw_code) & MASK_B); }