mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-28 16:02:46 +03:00
Added check for the same digits in user input.
This commit is contained in:
@@ -26,7 +26,7 @@ void CGame::Init(int digits)
|
|||||||
n = d(e);
|
n = d(e);
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
{
|
{
|
||||||
while (IsEqual(n, i))
|
while (IsEqual1(m_uGuessedNumber, n, i))
|
||||||
{
|
{
|
||||||
n = d(e);
|
n = d(e);
|
||||||
}
|
}
|
||||||
@@ -108,23 +108,28 @@ int CGame::GetNumber(CStep step)
|
|||||||
{
|
{
|
||||||
std::vector<int> digits;
|
std::vector<int> digits;
|
||||||
std::wstring sNumber;
|
std::wstring sNumber;
|
||||||
|
wchar_t c;
|
||||||
|
|
||||||
std::wcout << L"=>\t";
|
std::wcout << L"=>\t";
|
||||||
std::wcin >> sNumber;
|
std::wcin >> sNumber;
|
||||||
if (sNumber == L"Q" || sNumber == L"q") return -1;
|
if (sNumber == L"Q" || sNumber == L"q") return -1;
|
||||||
if (sNumber.length() != m_ucDigits) return -2;
|
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);
|
int r = std::iswdigit(c);
|
||||||
if (!(bool)r)
|
if (!(bool)r)
|
||||||
{
|
{
|
||||||
return -2;
|
return -3;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int r = ((int)c) - 48;
|
if (IsEqual2(sNumber, c, i))
|
||||||
digits.push_back(r);
|
{
|
||||||
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CGame::IsEqual(int n, int j)
|
bool CGame::IsEqual1(const int* a, const int n, int j)
|
||||||
{
|
{
|
||||||
j--;
|
j--;
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
while (j >= 0)
|
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--;
|
j--;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -2,28 +2,38 @@
|
|||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "CStep.h"
|
#include "CStep.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#define VERSION 0.1
|
#define VERSION 0.1
|
||||||
|
|
||||||
class CGame
|
class CGame
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
unsigned char m_ucDigits;
|
int m_ucDigits;
|
||||||
unsigned char m_ucBulls;
|
int m_ucBulls;
|
||||||
unsigned char m_ucCows;
|
int m_ucCows;
|
||||||
bool m_fGameIsEnd;
|
bool m_fGameIsEnd;
|
||||||
int m_uStepCounter;
|
int m_uStepCounter;
|
||||||
int* m_uGuessedNumber;
|
int* m_uGuessedNumber;
|
||||||
std::vector<CStep> m_Steps;
|
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);
|
int GetNumber(CStep);
|
||||||
|
|
||||||
void ShowGuessedNumber(bool);
|
void ShowGuessedNumber(bool);
|
||||||
|
|
||||||
void PrintGameHeader();
|
void PrintGameHeader();
|
||||||
|
|
||||||
void PrintGameFooter();
|
void PrintGameFooter();
|
||||||
|
|
||||||
void PrintSteps();
|
void PrintSteps();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void Init(int);
|
void Init(int);
|
||||||
|
|
||||||
void Start();
|
void Start();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user