diff --git a/bulls-and-cows-cpp/CGame.cpp b/bulls-and-cows-cpp/CGame.cpp index 29b71bd..ff4e6ff 100644 --- a/bulls-and-cows-cpp/CGame.cpp +++ b/bulls-and-cows-cpp/CGame.cpp @@ -1,12 +1,15 @@ -// CGame.cpp +// CGame.cpp // #include "CGame.h" +//#include +#include #include +#include #include -// Initialize the variables void CGame::Init() { + m_fGameIsEnd = false; m_ucBulls = 0; m_ucCows = 0; m_uStepCounter = 0; @@ -23,29 +26,112 @@ void CGame::Init() n = d(e); if (i > 0) { - while (CGame::IsEqual(n, i)) + while (IsEqual(n, i)) { n = d(e); } } m_uGuessedNumber[i] = n; } - - } -void CGame::ShowGuessedNumber() +void CGame::Start() { + CStep step; + int ret = 0; + + while (!m_fGameIsEnd) + { + do + { + PrintGameHeader(); + ShowGuessedNumber(false); + PrintSteps(); + ret = GetNumber(step); + if (ret == -1) return; + } while (ret < 0); + } + + PrintGameHeader(); + ShowGuessedNumber(true); + PrintSteps(); + PrintGameFooter(); +} + +void CGame::ShowGuessedNumber(bool show) +{ + std::wcout << '\t'; for (int i = 0; i < m_ucDigits; ++i) { - std::cout << m_uGuessedNumber[i]; + show ? std::wcout << m_uGuessedNumber[i] : std::wcout << '#'; } - std::cout << std::endl; + std::wcout << '\n' << '\t'; + + for (int i = 0; i < m_ucDigits; ++i) + { + std::wcout << '-'; + } + std::wcout << std::endl; } void CGame::PrintGameHeader() { - std::wcout << L" . " << VERSION << std::endl; + 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; +} + +void CGame::PrintGameFooter() +{ + std::wcout << L"\n\n\t" << L"!!! П О Б Е Д А !!!" << L"\n\n" << std::endl; +} + +void CGame::PrintSteps() +{ + if (m_Steps.size() < 1) return; + + for (CStep& s : m_Steps) + { + std::wstring stepNumber = L"\0"; + for (int& d : s.GetStepNumber()) + { + stepNumber += std::to_wstring(d); + } + std::wcout << '\t' << stepNumber << '\t' << s.GetStepAnimals().first << L" Б., " << s.GetStepAnimals().second << L" К." << std::endl; + } +} + +int CGame::GetNumber(CStep step) +{ + std::vector digits; + std::wstring sNumber; + + 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) + { + int r = std::iswdigit(c); + if (!(bool)r) + { + return -2; + } + else + { + int r = ((int)c) - 48; + digits.push_back(r); + } + } + + step.StoreStepNumber(digits); + step.CheckForAnimals(m_uGuessedNumber, m_ucDigits); + m_Steps.push_back(step); + if (step.GetStepAnimals().first == 4) m_fGameIsEnd = true; + + return 0; } bool CGame::IsEqual(int n, int j) @@ -61,7 +147,7 @@ bool CGame::IsEqual(int n, int j) return result; } -CGame::CGame(int digits) +CGame::CGame(int d) { - m_ucDigits = digits; + m_ucDigits = d; } diff --git a/bulls-and-cows-cpp/CGame.h b/bulls-and-cows-cpp/CGame.h index 54eabe4..031161f 100644 --- a/bulls-and-cows-cpp/CGame.h +++ b/bulls-and-cows-cpp/CGame.h @@ -2,7 +2,6 @@ // #pragma once #include "CStep.h" -#include #define VERSION 0.1 @@ -10,18 +9,23 @@ class CGame { private: unsigned char m_ucDigits; - int m_uStepCounter; - int* m_uGuessedNumber; - std::vector m_Step; - - bool IsEqual(int, int); - -public: unsigned char m_ucBulls; unsigned char m_ucCows; + bool m_fGameIsEnd; + int m_uStepCounter; + int* m_uGuessedNumber; + std::vector m_Steps; - void Init(); - void ShowGuessedNumber(); + bool IsEqual(int, int); + int GetNumber(CStep); + void ShowGuessedNumber(bool); void PrintGameHeader(); + void PrintGameFooter(); + void PrintSteps(); + +public: + void Init(); + void Start(); + CGame(int); }; diff --git a/bulls-and-cows-cpp/CStep.cpp b/bulls-and-cows-cpp/CStep.cpp index e2d70da..6010b20 100644 --- a/bulls-and-cows-cpp/CStep.cpp +++ b/bulls-and-cows-cpp/CStep.cpp @@ -2,3 +2,44 @@ // #include "CStep.h" +void CStep::StoreStepNumber(std::vector& digits) +{ + m_uStepNumber = digits; +} + +void CStep::CheckForAnimals(const int* digits, int size) +{ + int b = 0, c = 0; + + for (size_t i = 0; i < size; i++) // m_uGuessedNumber + { + for (size_t j = 0; j < size; j++) // m_uStepNumber + { + if ((m_uStepNumber[i] == digits[j]) && (i == j)) + { + b++; + } + else if (m_uStepNumber[i] == digits[j]) + { + c++; + } + + } + } + m_uStepAnimals.first = b; + m_uStepAnimals.second = c; +} + +std::vector CStep::GetStepNumber() +{ + return m_uStepNumber; +} + +std::pair CStep::GetStepAnimals() +{ + return m_uStepAnimals; +} + +CStep::CStep() +{ +} diff --git a/bulls-and-cows-cpp/CStep.h b/bulls-and-cows-cpp/CStep.h index d899601..471272d 100644 --- a/bulls-and-cows-cpp/CStep.h +++ b/bulls-and-cows-cpp/CStep.h @@ -1,11 +1,18 @@ // CStep.h // #pragma once -#include +#include class CStep { +private: + std::vector m_uStepNumber; + std::pair m_uStepAnimals; + public: - std::tuple m_uStepNumber; - std::tuple m_ucStepAnimals; + void StoreStepNumber(std::vector&); + void CheckForAnimals(const int*, int); + std::vector GetStepNumber(); + std::pair GetStepAnimals(); + CStep(); }; diff --git a/bulls-and-cows-cpp/bulls-and-cows.cpp b/bulls-and-cows-cpp/bulls-and-cows.cpp index 775c7a4..facb4f4 100644 --- a/bulls-and-cows-cpp/bulls-and-cows.cpp +++ b/bulls-and-cows-cpp/bulls-and-cows.cpp @@ -3,6 +3,7 @@ #include #include "..\..\..\MyFunctions\SetUserLocale.h" #include "CGame.h" +#include int wmain(int argc, wchar_t* argv[]) { @@ -10,10 +11,7 @@ int wmain(int argc, wchar_t* argv[]) CGame game(4); game.Init(); - game.PrintGameHeader(); - - - + game.Start(); return 0; }