mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-28 16:02:46 +03:00
Added initial game class.
This commit is contained in:
@@ -2,9 +2,66 @@
|
||||
//
|
||||
#include "CGame.h"
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
|
||||
// Initialize the variables
|
||||
void CGame::Init()
|
||||
{
|
||||
// TODO: Add your implementation code here.
|
||||
m_ucBulls = 0;
|
||||
m_ucCows = 0;
|
||||
m_uStepCounter = 0;
|
||||
|
||||
std::random_device r;
|
||||
std::default_random_engine e(r());
|
||||
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)
|
||||
{
|
||||
n = d(e);
|
||||
if (i > 0)
|
||||
{
|
||||
while (CGame::IsEqual(n, i))
|
||||
{
|
||||
n = d(e);
|
||||
}
|
||||
}
|
||||
m_uGuessedNumber[i] = n;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void CGame::ShowGuessedNumber()
|
||||
{
|
||||
for (int i = 0; i < m_ucDigits; ++i)
|
||||
{
|
||||
std::cout << m_uGuessedNumber[i];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void CGame::PrintGameHeader()
|
||||
{
|
||||
std::wcout << L"Áûêè è Êîðîâû. Âåðñèÿ " << VERSION << std::endl;
|
||||
}
|
||||
|
||||
bool CGame::IsEqual(int n, int j)
|
||||
{
|
||||
j--;
|
||||
bool result = false;
|
||||
|
||||
while (j >= 0)
|
||||
{
|
||||
if (n == m_uGuessedNumber[j]) result = true;
|
||||
j--;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
CGame::CGame(int digits)
|
||||
{
|
||||
m_ucDigits = digits;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,27 @@
|
||||
// CGame.h
|
||||
//
|
||||
#pragma once
|
||||
#include "CStep.h"
|
||||
#include <vector>
|
||||
|
||||
#define VERSION 0.1
|
||||
|
||||
class CGame
|
||||
{
|
||||
private:
|
||||
unsigned int m_uStepCounter;
|
||||
unsigned int m_uGuessedNumber;
|
||||
unsigned char m_ucDigits;
|
||||
int m_uStepCounter;
|
||||
int* m_uGuessedNumber;
|
||||
std::vector<CStep> m_Step;
|
||||
|
||||
bool IsEqual(int, int);
|
||||
|
||||
public:
|
||||
unsigned char m_ucBulls;
|
||||
unsigned char m_ucCows;
|
||||
|
||||
private:
|
||||
// Initialize the variables
|
||||
void Init();
|
||||
void ShowGuessedNumber();
|
||||
void PrintGameHeader();
|
||||
CGame(int);
|
||||
};
|
||||
|
||||
|
||||
4
bulls-and-cows-cpp/CStep.cpp
Normal file
4
bulls-and-cows-cpp/CStep.cpp
Normal file
@@ -0,0 +1,4 @@
|
||||
// CStep.cpp
|
||||
//
|
||||
#include "CStep.h"
|
||||
|
||||
11
bulls-and-cows-cpp/CStep.h
Normal file
11
bulls-and-cows-cpp/CStep.h
Normal file
@@ -0,0 +1,11 @@
|
||||
// CStep.h
|
||||
//
|
||||
#pragma once
|
||||
#include <tuple>
|
||||
|
||||
class CStep
|
||||
{
|
||||
public:
|
||||
std::tuple<int, int, int, int> m_uStepNumber;
|
||||
std::tuple<unsigned char, unsigned char> m_ucStepAnimals;
|
||||
};
|
||||
@@ -129,11 +129,15 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\..\MyFunctions\SetUserLocale.cpp" />
|
||||
<ClCompile Include="bulls-and-cows.cpp" />
|
||||
<ClCompile Include="CGame.cpp" />
|
||||
<ClCompile Include="CStep.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\..\MyFunctions\SetUserLocale.h" />
|
||||
<ClInclude Include="CGame.h" />
|
||||
<ClInclude Include="CStep.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -21,10 +21,22 @@
|
||||
<ClCompile Include="CGame.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\..\MyFunctions\SetUserLocale.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CStep.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CGame.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\..\MyFunctions\SetUserLocale.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CStep.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,14 +1,16 @@
|
||||
// bulls-and-cows.cpp
|
||||
//
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <Windows.h>
|
||||
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
||||
#include "CGame.h"
|
||||
|
||||
int wmain(int argc, wchar_t* argv[])
|
||||
{
|
||||
if (!SetUserLocale()) return 1;
|
||||
|
||||
CGame game(4);
|
||||
game.Init();
|
||||
game.PrintGameHeader();
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user