mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-28 16:02:46 +03:00
Added the ability to set the number of digits in the guessed number.
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
// CGame.cpp
|
||||
//
|
||||
#include "CGame.h"
|
||||
#include <string>
|
||||
#include "CUserInput.h"
|
||||
#include <random>
|
||||
#include <cwctype>
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
// CGame.h
|
||||
//
|
||||
#pragma once
|
||||
#include "defines.h"
|
||||
#include "CUserInput.h"
|
||||
#include "CStep.h"
|
||||
#include <string>
|
||||
|
||||
#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<CStep> m_Steps;
|
||||
|
||||
int GetNumber(CStep);
|
||||
void ShowGuessedNumber(bool);
|
||||
void PrintGameHeader();
|
||||
void PrintGameFooter();
|
||||
void PrintSteps();
|
||||
|
||||
template<typename T, typename N>
|
||||
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;
|
||||
}
|
||||
|
||||
40
bulls-and-cows-cpp/CUserInput.cpp
Normal file
40
bulls-and-cows-cpp/CUserInput.cpp
Normal file
@@ -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;
|
||||
}
|
||||
18
bulls-and-cows-cpp/CUserInput.h
Normal file
18
bulls-and-cows-cpp/CUserInput.h
Normal file
@@ -0,0 +1,18 @@
|
||||
// CUserInput.h
|
||||
//
|
||||
#pragma once
|
||||
#include "defines.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cwctype>
|
||||
|
||||
class CUserInput
|
||||
{
|
||||
public:
|
||||
int m_nNumOfExpectedDigits;
|
||||
std::vector<int> m_vUserInput;
|
||||
|
||||
CUserInput(int nNumOfExpectedDigits);
|
||||
int Get();
|
||||
};
|
||||
@@ -81,6 +81,7 @@
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<RandomizedBaseAddress>false</RandomizedBaseAddress>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
@@ -133,11 +134,14 @@
|
||||
<ClCompile Include="bulls-and-cows.cpp" />
|
||||
<ClCompile Include="CGame.cpp" />
|
||||
<ClCompile Include="CStep.cpp" />
|
||||
<ClCompile Include="CUserInput.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\..\MyFunctions\SetUserLocale.h" />
|
||||
<ClInclude Include="CGame.h" />
|
||||
<ClInclude Include="CStep.h" />
|
||||
<ClInclude Include="CUserInput.h" />
|
||||
<ClInclude Include="defines.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -27,6 +27,9 @@
|
||||
<ClCompile Include="CStep.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CUserInput.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CGame.h">
|
||||
@@ -38,5 +41,11 @@
|
||||
<ClInclude Include="CStep.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CUserInput.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="defines.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -3,15 +3,13 @@
|
||||
#include <Windows.h>
|
||||
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
||||
#include "CGame.h"
|
||||
//#include <iostream>
|
||||
|
||||
int wmain(int argc, wchar_t* argv[])
|
||||
{
|
||||
if (!SetUserLocale()) return 1;
|
||||
|
||||
CGame game;
|
||||
|
||||
game.Init(4);
|
||||
game.Init();
|
||||
game.Start();
|
||||
|
||||
return 0;
|
||||
|
||||
6
bulls-and-cows-cpp/defines.h
Normal file
6
bulls-and-cows-cpp/defines.h
Normal file
@@ -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'"
|
||||
Reference in New Issue
Block a user