diff --git a/bulls-and-cows-cpp/CGame.cpp b/bulls-and-cows-cpp/CGame.cpp index 9217bdb..7e152c3 100644 --- a/bulls-and-cows-cpp/CGame.cpp +++ b/bulls-and-cows-cpp/CGame.cpp @@ -1,14 +1,11 @@ // CGame.cpp // #include "CGame.h" -#include +#include "CUserInput.h" #include -#include -#include -void CGame::Init(int digits) +void CGame::Init() { - m_ucDigits = digits; m_fGameIsEnd = false; m_ucBulls = 0; m_ucCows = 0; @@ -46,7 +43,7 @@ void CGame::Start() do { PrintGameHeader(); - ShowGuessedNumber(true); + ShowGuessedNumber(false); PrintSteps(); ret = GetNumber(step); switch (ret) @@ -62,11 +59,6 @@ void CGame::Start() break; } case -3: - { - m_sLastError = L"Вы должны ввести число!"; - break; - } - case -4: { m_sLastError = L"Цифры в числе не должны повторяться!"; break; @@ -106,9 +98,8 @@ void CGame::ShowGuessedNumber(bool show) void CGame::PrintGameHeader() { std::system("cls"); - std::wcout << L"Быки и Коровы. Версия " << VERSION << std::endl; - std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << std::endl; - std::wcout << std::endl; + std::wcout << strGAMENAME << ' ' << strGAMEVERSION << ' ' << VERSION << std::endl; + std::wcout << strGAMECOPYRIGHT << '\n' << std::endl; } void CGame::PrintGameFooter() @@ -150,13 +141,13 @@ int CGame::GetNumber(CStep step) int r = std::iswdigit(c); if (!(bool)r) { - return -3; + return -2; } else { if (IsEqual(sNumber, c, i)) { - return -4; + return -3; } digits.push_back(((int)c) - 48); // Convert from char 'n' to number n } @@ -165,11 +156,40 @@ int CGame::GetNumber(CStep step) step.StoreStepNumber(digits); step.CheckForAnimals(m_uGuessedNumber, m_ucDigits); m_Steps.push_back(step); - if (step.GetStepAnimals().first == 4) m_fGameIsEnd = true; + if (step.GetStepAnimals().first == m_ucDigits) m_fGameIsEnd = true; return 0; } +CGame::CGame() +{ + CUserInput userInput(1); + + bool ok = false; + + do + { + PrintGameHeader(); + std::wcout << L"Введите количество цифр в числе (от 3 до 7).\n"; + std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n"; + std::wcout << L"> "; + if (userInput.Get()) + { + if (userInput.m_vUserInput.at(0) >= 3 && userInput.m_vUserInput.at(0) <= 7) + { + m_ucDigits = userInput.m_vUserInput.at(0); + ok = true; + } + else + { + userInput.m_vUserInput.clear(); + } + } + } while (!ok); + + std::wcout << L"m_ucDigits: " << m_ucDigits << std::endl; +} + CGame::~CGame() { delete[] m_uGuessedNumber; diff --git a/bulls-and-cows-cpp/CGame.h b/bulls-and-cows-cpp/CGame.h index 9e0f7c1..03c2c07 100644 --- a/bulls-and-cows-cpp/CGame.h +++ b/bulls-and-cows-cpp/CGame.h @@ -1,15 +1,14 @@ // CGame.h // #pragma once +#include "defines.h" +#include "CUserInput.h" #include "CStep.h" -#include - -#define VERSION 0.2 class CGame { private: - int m_ucDigits; + int m_ucDigits = 4; // Number of digits into the guessed number int m_ucBulls; int m_ucCows; bool m_fGameIsEnd; @@ -18,25 +17,19 @@ private: std::wstring m_sLastError; std::vector m_Steps; + int GetNumber(CStep); + void ShowGuessedNumber(bool); + void PrintGameHeader(); + void PrintGameFooter(); + void PrintSteps(); + template bool IsEqual(const T, const N, int); - int GetNumber(CStep); - - void ShowGuessedNumber(bool); - - void PrintGameHeader(); - - void PrintGameFooter(); - - void PrintSteps(); - public: - - void Init(int); - + void Init(); void Start(); - + CGame(); ~CGame(); }; @@ -47,7 +40,7 @@ bool CGame::IsEqual(const T number, const N digit, int i) while ((--i) >= 0) { - if (digit == number[i]) result = true; + if (digit == number.at(i)) result = true; } return result; } diff --git a/bulls-and-cows-cpp/CUserInput.cpp b/bulls-and-cows-cpp/CUserInput.cpp new file mode 100644 index 0000000..6801a1f --- /dev/null +++ b/bulls-and-cows-cpp/CUserInput.cpp @@ -0,0 +1,40 @@ +// CUserInput.cpp +// +#include "CUserInput.h" + +CUserInput::CUserInput(int nNumOfDigits) +{ + m_nNumOfExpectedDigits = nNumOfDigits; +} + +int CUserInput::Get() +{ + std::wstring sDigits; + wchar_t cDigit; + int nDigit; + + std::getline(std::wcin, sDigits); + + if (sDigits == L"Q" || sDigits == L"q") + { + return -1; + } + + if (sDigits.length() != m_nNumOfExpectedDigits) + { + return -2; + } + + for (unsigned i = 0; i < m_nNumOfExpectedDigits; i++) + { + cDigit = sDigits.at(i); + if (!(bool)std::iswdigit(cDigit)) + { + m_vUserInput.clear(); + return -2; + } + nDigit = ((int)cDigit) - 48; + m_vUserInput.push_back(nDigit); + } + return 0; +} diff --git a/bulls-and-cows-cpp/CUserInput.h b/bulls-and-cows-cpp/CUserInput.h new file mode 100644 index 0000000..4c5e360 --- /dev/null +++ b/bulls-and-cows-cpp/CUserInput.h @@ -0,0 +1,18 @@ +// CUserInput.h +// +#pragma once +#include "defines.h" +#include +#include +#include +#include + +class CUserInput +{ +public: + int m_nNumOfExpectedDigits; + std::vector m_vUserInput; + + CUserInput(int nNumOfExpectedDigits); + int Get(); +}; diff --git a/bulls-and-cows-cpp/bulls-and-cows-cpp.vcxproj b/bulls-and-cows-cpp/bulls-and-cows-cpp.vcxproj index 9ef9cf3..37e2534 100644 --- a/bulls-and-cows-cpp/bulls-and-cows-cpp.vcxproj +++ b/bulls-and-cows-cpp/bulls-and-cows-cpp.vcxproj @@ -81,6 +81,7 @@ Console true + false @@ -133,11 +134,14 @@ + + + diff --git a/bulls-and-cows-cpp/bulls-and-cows-cpp.vcxproj.filters b/bulls-and-cows-cpp/bulls-and-cows-cpp.vcxproj.filters index efee5fb..a16d925 100644 --- a/bulls-and-cows-cpp/bulls-and-cows-cpp.vcxproj.filters +++ b/bulls-and-cows-cpp/bulls-and-cows-cpp.vcxproj.filters @@ -27,6 +27,9 @@ Source Files + + Source Files + @@ -38,5 +41,11 @@ Header Files + + Header Files + + + Header Files + \ No newline at end of file diff --git a/bulls-and-cows-cpp/bulls-and-cows.cpp b/bulls-and-cows-cpp/bulls-and-cows.cpp index 292fe84..0d9f095 100644 --- a/bulls-and-cows-cpp/bulls-and-cows.cpp +++ b/bulls-and-cows-cpp/bulls-and-cows.cpp @@ -3,15 +3,13 @@ #include #include "..\..\..\MyFunctions\SetUserLocale.h" #include "CGame.h" -//#include int wmain(int argc, wchar_t* argv[]) { if (!SetUserLocale()) return 1; CGame game; - - game.Init(4); + game.Init(); game.Start(); return 0; diff --git a/bulls-and-cows-cpp/defines.h b/bulls-and-cows-cpp/defines.h new file mode 100644 index 0000000..0d32c05 --- /dev/null +++ b/bulls-and-cows-cpp/defines.h @@ -0,0 +1,6 @@ +#pragma once + +#define VERSION 0.2 +#define strGAMENAME L" ." +#define strGAMEVERSION L"" +#define strGAMECOPYRIGHT L"Copyright (c) 2023 by W0LF aka 'dreamforce'"