mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-29 00:12:46 +03:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5a4fa61ed2 | |||
| 8197a64803 | |||
| e75d82e896 | |||
| 7669dff7a6 | |||
| 7edca0b5e6 | |||
| 340924213a | |||
| 3833461e90 | |||
| 3e22bb1dc1 | |||
| 324510173a |
@@ -1,14 +1,10 @@
|
|||||||
// CGame.cpp
|
// Bulls and Cows the game
|
||||||
|
// CGame.cpp
|
||||||
//
|
//
|
||||||
#include "CGame.h"
|
#include "CGame.h"
|
||||||
#include <string>
|
|
||||||
#include <random>
|
|
||||||
#include <cwctype>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
void CGame::Init(int digits)
|
void CGame::Init()
|
||||||
{
|
{
|
||||||
m_ucDigits = digits;
|
|
||||||
m_fGameIsEnd = false;
|
m_fGameIsEnd = false;
|
||||||
m_ucBulls = 0;
|
m_ucBulls = 0;
|
||||||
m_ucCows = 0;
|
m_ucCows = 0;
|
||||||
@@ -39,6 +35,7 @@ void CGame::Init(int digits)
|
|||||||
void CGame::Start()
|
void CGame::Start()
|
||||||
{
|
{
|
||||||
CStep step;
|
CStep step;
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
while (!m_fGameIsEnd)
|
while (!m_fGameIsEnd)
|
||||||
@@ -46,38 +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;
|
|
||||||
}
|
|
||||||
case -4:
|
|
||||||
{
|
|
||||||
m_sLastError = L"Цифры в числе не должны повторяться!";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
m_sLastError = L"";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (ret < 0);
|
|
||||||
m_uStepCounter++;
|
m_uStepCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,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';
|
||||||
@@ -106,9 +109,8 @@ void CGame::ShowGuessedNumber(bool show)
|
|||||||
void CGame::PrintGameHeader()
|
void CGame::PrintGameHeader()
|
||||||
{
|
{
|
||||||
std::system("cls");
|
std::system("cls");
|
||||||
std::wcout << L"Быки и Коровы. Версия " << VERSION << std::endl;
|
std::wcout << L"Быки и Коровы." << ' ' << L"Версия" << ' ' << VERSION << std::endl;
|
||||||
std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << std::endl;
|
std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << "\n\n" << std::endl;
|
||||||
std::wcout << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGame::PrintGameFooter()
|
void CGame::PrintGameFooter()
|
||||||
@@ -132,40 +134,35 @@ void CGame::PrintSteps()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int CGame::GetNumber(CStep step)
|
CGame::CGame()
|
||||||
{
|
{
|
||||||
std::vector<int> digits;
|
CUserInput userInput(1);
|
||||||
std::wstring sNumber;
|
|
||||||
wchar_t c;
|
|
||||||
|
|
||||||
std::wcout << L"\t\t\t\t\t" << m_sLastError << L"\r";
|
bool ok = false;
|
||||||
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++)
|
do
|
||||||
{
|
{
|
||||||
c = sNumber[i];
|
PrintGameHeader();
|
||||||
int r = std::iswdigit(c);
|
std::wcout << L"Введите количество цифр в числе (от 3 до 5).\n";
|
||||||
if (!(bool)r)
|
std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n\n";
|
||||||
|
std::wcout << L"=> ";
|
||||||
|
|
||||||
|
if (userInput.Get())
|
||||||
{
|
{
|
||||||
return -3;
|
if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 5))
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (IsEqual(sNumber, c, i))
|
|
||||||
{
|
{
|
||||||
return -4;
|
m_ucDigits = userInput.m_vUserInput.at(0);
|
||||||
|
ok = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
userInput.m_vUserInput.clear();
|
||||||
}
|
}
|
||||||
digits.push_back(((int)c) - 48); // Convert from char 'n' to number n
|
|
||||||
}
|
}
|
||||||
}
|
} while (!ok);
|
||||||
|
}
|
||||||
step.StoreStepNumber(digits);
|
|
||||||
step.CheckForAnimals(m_uGuessedNumber, m_ucDigits);
|
CGame::~CGame()
|
||||||
m_Steps.push_back(step);
|
{
|
||||||
if (step.GetStepAnimals().first == 4) m_fGameIsEnd = true;
|
delete[] m_uGuessedNumber;
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CGame.h
|
// CGame.h
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "headers.h"
|
||||||
|
#include "CUserInput.h"
|
||||||
#include "CStep.h"
|
#include "CStep.h"
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#define VERSION 0.2
|
|
||||||
|
|
||||||
class CGame
|
class CGame
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int m_ucDigits;
|
int m_ucDigits = 4; // Number of digits into the guessed number
|
||||||
int m_ucBulls;
|
int m_ucBulls;
|
||||||
int m_ucCows;
|
int m_ucCows;
|
||||||
bool m_fGameIsEnd;
|
bool m_fGameIsEnd;
|
||||||
@@ -18,36 +18,30 @@ private:
|
|||||||
std::wstring m_sLastError;
|
std::wstring m_sLastError;
|
||||||
std::vector<CStep> m_Steps;
|
std::vector<CStep> m_Steps;
|
||||||
|
|
||||||
|
bool GetNumber(CStep);
|
||||||
|
void ShowGuessedNumber(bool);
|
||||||
|
void PrintGameHeader();
|
||||||
|
void PrintGameFooter();
|
||||||
|
void PrintSteps();
|
||||||
|
|
||||||
template<typename T, typename N>
|
template<typename T, typename N>
|
||||||
bool IsEqual(const T, const N, int);
|
bool IsEqual(const T, const N, int);
|
||||||
|
|
||||||
int GetNumber(CStep);
|
|
||||||
|
|
||||||
void ShowGuessedNumber(bool);
|
|
||||||
|
|
||||||
void PrintGameHeader();
|
|
||||||
|
|
||||||
void PrintGameFooter();
|
|
||||||
|
|
||||||
void PrintSteps();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
void Init();
|
||||||
void Init(int);
|
|
||||||
|
|
||||||
void Start();
|
void Start();
|
||||||
|
CGame();
|
||||||
|
~CGame();
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, typename N>
|
template<typename T, typename N>
|
||||||
bool CGame::IsEqual(const T number, const N digit, int i)
|
bool CGame::IsEqual(const T number, const N digit, int i)
|
||||||
{
|
{
|
||||||
i--;
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
while (i >= 0)
|
while ((--i) >= 0)
|
||||||
{
|
{
|
||||||
if (digit == number[i]) result = true;
|
if (digit == number[i]) result = true;
|
||||||
i--;
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CStep.cpp
|
// CStep.cpp
|
||||||
//
|
//
|
||||||
#include "CStep.h"
|
#include "CStep.h"
|
||||||
|
|||||||
@@ -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
|
||||||
{
|
{
|
||||||
|
|||||||
36
bulls-and-cows-cpp/CUserInput.cpp
Normal file
36
bulls-and-cows-cpp/CUserInput.cpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// CUserInput.cpp
|
||||||
|
//
|
||||||
|
#include "CUserInput.h"
|
||||||
|
|
||||||
|
CUserInput::CUserInput(int nNumOfDigits)
|
||||||
|
{
|
||||||
|
m_nNumOfExpectedDigits = nNumOfDigits;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUserInput::Get()
|
||||||
|
{
|
||||||
|
std::wstring sDigits;
|
||||||
|
wchar_t cDigit;
|
||||||
|
int nDigit;
|
||||||
|
|
||||||
|
std::getline(std::wcin, sDigits);
|
||||||
|
|
||||||
|
if (sDigits.length() != m_nNumOfExpectedDigits)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < m_nNumOfExpectedDigits; i++)
|
||||||
|
{
|
||||||
|
cDigit = sDigits.at(i);
|
||||||
|
if (!(bool)std::iswdigit(cDigit))
|
||||||
|
{
|
||||||
|
m_vUserInput.clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
nDigit = ((int)cDigit) - 48;
|
||||||
|
m_vUserInput.push_back(nDigit);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
15
bulls-and-cows-cpp/CUserInput.h
Normal file
15
bulls-and-cows-cpp/CUserInput.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// CUserInput.h
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include "headers.h"
|
||||||
|
|
||||||
|
class CUserInput
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int m_nNumOfExpectedDigits;
|
||||||
|
std::vector<int> m_vUserInput;
|
||||||
|
|
||||||
|
CUserInput(int nNumOfExpectedDigits);
|
||||||
|
bool Get();
|
||||||
|
};
|
||||||
@@ -81,6 +81,7 @@
|
|||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<RandomizedBaseAddress>false</RandomizedBaseAddress>
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
@@ -133,11 +134,15 @@
|
|||||||
<ClCompile Include="bulls-and-cows.cpp" />
|
<ClCompile Include="bulls-and-cows.cpp" />
|
||||||
<ClCompile Include="CGame.cpp" />
|
<ClCompile Include="CGame.cpp" />
|
||||||
<ClCompile Include="CStep.cpp" />
|
<ClCompile Include="CStep.cpp" />
|
||||||
|
<ClCompile Include="CUserInput.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\..\..\MyFunctions\SetUserLocale.h" />
|
<ClInclude Include="..\..\..\MyFunctions\SetUserLocale.h" />
|
||||||
<ClInclude Include="CGame.h" />
|
<ClInclude Include="CGame.h" />
|
||||||
<ClInclude Include="CStep.h" />
|
<ClInclude Include="CStep.h" />
|
||||||
|
<ClInclude Include="CUserInput.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">
|
||||||
|
|||||||
@@ -27,6 +27,9 @@
|
|||||||
<ClCompile Include="CStep.cpp">
|
<ClCompile Include="CStep.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="CUserInput.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="CGame.h">
|
<ClInclude Include="CGame.h">
|
||||||
@@ -38,5 +41,14 @@
|
|||||||
<ClInclude Include="CStep.h">
|
<ClInclude Include="CStep.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="CUserInput.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="defines.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="headers.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,17 +1,16 @@
|
|||||||
|
// 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"
|
||||||
//#include <iostream>
|
|
||||||
|
|
||||||
int wmain(int argc, wchar_t* argv[])
|
int wmain(int argc, wchar_t* argv[])
|
||||||
{
|
{
|
||||||
if (!SetUserLocale()) return 1;
|
if (!SetUserLocale()) return 1;
|
||||||
|
|
||||||
CGame game;
|
CGame game;
|
||||||
|
game.Init();
|
||||||
game.Init(4);
|
|
||||||
game.Start();
|
game.Start();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
11
bulls-and-cows-cpp/defines.h
Normal file
11
bulls-and-cows-cpp/defines.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// defines.h
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#define VERSION 0.4
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
|
#define IS_SHOW true
|
||||||
|
#else
|
||||||
|
#define IS_SHOW false
|
||||||
|
#endif
|
||||||
12
bulls-and-cows-cpp/headers.h
Normal file
12
bulls-and-cows-cpp/headers.h
Normal 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"
|
||||||
Reference in New Issue
Block a user