The game are working!

This commit is contained in:
2023-06-14 22:42:11 +03:00
parent 16fc66d3d0
commit ae4230e721
5 changed files with 164 additions and 28 deletions

View File

@@ -1,12 +1,15 @@
// CGame.cpp // CGame.cpp
// //
#include "CGame.h" #include "CGame.h"
//#include <vector>
#include <string>
#include <random> #include <random>
#include <cwctype>
#include <iostream> #include <iostream>
// Initialize the variables
void CGame::Init() void CGame::Init()
{ {
m_fGameIsEnd = false;
m_ucBulls = 0; m_ucBulls = 0;
m_ucCows = 0; m_ucCows = 0;
m_uStepCounter = 0; m_uStepCounter = 0;
@@ -23,29 +26,112 @@ void CGame::Init()
n = d(e); n = d(e);
if (i > 0) if (i > 0)
{ {
while (CGame::IsEqual(n, i)) while (IsEqual(n, i))
{ {
n = d(e); n = d(e);
} }
} }
m_uGuessedNumber[i] = n; m_uGuessedNumber[i] = n;
} }
} }
void CGame::ShowGuessedNumber() void CGame::Start()
{ {
CStep step;
int ret = 0;
while (!m_fGameIsEnd)
{
do
{
PrintGameHeader();
ShowGuessedNumber(false);
PrintSteps();
ret = GetNumber(step);
if (ret == -1) return;
} while (ret < 0);
}
PrintGameHeader();
ShowGuessedNumber(true);
PrintSteps();
PrintGameFooter();
}
void CGame::ShowGuessedNumber(bool show)
{
std::wcout << '\t';
for (int i = 0; i < m_ucDigits; ++i) 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() void CGame::PrintGameHeader()
{ {
std::wcout << L"Áûêè è Êîðîâû. Âåðñèÿ " << VERSION << std::endl; std::system("cls");
std::wcout << L"Быки и Коровы. Версия " << VERSION << std::endl;
std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << std::endl;
std::wcout << std::endl;
}
void CGame::PrintGameFooter()
{
std::wcout << L"\n\n\t" << L"!!! П О Б Е Д А !!!" << 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;
std::wcout << L"=>\t";
std::wcin >> sNumber;
if (sNumber == L"Q" || sNumber == L"q") return -1;
if (sNumber.length() != m_ucDigits) return -2;
for (wchar_t& c : sNumber)
{
int r = std::iswdigit(c);
if (!(bool)r)
{
return -2;
}
else
{
int r = ((int)c) - 48;
digits.push_back(r);
}
}
step.StoreStepNumber(digits);
step.CheckForAnimals(m_uGuessedNumber, m_ucDigits);
m_Steps.push_back(step);
if (step.GetStepAnimals().first == 4) m_fGameIsEnd = true;
return 0;
} }
bool CGame::IsEqual(int n, int j) bool CGame::IsEqual(int n, int j)
@@ -61,7 +147,7 @@ bool CGame::IsEqual(int n, int j)
return result; return result;
} }
CGame::CGame(int digits) CGame::CGame(int d)
{ {
m_ucDigits = digits; m_ucDigits = d;
} }

View File

@@ -2,7 +2,6 @@
// //
#pragma once #pragma once
#include "CStep.h" #include "CStep.h"
#include <vector>
#define VERSION 0.1 #define VERSION 0.1
@@ -10,18 +9,23 @@ class CGame
{ {
private: private:
unsigned char m_ucDigits; 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_ucBulls;
unsigned char m_ucCows; unsigned char m_ucCows;
bool m_fGameIsEnd;
int m_uStepCounter;
int* m_uGuessedNumber;
std::vector<CStep> m_Steps;
void Init(); bool IsEqual(int, int);
void ShowGuessedNumber(); int GetNumber(CStep);
void ShowGuessedNumber(bool);
void PrintGameHeader(); void PrintGameHeader();
void PrintGameFooter();
void PrintSteps();
public:
void Init();
void Start();
CGame(int); CGame(int);
}; };

View File

@@ -2,3 +2,44 @@
// //
#include "CStep.h" #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;
}
CStep::CStep()
{
}

View File

@@ -1,11 +1,18 @@
// CStep.h // CStep.h
// //
#pragma once #pragma once
#include <tuple> #include <vector>
class CStep class CStep
{ {
private:
std::vector<int> m_uStepNumber;
std::pair<int, int> m_uStepAnimals;
public: public:
std::tuple<int, int, int, int> m_uStepNumber; void StoreStepNumber(std::vector<int>&);
std::tuple<unsigned char, unsigned char> m_ucStepAnimals; void CheckForAnimals(const int*, int);
std::vector<int> GetStepNumber();
std::pair<int, int> GetStepAnimals();
CStep();
}; };

View File

@@ -3,6 +3,7 @@
#include <Windows.h> #include <Windows.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[])
{ {
@@ -10,10 +11,7 @@ int wmain(int argc, wchar_t* argv[])
CGame game(4); CGame game(4);
game.Init(); game.Init();
game.PrintGameHeader(); game.Start();
return 0; return 0;
} }