Added check for the same digits in user input.

This commit is contained in:
2023-06-30 18:59:51 +03:00
parent c0dfdf53bb
commit 2bc245354c
2 changed files with 39 additions and 11 deletions

View File

@@ -26,7 +26,7 @@ void CGame::Init(int digits)
n = d(e);
if (i > 0)
{
while (IsEqual(n, i))
while (IsEqual1(m_uGuessedNumber, n, i))
{
n = d(e);
}
@@ -108,23 +108,28 @@ int CGame::GetNumber(CStep step)
{
std::vector<int> digits;
std::wstring sNumber;
wchar_t c;
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)
for (int i = 0; i < sNumber.length(); i++)
{
c = sNumber[i];
int r = std::iswdigit(c);
if (!(bool)r)
{
return -2;
return -3;
}
else
{
int r = ((int)c) - 48;
digits.push_back(r);
if (IsEqual2(sNumber, c, i))
{
return -4;
}
digits.push_back(((int)c) - 48); // Convert from char 'n' to number n
}
}
@@ -136,14 +141,27 @@ int CGame::GetNumber(CStep step)
return 0;
}
bool CGame::IsEqual(int n, int j)
bool CGame::IsEqual1(const int* a, const int n, int j)
{
j--;
bool result = false;
while (j >= 0)
{
if (n == m_uGuessedNumber[j]) result = true;
if (n == a[j]) result = true;
j--;
}
return result;
}
bool CGame::IsEqual2(const std::wstring& a, const wchar_t n, int j)
{
j--;
bool result = false;
while (j >= 0)
{
if (n == a[j]) result = true;
j--;
}
return result;

View File

@@ -2,28 +2,38 @@
//
#pragma once
#include "CStep.h"
#include <string>
#define VERSION 0.1
class CGame
{
private:
unsigned char m_ucDigits;
unsigned char m_ucBulls;
unsigned char m_ucCows;
int m_ucDigits;
int m_ucBulls;
int m_ucCows;
bool m_fGameIsEnd;
int m_uStepCounter;
int* m_uGuessedNumber;
std::vector<CStep> m_Steps;
bool IsEqual(int, int);
bool IsEqual1(const int*, const int, int);
bool IsEqual2(const std::wstring&, const wchar_t, int);
int GetNumber(CStep);
void ShowGuessedNumber(bool);
void PrintGameHeader();
void PrintGameFooter();
void PrintSteps();
public:
void Init(int);
void Start();
};