4 Commits
v0.3 ... v0.4

Author SHA1 Message Date
5a4fa61ed2 Version 0.4 2023-08-04 14:19:23 +03:00
8197a64803 Refactored GetNumber() method. 2023-08-04 13:55:27 +03:00
e75d82e896 Reworked the user input in the game class. 2023-08-03 01:00:32 +03:00
7669dff7a6 Moved header's includes into separete header file. 2023-08-02 23:01:19 +03:00
11 changed files with 81 additions and 85 deletions

View File

@@ -1,8 +1,7 @@
// CGame.cpp // Bulls and Cows the game
// CGame.cpp
// //
#include "CGame.h" #include "CGame.h"
#include "CUserInput.h"
#include <random>
void CGame::Init() void CGame::Init()
{ {
@@ -36,6 +35,7 @@ void CGame::Init()
void CGame::Start() void CGame::Start()
{ {
CStep step; CStep step;
int ret = 0; int ret = 0;
while (!m_fGameIsEnd) while (!m_fGameIsEnd)
@@ -43,33 +43,10 @@ void CGame::Start()
do do
{ {
PrintGameHeader(); PrintGameHeader();
ShowGuessedNumber(false); ShowGuessedNumber(IS_SHOW);
PrintSteps(); PrintSteps();
ret = GetNumber(step); } while (!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++; m_uStepCounter++;
} }
@@ -79,6 +56,40 @@ void CGame::Start()
PrintGameFooter(); PrintGameFooter();
} }
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++)
{
if (IsEqual(userInput.m_vUserInput, userInput.m_vUserInput.at(i), i))
{
m_sLastError = L"Цифры в числе не должны повторяться!";
return false;
}
}
}
else
{
m_sLastError = L"Вы должны ввести " + std::to_wstring(m_ucDigits) + L"-значное число!";
return false;
}
step.StoreStepNumber(userInput.m_vUserInput);
step.CheckForAnimals(m_uGuessedNumber, m_ucDigits);
m_Steps.push_back(step);
if (step.GetStepAnimals().first == m_ucDigits) m_fGameIsEnd = true;
m_sLastError = L"";
return true;
}
void CGame::ShowGuessedNumber(bool show) void CGame::ShowGuessedNumber(bool show)
{ {
std::wcout << '\t'; std::wcout << '\t';
@@ -98,8 +109,8 @@ void CGame::ShowGuessedNumber(bool show)
void CGame::PrintGameHeader() void CGame::PrintGameHeader()
{ {
std::system("cls"); std::system("cls");
std::wcout << strGAMENAME << ' ' << strGAMEVERSION << ' ' << VERSION << std::endl; std::wcout << L"Быки и Коровы." << ' ' << L"Версия" << ' ' << VERSION << std::endl;
std::wcout << strGAMECOPYRIGHT << '\n' << std::endl; std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << "\n\n" << std::endl;
} }
void CGame::PrintGameFooter() void CGame::PrintGameFooter()
@@ -123,44 +134,6 @@ void CGame::PrintSteps()
} }
} }
int CGame::GetNumber(CStep step)
{
std::vector<int> digits;
std::wstring sNumber;
wchar_t c;
std::wcout << L"\t\t\t\t\t" << m_sLastError << L"\r";
std::wcout << L"=>\t";
std::wcin >> sNumber;
if (sNumber == L"Q" || sNumber == L"q") return -1;
if (sNumber.length() != m_ucDigits) return -2;
for (int i = 0; i < sNumber.length(); i++)
{
c = sNumber[i];
int r = std::iswdigit(c);
if (!(bool)r)
{
return -2;
}
else
{
if (IsEqual(sNumber, c, i))
{
return -3;
}
digits.push_back(((int)c) - 48); // Convert from char 'n' to number n
}
}
step.StoreStepNumber(digits);
step.CheckForAnimals(m_uGuessedNumber, m_ucDigits);
m_Steps.push_back(step);
if (step.GetStepAnimals().first == m_ucDigits) m_fGameIsEnd = true;
return 0;
}
CGame::CGame() CGame::CGame()
{ {
CUserInput userInput(1); CUserInput userInput(1);
@@ -170,13 +143,13 @@ CGame::CGame()
do do
{ {
PrintGameHeader(); PrintGameHeader();
std::wcout << L"Введите количество цифр в числе (от 3 до 7).\n"; std::wcout << L"Введите количество цифр в числе (от 3 до 5).\n";
std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n"; std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n\n";
std::wcout << L"=> "; std::wcout << L"=> ";
if (userInput.Get()) if (userInput.Get())
{ {
if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 7)) if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 5))
{ {
m_ucDigits = userInput.m_vUserInput.at(0); m_ucDigits = userInput.m_vUserInput.at(0);
ok = true; ok = true;

View File

@@ -1,7 +1,8 @@
// Bulls and Cows the game
// CGame.h // CGame.h
// //
#pragma once #pragma once
#include "defines.h" #include "headers.h"
#include "CUserInput.h" #include "CUserInput.h"
#include "CStep.h" #include "CStep.h"
@@ -17,7 +18,7 @@ private:
std::wstring m_sLastError; std::wstring m_sLastError;
std::vector<CStep> m_Steps; std::vector<CStep> m_Steps;
int GetNumber(CStep); bool GetNumber(CStep);
void ShowGuessedNumber(bool); void ShowGuessedNumber(bool);
void PrintGameHeader(); void PrintGameHeader();
void PrintGameFooter(); void PrintGameFooter();

View File

@@ -1,3 +1,4 @@
// Bulls and Cows the game
// CStep.cpp // CStep.cpp
// //
#include "CStep.h" #include "CStep.h"

View File

@@ -1,7 +1,8 @@
// Bulls and Cows the game
// CStep.h // CStep.h
// //
#pragma once #pragma once
#include <vector> #include "headers.h"
class CStep class CStep
{ {

View File

@@ -1,3 +1,4 @@
// Bulls and Cows the game
// CUserInput.cpp // CUserInput.cpp
// //
#include "CUserInput.h" #include "CUserInput.h"

View File

@@ -1,11 +1,8 @@
// Bulls and Cows the game
// CUserInput.h // CUserInput.h
// //
#pragma once #pragma once
#include "defines.h" #include "headers.h"
#include <iostream>
#include <string>
#include <vector>
#include <cwctype>
class CUserInput class CUserInput
{ {

View File

@@ -142,6 +142,7 @@
<ClInclude Include="CStep.h" /> <ClInclude Include="CStep.h" />
<ClInclude Include="CUserInput.h" /> <ClInclude Include="CUserInput.h" />
<ClInclude Include="defines.h" /> <ClInclude Include="defines.h" />
<ClInclude Include="headers.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@@ -47,5 +47,8 @@
<ClInclude Include="defines.h"> <ClInclude Include="defines.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="headers.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,6 +1,7 @@
// Bulls and Cows the game
// bulls-and-cows.cpp // bulls-and-cows.cpp
// //
#include <Windows.h> #include "headers.h"
#include "..\..\..\MyFunctions\SetUserLocale.h" #include "..\..\..\MyFunctions\SetUserLocale.h"
#include "CGame.h" #include "CGame.h"

View File

@@ -1,6 +1,11 @@
// Bulls and Cows the game
// defines.h
//
#pragma once #pragma once
#define VERSION 0.4
#define VERSION 0.3 #ifdef _DEBUG
#define strGAMENAME L"Áûêè è Êîðîâû." #define IS_SHOW true
#define strGAMEVERSION L"Âåðñèÿ" #else
#define strGAMECOPYRIGHT L"Copyright (c) 2023 by W0LF aka 'dreamforce'" #define IS_SHOW false
#endif

View File

@@ -0,0 +1,12 @@
// Bulls and Cows the game
// headers.h
//
#pragma once
#include <Windows.h>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#include <cwctype>
#include "defines.h"