From 8197a648034afe753acd59e1e2f7384ff38b0365 Mon Sep 17 00:00:00 2001 From: W0LF Date: Fri, 4 Aug 2023 13:55:27 +0300 Subject: [PATCH] Refactored GetNumber() method. --- bulls-and-cows-cpp/CGame.cpp | 44 +++++++----------------------------- bulls-and-cows-cpp/CGame.h | 2 +- bulls-and-cows-cpp/defines.h | 6 +++++ 3 files changed, 15 insertions(+), 37 deletions(-) diff --git a/bulls-and-cows-cpp/CGame.cpp b/bulls-and-cows-cpp/CGame.cpp index 3bfbd42..a4820ac 100644 --- a/bulls-and-cows-cpp/CGame.cpp +++ b/bulls-and-cows-cpp/CGame.cpp @@ -42,38 +42,10 @@ void CGame::Start() do { PrintGameHeader(); -#ifdef _DEBUG - ShowGuessedNumber(true); -#else - ShowGuessedNumber(false); -#endif // _DEBUG + ShowGuessedNumber(IS_SHOW); PrintSteps(); + } while (!GetNumber(step)); - ret = GetNumber(step); - switch (ret) - { - case -1: - { - return; - break; - } - case -2: - { - //m_sLastError = L"Вы должны ввести " + std::to_wstring(m_ucDigits) + L"-значное число!"; - break; - } - case -3: - { - //m_sLastError = L"Цифры в числе не должны повторяться!"; - break; - } - default: - { - //m_sLastError = L""; - break; - } - } - } while (ret < 0); m_uStepCounter++; } @@ -83,13 +55,13 @@ void CGame::Start() PrintGameFooter(); } -int CGame::GetNumber(CStep step) +bool CGame::GetNumber(CStep step) { std::wcout << L"\t\t\t\t\t" << m_sLastError << L"\r"; std::wcout << L"=>\t"; CUserInput userInput(m_ucDigits); - + if (userInput.Get()) { for (unsigned i = 0; i < userInput.m_vUserInput.size(); i++) @@ -97,14 +69,14 @@ int CGame::GetNumber(CStep step) if (IsEqual(userInput.m_vUserInput, userInput.m_vUserInput.at(i), i)) { m_sLastError = L"Цифры в числе не должны повторяться!"; - return -3; + return false; } } } else { m_sLastError = L"Вы должны ввести " + std::to_wstring(m_ucDigits) + L"-значное число!"; - return -2; + return false; } step.StoreStepNumber(userInput.m_vUserInput); @@ -114,7 +86,7 @@ int CGame::GetNumber(CStep step) if (step.GetStepAnimals().first == m_ucDigits) m_fGameIsEnd = true; m_sLastError = L""; - return 0; + return true; } void CGame::ShowGuessedNumber(bool show) @@ -137,7 +109,7 @@ void CGame::PrintGameHeader() { std::system("cls"); std::wcout << strGAMENAME << ' ' << strGAMEVERSION << ' ' << VERSION << std::endl; - std::wcout << strGAMECOPYRIGHT << '\n' << std::endl; + std::wcout << strGAMECOPYRIGHT << "\n\n\n" << std::endl; } void CGame::PrintGameFooter() diff --git a/bulls-and-cows-cpp/CGame.h b/bulls-and-cows-cpp/CGame.h index 0f87f94..59ce92d 100644 --- a/bulls-and-cows-cpp/CGame.h +++ b/bulls-and-cows-cpp/CGame.h @@ -17,7 +17,7 @@ private: std::wstring m_sLastError; std::vector m_Steps; - int GetNumber(CStep); + bool GetNumber(CStep); void ShowGuessedNumber(bool); void PrintGameHeader(); void PrintGameFooter(); diff --git a/bulls-and-cows-cpp/defines.h b/bulls-and-cows-cpp/defines.h index f165750..658152a 100644 --- a/bulls-and-cows-cpp/defines.h +++ b/bulls-and-cows-cpp/defines.h @@ -6,3 +6,9 @@ #define strGAMENAME L" ." #define strGAMEVERSION L"" #define strGAMECOPYRIGHT L"Copyright (c) 2023 by W0LF aka 'dreamforce'" + +#ifdef _DEBUG +#define IS_SHOW true +#else +#define IS_SHOW false +#endif