Files
bulls-and-cows-cpp/bulls-and-cows-cpp/CGame.cpp

172 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Bulls and Cows the game
// CGame.cpp
//
#include "CGame.h"
void CGame::Init()
{
m_fGameIsEnd = false;
m_ucBulls = 0;
m_ucCows = 0;
m_uStepCounter = 0;
m_sLastError = L"";
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 (IsEqual(m_uGuessedNumber, n, i))
{
n = d(e);
}
}
m_uGuessedNumber[i] = n;
}
}
void CGame::Start()
{
CStep step;
int ret = 0;
while (!m_fGameIsEnd)
{
do
{
PrintGameHeader();
ShowGuessedNumber(IS_SHOW);
PrintSteps();
} while (!GetNumber(step));
m_uStepCounter++;
}
PrintGameHeader();
ShowGuessedNumber(true);
PrintSteps();
PrintGameFooter();
}
bool CGame::GetNumber(CStep step)
{
std::wcout << L"\t\t\t\t\t" << m_sLastError << L"\r";
std::wcout << "=>\t";
CUserInput userInput(m_ucDigits);
if (userInput.Get())
{
for (unsigned i = 0; i < userInput.m_vUserInput.size(); i++)
{
if (IsEqual(userInput.m_vUserInput, userInput.m_vUserInput.at(i), i))
{
m_sLastError = L"Цифры в числе не должны повторяться!";
return false;
}
}
}
else
{
m_sLastError = L"Вы должны ввести " + std::to_wstring(m_ucDigits) + L"-значное число!";
return false;
}
step.StoreStepNumber(userInput.m_vUserInput);
step.CheckForAnimals(m_uGuessedNumber, m_ucDigits);
m_Steps.push_back(step);
if (step.GetStepAnimals().first == m_ucDigits) m_fGameIsEnd = true;
m_sLastError = L"";
return true;
}
void CGame::ShowGuessedNumber(bool show)
{
std::wcout << '\t';
for (int i = 0; i < m_ucDigits; ++i)
{
show ? std::wcout << m_uGuessedNumber[i] : std::wcout << '#';
}
std::wcout << '\n' << '\t';
for (int i = 0; i < m_ucDigits; ++i)
{
std::wcout << '-';
}
std::wcout << std::endl;
}
void CGame::PrintGameHeader()
{
std::system("cls");
std::wcout << L"Быки и Коровы." << ' ' << L"Версия" << ' ' << VERSION << std::endl;
std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << "\n\n" << std::endl;
}
void CGame::PrintGameFooter()
{
std::wcout << L"\n\n\t" << L"!!! П О Б Е Д А !!!" << std::endl;
std::wcout << L"\tКоличество ходов: " << m_uStepCounter << L"\n\n" << std::endl;
}
void CGame::PrintGameInitialQuery()
{
std::wcout << L"Введите количество цифр в числе (от 3 до 5).\n";
std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n" << std::endl;
}
void CGame::PrintSteps()
{
if (m_Steps.size() < 1) return;
for (CStep& s : m_Steps)
{
std::wcout << '\t';
for (auto& d : s.GetStepNumber()) std::wcout << d;
std::wcout << '\t' << s.GetStepAnimals().first << L" Б., " << s.GetStepAnimals().second << L" К.";
std::wcout << std::endl;
}
}
CGame::CGame()
{
CUserInput userInput(1);
bool ok = false;
do
{
PrintGameHeader();
PrintGameInitialQuery();
std::wcout << "=> ";
if (userInput.Get())
{
if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 5))
{
m_ucDigits = userInput.m_vUserInput.at(0);
ok = true;
}
else
{
userInput.m_vUserInput.clear();
}
}
} while (!ok);
}
CGame::~CGame()
{
delete[] m_uGuessedNumber;
}