mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-28 16:02:46 +03:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5a4fa61ed2 | |||
| 8197a64803 | |||
| e75d82e896 | |||
| 7669dff7a6 | |||
| 7edca0b5e6 | |||
| 340924213a | |||
| 3833461e90 | |||
| 3e22bb1dc1 | |||
| 324510173a | |||
| ba43d72188 | |||
| 2d9c1c1d57 | |||
| a34da8d34f | |||
| 2bc245354c | |||
| c0dfdf53bb | |||
| 4b634caf52 | |||
| ae4230e721 |
@@ -1,15 +1,15 @@
|
||||
// Bulls and Cows the game
|
||||
// CGame.cpp
|
||||
//
|
||||
#include "CGame.h"
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
|
||||
// Initialize the variables
|
||||
void CGame::Init()
|
||||
{
|
||||
m_fGameIsEnd = false;
|
||||
m_ucBulls = 0;
|
||||
m_ucCows = 0;
|
||||
m_uStepCounter = 0;
|
||||
m_sLastError = L"";
|
||||
|
||||
std::random_device r;
|
||||
std::default_random_engine e(r());
|
||||
@@ -23,45 +23,146 @@ void CGame::Init()
|
||||
n = d(e);
|
||||
if (i > 0)
|
||||
{
|
||||
while (CGame::IsEqual(n, i))
|
||||
while (IsEqual(m_uGuessedNumber, 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(IS_SHOW);
|
||||
PrintSteps();
|
||||
} while (!GetNumber(step));
|
||||
|
||||
m_uStepCounter++;
|
||||
}
|
||||
|
||||
PrintGameHeader();
|
||||
ShowGuessedNumber(true);
|
||||
PrintSteps();
|
||||
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)
|
||||
{
|
||||
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"Быки и Коровы." << ' ' << L"Версия" << ' ' << VERSION << std::endl;
|
||||
std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << "\n\n" << std::endl;
|
||||
}
|
||||
|
||||
bool CGame::IsEqual(int n, int j)
|
||||
void CGame::PrintGameFooter()
|
||||
{
|
||||
j--;
|
||||
bool result = false;
|
||||
std::wcout << L"\n\n\t" << L"!!! П О Б Е Д А !!!" << std::endl;
|
||||
std::wcout << L"\tКоличество ходов: " << m_uStepCounter << L"\n\n" << std::endl;
|
||||
}
|
||||
|
||||
while (j >= 0)
|
||||
void CGame::PrintSteps()
|
||||
{
|
||||
if (m_Steps.size() < 1) return;
|
||||
|
||||
for (CStep& s : m_Steps)
|
||||
{
|
||||
if (n == m_uGuessedNumber[j]) result = true;
|
||||
j--;
|
||||
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;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
CGame::CGame(int digits)
|
||||
CGame::CGame()
|
||||
{
|
||||
m_ucDigits = digits;
|
||||
CUserInput userInput(1);
|
||||
|
||||
bool ok = false;
|
||||
|
||||
do
|
||||
{
|
||||
PrintGameHeader();
|
||||
std::wcout << L"Введите количество цифр в числе (от 3 до 5).\n";
|
||||
std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n\n";
|
||||
std::wcout << L"=> ";
|
||||
|
||||
if (userInput.Get())
|
||||
{
|
||||
if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 5))
|
||||
{
|
||||
m_ucDigits = userInput.m_vUserInput.at(0);
|
||||
ok = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
userInput.m_vUserInput.clear();
|
||||
}
|
||||
}
|
||||
} while (!ok);
|
||||
}
|
||||
|
||||
CGame::~CGame()
|
||||
{
|
||||
delete[] m_uGuessedNumber;
|
||||
}
|
||||
|
||||
@@ -1,27 +1,47 @@
|
||||
// Bulls and Cows the game
|
||||
// CGame.h
|
||||
//
|
||||
#pragma once
|
||||
#include "headers.h"
|
||||
#include "CUserInput.h"
|
||||
#include "CStep.h"
|
||||
#include <vector>
|
||||
|
||||
#define VERSION 0.1
|
||||
|
||||
class CGame
|
||||
{
|
||||
private:
|
||||
unsigned char m_ucDigits;
|
||||
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::vector<CStep> m_Step;
|
||||
std::wstring m_sLastError;
|
||||
std::vector<CStep> m_Steps;
|
||||
|
||||
bool IsEqual(int, int);
|
||||
bool GetNumber(CStep);
|
||||
void ShowGuessedNumber(bool);
|
||||
void PrintGameHeader();
|
||||
void PrintGameFooter();
|
||||
void PrintSteps();
|
||||
|
||||
template<typename T, typename N>
|
||||
bool IsEqual(const T, const N, int);
|
||||
|
||||
public:
|
||||
unsigned char m_ucBulls;
|
||||
unsigned char m_ucCows;
|
||||
|
||||
void Init();
|
||||
void ShowGuessedNumber();
|
||||
void PrintGameHeader();
|
||||
CGame(int);
|
||||
void Start();
|
||||
CGame();
|
||||
~CGame();
|
||||
};
|
||||
|
||||
template<typename T, typename N>
|
||||
bool CGame::IsEqual(const T number, const N digit, int i)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
while ((--i) >= 0)
|
||||
{
|
||||
if (digit == number[i]) result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,42 @@
|
||||
// Bulls and Cows the game
|
||||
// CStep.cpp
|
||||
//
|
||||
#include "CStep.h"
|
||||
|
||||
void CStep::StoreStepNumber(std::vector<int>& 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<int> CStep::GetStepNumber()
|
||||
{
|
||||
return m_uStepNumber;
|
||||
}
|
||||
|
||||
std::pair<int, int> CStep::GetStepAnimals()
|
||||
{
|
||||
return m_uStepAnimals;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
// Bulls and Cows the game
|
||||
// CStep.h
|
||||
//
|
||||
#pragma once
|
||||
#include <tuple>
|
||||
#include "headers.h"
|
||||
|
||||
class CStep
|
||||
{
|
||||
private:
|
||||
std::vector<int> m_uStepNumber;
|
||||
std::pair<int, int> m_uStepAnimals;
|
||||
|
||||
public:
|
||||
std::tuple<int, int, int, int> m_uStepNumber;
|
||||
std::tuple<unsigned char, unsigned char> m_ucStepAnimals;
|
||||
void StoreStepNumber(std::vector<int>&);
|
||||
void CheckForAnimals(const int*, int);
|
||||
std::vector<int> GetStepNumber();
|
||||
std::pair<int, int> GetStepAnimals();
|
||||
};
|
||||
|
||||
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>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<RandomizedBaseAddress>false</RandomizedBaseAddress>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
@@ -133,11 +134,15 @@
|
||||
<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" />
|
||||
<ClInclude Include="headers.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,14 @@
|
||||
<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>
|
||||
<ClInclude Include="headers.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,6 +1,7 @@
|
||||
// Bulls and Cows the game
|
||||
// bulls-and-cows.cpp
|
||||
//
|
||||
#include <Windows.h>
|
||||
#include "headers.h"
|
||||
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
||||
#include "CGame.h"
|
||||
|
||||
@@ -8,12 +9,9 @@ int wmain(int argc, wchar_t* argv[])
|
||||
{
|
||||
if (!SetUserLocale()) return 1;
|
||||
|
||||
CGame game(4);
|
||||
CGame game;
|
||||
game.Init();
|
||||
game.PrintGameHeader();
|
||||
|
||||
|
||||
|
||||
game.Start();
|
||||
|
||||
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