From 6aeb8fccbdb7c63da0c6092a4f3c89703450ee8d Mon Sep 17 00:00:00 2001 From: QingZhou <1565190688@qq.com> Date: Mon, 14 Oct 2024 02:38:22 +0800 Subject: [PATCH] New_feature: hightlight conflict numbers. --- src/block.cpp | 8 ++++++++ src/common.h | 1 + src/i18n.cpp | 2 ++ src/i18n.h | 1 + src/scene.cpp | 20 ++++++++++++++++++-- 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/block.cpp b/src/block.cpp index 89b00fa..eed1942 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -54,6 +54,11 @@ void CBlock::print() const { << Color::Modifier() << " " << Color::Modifier(Color::BOLD, Color::BG_DEFAULT, Color::FG_RED) << PIPE << Color::Modifier() << " "; + else if (number.state == State::CONFLICT) + std::cout << Color::Modifier(Color::BOLD, Color::BG_DEFAULT, Color::FG_RED) << number.value + << Color::Modifier() << " " + << Color::Modifier(Color::BOLD, Color::BG_DEFAULT, Color::FG_RED) << PIPE + << Color::Modifier() << " "; else std::cout << number.value << " " << Color::Modifier(Color::BOLD, Color::BG_DEFAULT, Color::FG_RED) << PIPE << Color::Modifier() << " "; @@ -65,6 +70,9 @@ void CBlock::print() const { if (number.state == State::ERASED) std::cout << Color::Modifier(Color::BOLD, Color::BG_DEFAULT, Color::FG_GREEN) << number.value << Color::Modifier() << " " << PIPE << " "; + else if (number.state == State::CONFLICT) + std::cout << Color::Modifier(Color::BOLD, Color::BG_DEFAULT, Color::FG_RED) << number.value + << Color::Modifier() << " " << PIPE << " "; else std::cout << number.value << " " << PIPE << " "; } diff --git a/src/common.h b/src/common.h index 697f4d1..7cd7292 100644 --- a/src/common.h +++ b/src/common.h @@ -14,6 +14,7 @@ enum class State : int { INITED = 0, ERASED, + CONFLICT, }; enum class KeyMode : int diff --git a/src/i18n.cpp b/src/i18n.cpp index a935bee..cb9e717 100644 --- a/src/i18n.cpp +++ b/src/i18n.cpp @@ -15,6 +15,7 @@ I18n::Dict english = { {I18n::Key::CONGRATULATION, "Congratulation! You Win!"}, {I18n::Key::NOT_COMPLETED, "Sorry, not completed."}, {I18n::Key::ASK_DIFFICULTY, "Select difficulty: 1 Easy 2 Normal 3 Hard"}, + {I18n::Key::CONFLICT, "Warning! Conflict exists!"} }; I18n::Dict chinese = { {I18n::Key::ASK_KEY_MAP, "设置按键模式: 1 WASD 2 VIM"}, @@ -29,6 +30,7 @@ I18n::Dict chinese = { {I18n::Key::CONGRATULATION, "恭喜! 你解开了谜题!"}, {I18n::Key::NOT_COMPLETED, "对不起, 还未完成"}, {I18n::Key::ASK_DIFFICULTY, "设置难度: 1简单 2普通 3困难"}, + {I18n::Key::CONFLICT, "警告!存在冲突!"} }; // Default English diff --git a/src/i18n.h b/src/i18n.h index 7a96084..9d24624 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -25,6 +25,7 @@ class I18n { CONGRATULATION, NOT_COMPLETED, ASK_DIFFICULTY, + CONFLICT }; using Dict = std::map; diff --git a/src/scene.cpp b/src/scene.cpp index f600dd8..2d4d7c7 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -119,7 +119,7 @@ void CScene::init() bool CScene::setCurValue(const int nCurValue, int &nLastValue) { auto point = _map[_cur_point.x + _cur_point.y * 9]; - if (point.state == State::ERASED) + if (point.state == State::ERASED || point.state == State::CONFLICT) { nLastValue = point.value; setValue(nCurValue); @@ -138,6 +138,12 @@ void CScene::setValue(const int value) { auto p = _cur_point; this->setValue(p, value); + if (!_column_block[_cur_point.x].isValid() || !_row_block[_cur_point.y].isValid() || !_xy_block[_cur_point.y/3][_cur_point.x/3].isValid()){ + _map[_cur_point.x + _cur_point.y * 9].state = State::CONFLICT; + } + else { + _map[_cur_point.x + _cur_point.y * 9].state = State::ERASED; + } } // 选择count个格子清空 @@ -260,7 +266,17 @@ void CScene::play() else { _vCommand.push_back(std::move(oCommand)); // XXX: move without move constructor + int is_conflict = 0; + if (!_column_block[_cur_point.x].isValid() || !_row_block[_cur_point.y].isValid() || !_xy_block[_cur_point.y/3][_cur_point.x/3].isValid()) { + _map[_cur_point.x + _cur_point.y * 9].state = State::CONFLICT; + is_conflict = 1; + } + else { + _map[_cur_point.x + _cur_point.y * 9].state = State::ERASED; + } show(); + if (is_conflict) + message(I18n::Instance().Get(I18n::Key::CONFLICT)); continue; } } @@ -378,7 +394,7 @@ void CScene::generate() bool CScene::setPointValue(const point_t &stPoint, const int nValue) { auto point = _map[stPoint.x + stPoint.y * 9]; - if (State::ERASED == point.state) + if (State::ERASED == point.state || State::CONFLICT == point.state) { _cur_point = stPoint; setValue(nValue);