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 | |
|---|---|---|---|
| 036a8e4645 | |||
| 5482fe1814 | |||
| f7572874b0 | |||
| 33d1f783ac | |||
| 872e390ce9 | |||
| 5a4fa61ed2 | |||
| 8197a64803 | |||
| e75d82e896 | |||
| 7669dff7a6 |
@@ -1,15 +1,19 @@
|
||||
// CGame.cpp
|
||||
// Bulls and Cows the game
|
||||
// CGame.cpp
|
||||
//
|
||||
#include "defines.h"
|
||||
#include "CGame.h"
|
||||
#include "CUserInput.h"
|
||||
#include "CPrint.h"
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
void CGame::Init()
|
||||
{
|
||||
m_fGameIsEnd = false;
|
||||
m_ucBulls = 0;
|
||||
m_ucCows = 0;
|
||||
m_uStepCounter = 0;
|
||||
m_fGameIsEnd = false;
|
||||
m_sLastError = L"";
|
||||
|
||||
std::random_device r;
|
||||
@@ -17,6 +21,7 @@ void CGame::Init()
|
||||
std::uniform_int_distribution<int> d(0, 9);
|
||||
|
||||
m_uGuessedNumber = new int[m_ucDigits] { 0 };
|
||||
|
||||
int n = 0;
|
||||
|
||||
for (int i = 0; i < m_ucDigits; ++i)
|
||||
@@ -36,129 +41,59 @@ void CGame::Init()
|
||||
void CGame::Start()
|
||||
{
|
||||
CStep step;
|
||||
|
||||
int ret = 0;
|
||||
|
||||
while (!m_fGameIsEnd)
|
||||
{
|
||||
do
|
||||
{
|
||||
PrintGameHeader();
|
||||
ShowGuessedNumber(false);
|
||||
PrintSteps();
|
||||
ret = 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);
|
||||
CPrint::GameHeader();
|
||||
CPrint::GuessedNumber(this, IS_SHOW);
|
||||
CPrint::Steps(this);
|
||||
} while (!GetNumber(step));
|
||||
|
||||
m_uStepCounter++;
|
||||
}
|
||||
|
||||
PrintGameHeader();
|
||||
ShowGuessedNumber(true);
|
||||
PrintSteps();
|
||||
PrintGameFooter();
|
||||
CPrint::GameHeader();
|
||||
CPrint::GuessedNumber(this, true);
|
||||
CPrint::Steps(this);
|
||||
CPrint::GameFooter(this);
|
||||
}
|
||||
|
||||
void CGame::ShowGuessedNumber(bool show)
|
||||
bool CGame::GetNumber(CStep step)
|
||||
{
|
||||
std::wcout << '\t';
|
||||
for (int i = 0; i < m_ucDigits; ++i)
|
||||
{
|
||||
show ? std::wcout << m_uGuessedNumber[i] : std::wcout << '#';
|
||||
}
|
||||
std::wcout << '\n' << '\t';
|
||||
|
||||
for (int i = 0; i < m_ucDigits; ++i)
|
||||
{
|
||||
std::wcout << '-';
|
||||
}
|
||||
std::wcout << std::endl;
|
||||
}
|
||||
|
||||
void CGame::PrintGameHeader()
|
||||
{
|
||||
std::system("cls");
|
||||
std::wcout << strGAMENAME << ' ' << strGAMEVERSION << ' ' << VERSION << std::endl;
|
||||
std::wcout << strGAMECOPYRIGHT << '\n' << std::endl;
|
||||
}
|
||||
|
||||
void CGame::PrintGameFooter()
|
||||
{
|
||||
std::wcout << L"\n\n\t" << L"!!! П О Б Е Д А !!!" << std::endl;
|
||||
std::wcout << L"\tКоличество ходов: " << m_uStepCounter << 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<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;
|
||||
std::wcout << "=>\t";
|
||||
|
||||
for (int i = 0; i < sNumber.length(); i++)
|
||||
CUserInput userInput(m_ucDigits);
|
||||
|
||||
if (userInput.Get())
|
||||
{
|
||||
c = sNumber[i];
|
||||
int r = std::iswdigit(c);
|
||||
if (!(bool)r)
|
||||
for (unsigned i = 0; i < userInput.m_vUserInput.size(); i++)
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (IsEqual(sNumber, c, i))
|
||||
if (IsEqual(userInput.m_vUserInput, userInput.m_vUserInput.at(i), i))
|
||||
{
|
||||
return -3;
|
||||
m_sLastError = strERROR_1;
|
||||
return false;
|
||||
}
|
||||
digits.push_back(((int)c) - 48); // Convert from char 'n' to number n
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_sLastError = strERROR_2_1 + std::to_wstring(m_ucDigits) + strERROR_2_2;
|
||||
return false;
|
||||
}
|
||||
|
||||
step.StoreStepNumber(digits);
|
||||
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;
|
||||
|
||||
return 0;
|
||||
m_sLastError = L"";
|
||||
return true;
|
||||
}
|
||||
|
||||
CGame::CGame()
|
||||
@@ -169,14 +104,13 @@ CGame::CGame()
|
||||
|
||||
do
|
||||
{
|
||||
PrintGameHeader();
|
||||
std::wcout << L"Введите количество цифр в числе (от 3 до 7).\n";
|
||||
std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n";
|
||||
std::wcout << L"=> ";
|
||||
CPrint::GameHeader();
|
||||
CPrint::GameInitialQuery();
|
||||
std::wcout << "=> ";
|
||||
|
||||
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);
|
||||
ok = true;
|
||||
|
||||
@@ -1,32 +1,29 @@
|
||||
// Bulls and Cows the game
|
||||
// CGame.h
|
||||
//
|
||||
#pragma once
|
||||
#include "defines.h"
|
||||
#include "CUserInput.h"
|
||||
#include "CStep.h"
|
||||
#include <string>
|
||||
|
||||
class CGame
|
||||
{
|
||||
private:
|
||||
int m_ucDigits = 4; // Number of digits into the guessed number
|
||||
int m_ucBulls;
|
||||
int m_ucCows;
|
||||
bool m_fGameIsEnd;
|
||||
int m_uStepCounter;
|
||||
int* m_uGuessedNumber;
|
||||
std::wstring m_sLastError;
|
||||
std::vector<CStep> m_Steps;
|
||||
|
||||
int GetNumber(CStep);
|
||||
void ShowGuessedNumber(bool);
|
||||
void PrintGameHeader();
|
||||
void PrintGameFooter();
|
||||
void PrintSteps();
|
||||
bool GetNumber(CStep);
|
||||
|
||||
template<typename T, typename N>
|
||||
bool IsEqual(const T, const N, int);
|
||||
|
||||
public:
|
||||
int m_ucDigits; // Number of digits into the guessed number
|
||||
int m_uStepCounter;
|
||||
int* m_uGuessedNumber;
|
||||
std::vector<CStep> m_Steps;
|
||||
|
||||
void Init();
|
||||
void Start();
|
||||
CGame();
|
||||
|
||||
54
bulls-and-cows-cpp/CPrint.cpp
Normal file
54
bulls-and-cows-cpp/CPrint.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
// Bulls and Cows the game
|
||||
// CPrint.cpp
|
||||
//
|
||||
#include "CPrint.h"
|
||||
#include "defines.h"
|
||||
#include <iostream>
|
||||
|
||||
void CPrint::GameInitialQuery()
|
||||
{
|
||||
std::wcout << strINITIAL_QUERY_1;
|
||||
std::wcout << strINITIAL_QUERY_2 << std::endl;
|
||||
}
|
||||
|
||||
void CPrint::GameHeader()
|
||||
{
|
||||
std::system("cls");
|
||||
std::wcout << strGAME_NAME << ' ' << strGAME_VERSION << ' ' << VERSION << std::endl;
|
||||
std::wcout << strGAME_COPYRIGHT << "\n\n" << std::endl;
|
||||
}
|
||||
|
||||
void CPrint::GameFooter(CGame* const game)
|
||||
{
|
||||
std::wcout << "\n\n\t" << strVICTORY << std::endl;
|
||||
std::wcout << strNUM_STEPS << game->m_uStepCounter << "\n\n" << std::endl;
|
||||
}
|
||||
|
||||
void CPrint::Steps(CGame* const game)
|
||||
{
|
||||
if (game->m_Steps.size() < 1) return;
|
||||
|
||||
for (CStep& s : game->m_Steps)
|
||||
{
|
||||
std::wcout << '\t';
|
||||
for (int& d : s.GetStepNumber()) std::wcout << d;
|
||||
std::wcout << '\t' << s.GetStepAnimals().first << strBULLS << s.GetStepAnimals().second << strCOWS;
|
||||
std::wcout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void CPrint::GuessedNumber(CGame* const game, const bool show)
|
||||
{
|
||||
std::wcout << '\t';
|
||||
for (int i = 0; i < game->m_ucDigits; ++i)
|
||||
{
|
||||
show ? std::wcout << game->m_uGuessedNumber[i] : std::wcout << '#';
|
||||
}
|
||||
std::wcout << '\n' << '\t';
|
||||
|
||||
for (int i = 0; i < game->m_ucDigits; ++i)
|
||||
{
|
||||
std::wcout << '-';
|
||||
}
|
||||
std::wcout << std::endl;
|
||||
}
|
||||
15
bulls-and-cows-cpp/CPrint.h
Normal file
15
bulls-and-cows-cpp/CPrint.h
Normal file
@@ -0,0 +1,15 @@
|
||||
// Bulls and Cows the game
|
||||
// CPrint.h
|
||||
//
|
||||
#pragma once
|
||||
#include "CGame.h"
|
||||
|
||||
class CPrint
|
||||
{
|
||||
public:
|
||||
static void GameInitialQuery();
|
||||
static void GameHeader();
|
||||
static void GameFooter(CGame* const);
|
||||
static void Steps(CGame* const);
|
||||
static void GuessedNumber(CGame* const, const bool);
|
||||
};
|
||||
@@ -1,3 +1,4 @@
|
||||
// Bulls and Cows the game
|
||||
// CStep.cpp
|
||||
//
|
||||
#include "CStep.h"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// Bulls and Cows the game
|
||||
// CStep.h
|
||||
//
|
||||
#pragma once
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
// Bulls and Cows the game
|
||||
// CUserInput.cpp
|
||||
//
|
||||
#include "CUserInput.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cwctype>
|
||||
|
||||
CUserInput::CUserInput(int nNumOfDigits)
|
||||
{
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
// Bulls and Cows the game
|
||||
// CUserInput.h
|
||||
//
|
||||
#pragma once
|
||||
#include "defines.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cwctype>
|
||||
|
||||
class CUserInput
|
||||
{
|
||||
|
||||
@@ -77,6 +77,7 @@
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||
<DisableSpecificWarnings>4018;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@@ -133,12 +134,14 @@
|
||||
<ClCompile Include="..\..\..\MyFunctions\SetUserLocale.cpp" />
|
||||
<ClCompile Include="bulls-and-cows.cpp" />
|
||||
<ClCompile Include="CGame.cpp" />
|
||||
<ClCompile Include="CPrint.cpp" />
|
||||
<ClCompile Include="CStep.cpp" />
|
||||
<ClCompile Include="CUserInput.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\..\MyFunctions\SetUserLocale.h" />
|
||||
<ClInclude Include="CGame.h" />
|
||||
<ClInclude Include="CPrint.h" />
|
||||
<ClInclude Include="CStep.h" />
|
||||
<ClInclude Include="CUserInput.h" />
|
||||
<ClInclude Include="defines.h" />
|
||||
|
||||
@@ -30,6 +30,9 @@
|
||||
<ClCompile Include="CUserInput.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CPrint.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CGame.h">
|
||||
@@ -47,5 +50,8 @@
|
||||
<ClInclude Include="defines.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CPrint.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,6 +1,6 @@
|
||||
// Bulls and Cows the game
|
||||
// bulls-and-cows.cpp
|
||||
//
|
||||
#include <Windows.h>
|
||||
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
||||
#include "CGame.h"
|
||||
|
||||
|
||||
@@ -1,6 +1,28 @@
|
||||
// Bulls and Cows the game
|
||||
// defines.h
|
||||
//
|
||||
#pragma once
|
||||
#define VERSION 0.4
|
||||
|
||||
#define VERSION 0.3
|
||||
#define strGAMENAME L"Áûêè è Êîðîâû."
|
||||
#define strGAMEVERSION L"Âåðñèÿ"
|
||||
#define strGAMECOPYRIGHT L"Copyright (c) 2023 by W0LF aka 'dreamforce'"
|
||||
#ifdef _DEBUG
|
||||
#define IS_SHOW true
|
||||
#else
|
||||
#define IS_SHOW false
|
||||
#endif
|
||||
|
||||
#define strERROR_1 L"Цифры в числе не должны повторяться!"
|
||||
#define strERROR_2_1 L"Вы должны ввести "
|
||||
#define strERROR_2_2 L"-значное число!"
|
||||
|
||||
#define strBULLS L" Б., "
|
||||
#define strCOWS L" К."
|
||||
|
||||
#define strINITIAL_QUERY_1 L"Введите количество цифр в числе (от 3 до 5).\n"
|
||||
#define strINITIAL_QUERY_2 L"Чем больше цифр - тем сложнее. Обычно 4.\n"
|
||||
|
||||
#define strGAME_NAME L"Быки и Коровы."
|
||||
#define strGAME_VERSION L"Версия"
|
||||
#define strGAME_COPYRIGHT L"Copyright (c) 2023 by W0LF aka 'dreamforce'"
|
||||
|
||||
#define strVICTORY L"!!! П О Б Е Д А !!!"
|
||||
#define strNUM_STEPS L"\tКоличество ходов: "
|
||||
|
||||
Reference in New Issue
Block a user