diff --git a/bulls-and-cows-cpp/CGame.cpp b/bulls-and-cows-cpp/CGame.cpp index 65f3c86..8afe4fd 100644 --- a/bulls-and-cows-cpp/CGame.cpp +++ b/bulls-and-cows-cpp/CGame.cpp @@ -26,7 +26,7 @@ void CGame::Init(int digits) n = d(e); if (i > 0) { - while (IsEqual(n, i)) + while (IsEqual1(m_uGuessedNumber, n, i)) { n = d(e); } @@ -108,23 +108,28 @@ int CGame::GetNumber(CStep step) { std::vector digits; std::wstring sNumber; + wchar_t c; std::wcout << L"=>\t"; std::wcin >> sNumber; if (sNumber == L"Q" || sNumber == L"q") return -1; if (sNumber.length() != m_ucDigits) return -2; - for (wchar_t& c : sNumber) + for (int i = 0; i < sNumber.length(); i++) { + c = sNumber[i]; int r = std::iswdigit(c); if (!(bool)r) { - return -2; + return -3; } else { - int r = ((int)c) - 48; - digits.push_back(r); + if (IsEqual2(sNumber, c, i)) + { + return -4; + } + digits.push_back(((int)c) - 48); // Convert from char 'n' to number n } } @@ -136,14 +141,27 @@ int CGame::GetNumber(CStep step) return 0; } -bool CGame::IsEqual(int n, int j) +bool CGame::IsEqual1(const int* a, const int n, int j) { j--; bool result = false; while (j >= 0) { - if (n == m_uGuessedNumber[j]) result = true; + if (n == a[j]) result = true; + j--; + } + return result; +} + +bool CGame::IsEqual2(const std::wstring& a, const wchar_t n, int j) +{ + j--; + bool result = false; + + while (j >= 0) + { + if (n == a[j]) result = true; j--; } return result; diff --git a/bulls-and-cows-cpp/CGame.h b/bulls-and-cows-cpp/CGame.h index b1ccd4a..74dcd7e 100644 --- a/bulls-and-cows-cpp/CGame.h +++ b/bulls-and-cows-cpp/CGame.h @@ -2,28 +2,38 @@ // #pragma once #include "CStep.h" +#include #define VERSION 0.1 class CGame { private: - unsigned char m_ucDigits; - unsigned char m_ucBulls; - unsigned char m_ucCows; + int m_ucDigits; + int m_ucBulls; + int m_ucCows; bool m_fGameIsEnd; int m_uStepCounter; int* m_uGuessedNumber; std::vector m_Steps; - bool IsEqual(int, int); + bool IsEqual1(const int*, const int, int); + + bool IsEqual2(const std::wstring&, const wchar_t, int); + int GetNumber(CStep); + void ShowGuessedNumber(bool); + void PrintGameHeader(); + void PrintGameFooter(); + void PrintSteps(); public: + void Init(int); + void Start(); };