mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-29 00:12:46 +03:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5a4fa61ed2 | |||
| 8197a64803 | |||
| e75d82e896 | |||
| 7669dff7a6 |
@@ -1,8 +1,7 @@
|
|||||||
// CGame.cpp
|
// Bulls and Cows the game
|
||||||
|
// CGame.cpp
|
||||||
//
|
//
|
||||||
#include "CGame.h"
|
#include "CGame.h"
|
||||||
#include "CUserInput.h"
|
|
||||||
#include <random>
|
|
||||||
|
|
||||||
void CGame::Init()
|
void CGame::Init()
|
||||||
{
|
{
|
||||||
@@ -36,6 +35,7 @@ void CGame::Init()
|
|||||||
void CGame::Start()
|
void CGame::Start()
|
||||||
{
|
{
|
||||||
CStep step;
|
CStep step;
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
while (!m_fGameIsEnd)
|
while (!m_fGameIsEnd)
|
||||||
@@ -43,33 +43,10 @@ void CGame::Start()
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
PrintGameHeader();
|
PrintGameHeader();
|
||||||
ShowGuessedNumber(false);
|
ShowGuessedNumber(IS_SHOW);
|
||||||
PrintSteps();
|
PrintSteps();
|
||||||
ret = GetNumber(step);
|
} while (!GetNumber(step));
|
||||||
switch (ret)
|
|
||||||
{
|
|
||||||
case -1:
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case -2:
|
|
||||||
{
|
|
||||||
m_sLastError = L"Вы должны ввести " + std::to_wstring(m_ucDigits) + L"-значное число!";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case -3:
|
|
||||||
{
|
|
||||||
m_sLastError = L"Цифры в числе не должны повторяться!";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
m_sLastError = L"";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (ret < 0);
|
|
||||||
m_uStepCounter++;
|
m_uStepCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +56,40 @@ void CGame::Start()
|
|||||||
PrintGameFooter();
|
PrintGameFooter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CGame::GetNumber(CStep step)
|
||||||
|
{
|
||||||
|
std::wcout << L"\t\t\t\t\t" << m_sLastError << L"\r";
|
||||||
|
std::wcout << L"=>\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)
|
void CGame::ShowGuessedNumber(bool show)
|
||||||
{
|
{
|
||||||
std::wcout << '\t';
|
std::wcout << '\t';
|
||||||
@@ -98,8 +109,8 @@ void CGame::ShowGuessedNumber(bool show)
|
|||||||
void CGame::PrintGameHeader()
|
void CGame::PrintGameHeader()
|
||||||
{
|
{
|
||||||
std::system("cls");
|
std::system("cls");
|
||||||
std::wcout << strGAMENAME << ' ' << strGAMEVERSION << ' ' << VERSION << std::endl;
|
std::wcout << L"Быки и Коровы." << ' ' << L"Версия" << ' ' << VERSION << std::endl;
|
||||||
std::wcout << strGAMECOPYRIGHT << '\n' << std::endl;
|
std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << "\n\n" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGame::PrintGameFooter()
|
void CGame::PrintGameFooter()
|
||||||
@@ -123,44 +134,6 @@ void CGame::PrintSteps()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int CGame::GetNumber(CStep step)
|
|
||||||
{
|
|
||||||
std::vector<int> digits;
|
|
||||||
std::wstring sNumber;
|
|
||||||
wchar_t c;
|
|
||||||
|
|
||||||
std::wcout << L"\t\t\t\t\t" << m_sLastError << L"\r";
|
|
||||||
std::wcout << L"=>\t";
|
|
||||||
std::wcin >> sNumber;
|
|
||||||
if (sNumber == L"Q" || sNumber == L"q") return -1;
|
|
||||||
if (sNumber.length() != m_ucDigits) return -2;
|
|
||||||
|
|
||||||
for (int i = 0; i < sNumber.length(); i++)
|
|
||||||
{
|
|
||||||
c = sNumber[i];
|
|
||||||
int r = std::iswdigit(c);
|
|
||||||
if (!(bool)r)
|
|
||||||
{
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (IsEqual(sNumber, c, i))
|
|
||||||
{
|
|
||||||
return -3;
|
|
||||||
}
|
|
||||||
digits.push_back(((int)c) - 48); // Convert from char 'n' to number n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
step.StoreStepNumber(digits);
|
|
||||||
step.CheckForAnimals(m_uGuessedNumber, m_ucDigits);
|
|
||||||
m_Steps.push_back(step);
|
|
||||||
if (step.GetStepAnimals().first == m_ucDigits) m_fGameIsEnd = true;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
CGame::CGame()
|
CGame::CGame()
|
||||||
{
|
{
|
||||||
CUserInput userInput(1);
|
CUserInput userInput(1);
|
||||||
@@ -170,13 +143,13 @@ CGame::CGame()
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
PrintGameHeader();
|
PrintGameHeader();
|
||||||
std::wcout << L"Введите количество цифр в числе (от 3 до 7).\n";
|
std::wcout << L"Введите количество цифр в числе (от 3 до 5).\n";
|
||||||
std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n";
|
std::wcout << L"Чем больше цифр - тем сложнее. Обычно 4.\n\n";
|
||||||
std::wcout << L"=> ";
|
std::wcout << L"=> ";
|
||||||
|
|
||||||
if (userInput.Get())
|
if (userInput.Get())
|
||||||
{
|
{
|
||||||
if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 7))
|
if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 5))
|
||||||
{
|
{
|
||||||
m_ucDigits = userInput.m_vUserInput.at(0);
|
m_ucDigits = userInput.m_vUserInput.at(0);
|
||||||
ok = true;
|
ok = true;
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CGame.h
|
// CGame.h
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "defines.h"
|
#include "headers.h"
|
||||||
#include "CUserInput.h"
|
#include "CUserInput.h"
|
||||||
#include "CStep.h"
|
#include "CStep.h"
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ private:
|
|||||||
std::wstring m_sLastError;
|
std::wstring m_sLastError;
|
||||||
std::vector<CStep> m_Steps;
|
std::vector<CStep> m_Steps;
|
||||||
|
|
||||||
int GetNumber(CStep);
|
bool GetNumber(CStep);
|
||||||
void ShowGuessedNumber(bool);
|
void ShowGuessedNumber(bool);
|
||||||
void PrintGameHeader();
|
void PrintGameHeader();
|
||||||
void PrintGameFooter();
|
void PrintGameFooter();
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CStep.cpp
|
// CStep.cpp
|
||||||
//
|
//
|
||||||
#include "CStep.h"
|
#include "CStep.h"
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CStep.h
|
// CStep.h
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <vector>
|
#include "headers.h"
|
||||||
|
|
||||||
class CStep
|
class CStep
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CUserInput.cpp
|
// CUserInput.cpp
|
||||||
//
|
//
|
||||||
#include "CUserInput.h"
|
#include "CUserInput.h"
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CUserInput.h
|
// CUserInput.h
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "defines.h"
|
#include "headers.h"
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include <cwctype>
|
|
||||||
|
|
||||||
class CUserInput
|
class CUserInput
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -142,6 +142,7 @@
|
|||||||
<ClInclude Include="CStep.h" />
|
<ClInclude Include="CStep.h" />
|
||||||
<ClInclude Include="CUserInput.h" />
|
<ClInclude Include="CUserInput.h" />
|
||||||
<ClInclude Include="defines.h" />
|
<ClInclude Include="defines.h" />
|
||||||
|
<ClInclude Include="headers.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|||||||
@@ -47,5 +47,8 @@
|
|||||||
<ClInclude Include="defines.h">
|
<ClInclude Include="defines.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="headers.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// bulls-and-cows.cpp
|
// bulls-and-cows.cpp
|
||||||
//
|
//
|
||||||
#include <Windows.h>
|
#include "headers.h"
|
||||||
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
||||||
#include "CGame.h"
|
#include "CGame.h"
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// defines.h
|
||||||
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#define VERSION 0.4
|
||||||
|
|
||||||
#define VERSION 0.3
|
#ifdef _DEBUG
|
||||||
#define strGAMENAME L"Áûêè è Êîðîâû."
|
#define IS_SHOW true
|
||||||
#define strGAMEVERSION L"Âåðñèÿ"
|
#else
|
||||||
#define strGAMECOPYRIGHT L"Copyright (c) 2023 by W0LF aka 'dreamforce'"
|
#define IS_SHOW false
|
||||||
|
#endif
|
||||||
|
|||||||
12
bulls-and-cows-cpp/headers.h
Normal file
12
bulls-and-cows-cpp/headers.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// headers.h
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include <Windows.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <random>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <cwctype>
|
||||||
|
|
||||||
|
#include "defines.h"
|
||||||
Reference in New Issue
Block a user