mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-28 16:02:46 +03:00
More moved some Print*() methods to a separate class.
This commit is contained in:
@@ -9,10 +9,10 @@
|
|||||||
|
|
||||||
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;
|
||||||
|
m_fGameIsEnd = false;
|
||||||
m_sLastError = L"";
|
m_sLastError = L"";
|
||||||
|
|
||||||
std::random_device r;
|
std::random_device r;
|
||||||
@@ -20,6 +20,7 @@ void CGame::Init()
|
|||||||
std::uniform_int_distribution<int> d(0, 9);
|
std::uniform_int_distribution<int> d(0, 9);
|
||||||
|
|
||||||
m_uGuessedNumber = new int[m_ucDigits] { 0 };
|
m_uGuessedNumber = new int[m_ucDigits] { 0 };
|
||||||
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
for (int i = 0; i < m_ucDigits; ++i)
|
for (int i = 0; i < m_ucDigits; ++i)
|
||||||
@@ -47,24 +48,19 @@ void CGame::Start()
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
CPrint::GameHeader();
|
CPrint::GameHeader();
|
||||||
ShowGuessedNumber(IS_SHOW);
|
CPrint::GuessedNumber(this, IS_SHOW);
|
||||||
PrintSteps();
|
CPrint::Steps(this);
|
||||||
} while (!GetNumber(step));
|
} while (!GetNumber(step));
|
||||||
|
|
||||||
m_uStepCounter++;
|
m_uStepCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
CPrint::GameHeader();
|
CPrint::GameHeader();
|
||||||
ShowGuessedNumber(true);
|
CPrint::GuessedNumber(this, true);
|
||||||
PrintSteps();
|
CPrint::Steps(this);
|
||||||
CPrint::GameFooter(this);
|
CPrint::GameFooter(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CGame::GetStepCounter()
|
|
||||||
{
|
|
||||||
return m_uStepCounter;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CGame::GetNumber(CStep step)
|
bool CGame::GetNumber(CStep step)
|
||||||
{
|
{
|
||||||
std::wcout << L"\t\t\t\t\t" << m_sLastError << L"\r";
|
std::wcout << L"\t\t\t\t\t" << m_sLastError << L"\r";
|
||||||
@@ -99,35 +95,6 @@ bool CGame::GetNumber(CStep step)
|
|||||||
return true;
|
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::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()
|
CGame::CGame()
|
||||||
{
|
{
|
||||||
CUserInput userInput(1);
|
CUserInput userInput(1);
|
||||||
|
|||||||
@@ -9,26 +9,24 @@
|
|||||||
class CGame
|
class CGame
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int m_ucDigits = 4; // Number of digits into the guessed number
|
|
||||||
int m_ucBulls;
|
int m_ucBulls;
|
||||||
int m_ucCows;
|
int m_ucCows;
|
||||||
bool m_fGameIsEnd;
|
bool m_fGameIsEnd;
|
||||||
int m_uStepCounter;
|
|
||||||
int* m_uGuessedNumber;
|
|
||||||
std::wstring m_sLastError;
|
std::wstring m_sLastError;
|
||||||
std::vector<CStep> m_Steps;
|
|
||||||
|
|
||||||
bool GetNumber(CStep);
|
bool GetNumber(CStep);
|
||||||
void ShowGuessedNumber(bool);
|
|
||||||
void PrintSteps();
|
|
||||||
|
|
||||||
template<typename T, typename N>
|
template<typename T, typename N>
|
||||||
bool IsEqual(const T, const N, int);
|
bool IsEqual(const T, const N, int);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
int m_ucDigits; // Number of digits into the guessed number
|
||||||
|
int m_uStepCounter;
|
||||||
|
int* m_uGuessedNumber;
|
||||||
|
std::vector<CStep> m_Steps;
|
||||||
|
|
||||||
void Init();
|
void Init();
|
||||||
void Start();
|
void Start();
|
||||||
int GetStepCounter();
|
|
||||||
CGame();
|
CGame();
|
||||||
~CGame();
|
~CGame();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,24 +1,53 @@
|
|||||||
// Bulls and Cows the game
|
// Bulls and Cows the game
|
||||||
// CPrint.cpp
|
// CPrint.cpp
|
||||||
//
|
//
|
||||||
#include "CPrint.h"
|
#include "CPrint.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
void CPrint::GameInitialQuery()
|
||||||
|
{
|
||||||
|
std::wcout << L"Введите количество цифр в числе (от 3 до 5).\n";
|
||||||
|
std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
void CPrint::GameHeader()
|
void CPrint::GameHeader()
|
||||||
{
|
{
|
||||||
std::system("cls");
|
std::system("cls");
|
||||||
std::wcout << L"Áûêè è Êîðîâû." << ' ' << L"Âåðñèÿ" << ' ' << VERSION << std::endl;
|
std::wcout << L"Быки и Коровы." << ' ' << L"Версия" << ' ' << VERSION << std::endl;
|
||||||
std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << "\n\n" << std::endl;
|
std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << "\n\n" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CPrint::GameFooter(CGame *game)
|
void CPrint::GameFooter(CGame* const game)
|
||||||
{
|
{
|
||||||
std::wcout << L"\n\n\t" << L"!!! Ï Î Á Å Ä À !!!" << std::endl;
|
std::wcout << L"\n\n\t" << L"!!! П О Б Е Д А !!!" << std::endl;
|
||||||
std::wcout << L"\tÊîëè÷åñòâî õîäîâ: " << game->GetStepCounter() << L"\n\n" << std::endl;
|
std::wcout << L"\tКоличество ходов: " << game->m_uStepCounter << L"\n\n" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CPrint::GameInitialQuery()
|
void CPrint::Steps(CGame* const game)
|
||||||
{
|
{
|
||||||
std::wcout << L"Ââåäèòå êîëè÷åñòâî öèôð â ÷èñëå (îò 3 äî 5).\n";
|
if (game->m_Steps.size() < 1) return;
|
||||||
std::wcout << L"×åì áîëüøå öèôð - òåì ñëîæíåå. Îáû÷íî 4.\n" << std::endl;
|
|
||||||
|
for (CStep& s : game->m_Steps)
|
||||||
|
{
|
||||||
|
std::wcout << '\t';
|
||||||
|
for (int& d : s.GetStepNumber()) std::wcout << d;
|
||||||
|
std::wcout << '\t' << s.GetStepAnimals().first << L" Б., " << s.GetStepAnimals().second << L" К.";
|
||||||
|
std::wcout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPrint::GuessedNumber(CGame* const game, const bool show)
|
||||||
|
{
|
||||||
|
std::wcout << '\t';
|
||||||
|
for (int i = 0; i < game->m_ucDigits; ++i)
|
||||||
|
{
|
||||||
|
show ? std::wcout << game->m_uGuessedNumber[i] : std::wcout << '#';
|
||||||
|
}
|
||||||
|
std::wcout << '\n' << '\t';
|
||||||
|
|
||||||
|
for (int i = 0; i < game->m_ucDigits; ++i)
|
||||||
|
{
|
||||||
|
std::wcout << '-';
|
||||||
|
}
|
||||||
|
std::wcout << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "CGame.h"
|
#include "CGame.h"
|
||||||
|
|
||||||
static class CPrint
|
class CPrint
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void GameHeader();
|
|
||||||
static void GameFooter(CGame*);
|
|
||||||
static void GameInitialQuery();
|
static void GameInitialQuery();
|
||||||
|
static void GameHeader();
|
||||||
|
static void GameFooter(CGame* const);
|
||||||
|
static void Steps(CGame* const);
|
||||||
|
static void GuessedNumber(CGame* const, const bool);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user