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 "CGame.h"
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
// Initialize the variables
|
// Initialize the variables
|
||||||
void CGame::Init()
|
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
|
// CGame.h
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "CStep.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#define VERSION 0.1
|
||||||
|
|
||||||
class CGame
|
class CGame
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
unsigned int m_uStepCounter;
|
unsigned char m_ucDigits;
|
||||||
unsigned int m_uGuessedNumber;
|
int m_uStepCounter;
|
||||||
|
int* m_uGuessedNumber;
|
||||||
|
std::vector<CStep> m_Step;
|
||||||
|
|
||||||
|
bool IsEqual(int, int);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
unsigned char m_ucBulls;
|
unsigned char m_ucBulls;
|
||||||
unsigned char m_ucCows;
|
unsigned char m_ucCows;
|
||||||
|
|
||||||
private:
|
|
||||||
// Initialize the variables
|
|
||||||
void Init();
|
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>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="..\..\..\MyFunctions\SetUserLocale.cpp" />
|
||||||
<ClCompile Include="bulls-and-cows.cpp" />
|
<ClCompile Include="bulls-and-cows.cpp" />
|
||||||
<ClCompile Include="CGame.cpp" />
|
<ClCompile Include="CGame.cpp" />
|
||||||
|
<ClCompile Include="CStep.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="..\..\..\MyFunctions\SetUserLocale.h" />
|
||||||
<ClInclude Include="CGame.h" />
|
<ClInclude Include="CGame.h" />
|
||||||
|
<ClInclude Include="CStep.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|||||||
@@ -21,10 +21,22 @@
|
|||||||
<ClCompile Include="CGame.cpp">
|
<ClCompile Include="CGame.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\..\MyFunctions\SetUserLocale.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="CStep.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="CGame.h">
|
<ClInclude Include="CGame.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\..\MyFunctions\SetUserLocale.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="CStep.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
// bulls-and-cows.cpp
|
// bulls-and-cows.cpp
|
||||||
//
|
//
|
||||||
#include <iostream>
|
#include <Windows.h>
|
||||||
#include <vector>
|
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
||||||
#include <string>
|
|
||||||
#include <sstream>
|
|
||||||
#include "CGame.h"
|
#include "CGame.h"
|
||||||
|
|
||||||
int wmain(int argc, wchar_t* argv[])
|
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