mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-29 00:12:46 +03:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d034dea3af | |||
| f52d7bd6a3 | |||
| b0607e3a05 | |||
| 126cc44276 | |||
| 54952ba062 | |||
| 808c45b999 | |||
| c759cd76e4 | |||
| d69a8f2e68 | |||
| 036a8e4645 | |||
| 5482fe1814 | |||
| f7572874b0 | |||
| 33d1f783ac | |||
| 872e390ce9 | |||
| 5a4fa61ed2 | |||
| 8197a64803 | |||
| e75d82e896 | |||
| 7669dff7a6 | |||
| 7edca0b5e6 | |||
| 340924213a | |||
| 3833461e90 | |||
| 3e22bb1dc1 | |||
| 324510173a |
19
LICENSE
Normal file
19
LICENSE
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2023 W0LF aka 'dreamforce'
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
@@ -1 +1,5 @@
|
|||||||
Игра "Быки и Коровы" (C++)
|
|
||||||
|
# Игра "Быки и Коровы" (C++)
|
||||||
|
|
||||||
|
Здесь должно быть<br>
|
||||||
|
описание игры.
|
||||||
|
|||||||
@@ -1,171 +1,129 @@
|
|||||||
// CGame.cpp
|
// Bulls and Cows the game
|
||||||
|
// CGame.cpp
|
||||||
//
|
//
|
||||||
|
#include "defines.h"
|
||||||
#include "CGame.h"
|
#include "CGame.h"
|
||||||
#include <string>
|
#include "CUserInput.h"
|
||||||
#include <random>
|
#include "CPrint.h"
|
||||||
#include <cwctype>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
void CGame::Init(int digits)
|
void CGame::Init()
|
||||||
{
|
{
|
||||||
m_ucDigits = digits;
|
m_nBulls = 0;
|
||||||
|
m_nCows = 0;
|
||||||
|
m_nStepCounter = 0;
|
||||||
m_fGameIsEnd = false;
|
m_fGameIsEnd = false;
|
||||||
m_ucBulls = 0;
|
|
||||||
m_ucCows = 0;
|
|
||||||
m_uStepCounter = 0;
|
|
||||||
m_sLastError = L"";
|
m_sLastError = L"";
|
||||||
|
|
||||||
std::random_device r;
|
std::random_device r;
|
||||||
std::default_random_engine e(r());
|
std::default_random_engine e(r());
|
||||||
std::uniform_int_distribution<int> d(0, 9);
|
std::uniform_int_distribution<int> d(0, 9);
|
||||||
|
|
||||||
m_uGuessedNumber = new int[m_ucDigits] { 0 };
|
m_nGuessedNumber = new int[m_nDigits] { 0 };
|
||||||
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
for (int i = 0; i < m_ucDigits; ++i)
|
for (int i = 0; i < m_nDigits; ++i)
|
||||||
{
|
{
|
||||||
n = d(e);
|
n = d(e);
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
{
|
{
|
||||||
while (IsEqual(m_uGuessedNumber, n, i))
|
while (IsEqual(m_nGuessedNumber, n, i))
|
||||||
{
|
{
|
||||||
n = d(e);
|
n = d(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_uGuessedNumber[i] = n;
|
m_nGuessedNumber[i] = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGame::Start()
|
void CGame::Start()
|
||||||
{
|
{
|
||||||
CStep step;
|
CStep step;
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
while (!m_fGameIsEnd)
|
while (!m_fGameIsEnd)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
PrintGameHeader();
|
CPrint::GameHeader();
|
||||||
ShowGuessedNumber(false);
|
CPrint::GuessedNumber(this, IS_SHOW);
|
||||||
PrintSteps();
|
CPrint::Steps(this);
|
||||||
ret = GetNumber(step);
|
} while (!GetNumber(step));
|
||||||
switch (ret)
|
|
||||||
{
|
m_nStepCounter++;
|
||||||
case -1:
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case -2:
|
|
||||||
{
|
|
||||||
m_sLastError = L"Вы должны ввести " + std::to_wstring(m_ucDigits) + L"-значное число!";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case -3:
|
|
||||||
{
|
|
||||||
m_sLastError = L"Вы должны ввести число!";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case -4:
|
|
||||||
{
|
|
||||||
m_sLastError = L"Цифры в числе не должны повторяться!";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
m_sLastError = L"";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (ret < 0);
|
|
||||||
m_uStepCounter++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintGameHeader();
|
CPrint::GameHeader();
|
||||||
ShowGuessedNumber(true);
|
CPrint::GuessedNumber(this, true);
|
||||||
PrintSteps();
|
CPrint::Steps(this);
|
||||||
PrintGameFooter();
|
CPrint::GameFooter(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGame::ShowGuessedNumber(bool show)
|
bool CGame::GetNumber(CStep step)
|
||||||
{
|
{
|
||||||
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"Быки и Коровы. Версия " << VERSION << std::endl;
|
|
||||||
std::wcout << L"Copyright (c) 2023 by W0LF aka 'dreamforce'" << std::endl;
|
|
||||||
std::wcout << 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::PrintSteps()
|
|
||||||
{
|
|
||||||
if (m_Steps.size() < 1) return;
|
|
||||||
|
|
||||||
for (CStep& s : m_Steps)
|
|
||||||
{
|
|
||||||
std::wstring stepNumber = L"\0";
|
|
||||||
for (int& d : s.GetStepNumber())
|
|
||||||
{
|
|
||||||
stepNumber += std::to_wstring(d);
|
|
||||||
}
|
|
||||||
std::wcout << '\t' << stepNumber << '\t' << s.GetStepAnimals().first << L" Б., " << s.GetStepAnimals().second << L" К." << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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\t\t\t\t" << m_sLastError << L"\r";
|
||||||
std::wcout << L"=>\t";
|
std::wcout << "=>\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++)
|
CUserInput userInput(m_nDigits);
|
||||||
|
|
||||||
|
if (userInput.Get())
|
||||||
{
|
{
|
||||||
c = sNumber[i];
|
for (unsigned i = 0; i < userInput.m_vUserInput.size(); i++)
|
||||||
int r = std::iswdigit(c);
|
|
||||||
if (!(bool)r)
|
|
||||||
{
|
{
|
||||||
return -3;
|
if (IsEqual(userInput.m_vUserInput, userInput.m_vUserInput.at(i), i))
|
||||||
|
{
|
||||||
|
m_sLastError = strERROR_1;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (IsEqual(sNumber, c, i))
|
m_sLastError = strERROR_2_1 + std::to_wstring(m_nDigits) + strERROR_2_2;
|
||||||
{
|
return false;
|
||||||
return -4;
|
|
||||||
}
|
|
||||||
digits.push_back(((int)c) - 48); // Convert from char 'n' to number n
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
step.StoreStepNumber(digits);
|
step.StoreStepNumber(userInput.m_vUserInput);
|
||||||
step.CheckForAnimals(m_uGuessedNumber, m_ucDigits);
|
step.CheckForAnimals(m_nGuessedNumber, m_nDigits);
|
||||||
m_Steps.push_back(step);
|
m_vSteps.push_back(step);
|
||||||
if (step.GetStepAnimals().first == 4) m_fGameIsEnd = true;
|
|
||||||
|
|
||||||
return 0;
|
if (step.GetStepAnimals().first == m_nDigits) m_fGameIsEnd = true;
|
||||||
|
|
||||||
|
m_sLastError = L"";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
CGame::CGame()
|
||||||
|
{
|
||||||
|
CUserInput userInput(1);
|
||||||
|
|
||||||
|
bool ok = false;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
CPrint::GameHeader();
|
||||||
|
CPrint::GameInitialQuery();
|
||||||
|
std::wcout << "=> ";
|
||||||
|
|
||||||
|
if (userInput.Get())
|
||||||
|
{
|
||||||
|
if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 5))
|
||||||
|
{
|
||||||
|
m_nDigits = userInput.m_vUserInput.at(0);
|
||||||
|
ok = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
userInput.m_vUserInput.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (!ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGame::~CGame()
|
||||||
|
{
|
||||||
|
delete[] m_nGuessedNumber;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,53 +1,43 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CGame.h
|
// CGame.h
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "CStep.h"
|
#include "CStep.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#define VERSION 0.2
|
|
||||||
|
|
||||||
class CGame
|
class CGame
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int m_ucDigits;
|
int m_nBulls;
|
||||||
int m_ucBulls;
|
int m_nCows;
|
||||||
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);
|
||||||
|
|
||||||
template<typename T, typename N>
|
template<typename T, typename N>
|
||||||
bool IsEqual(const T, const N, int);
|
bool IsEqual(const T, const N, int);
|
||||||
|
|
||||||
int GetNumber(CStep);
|
|
||||||
|
|
||||||
void ShowGuessedNumber(bool);
|
|
||||||
|
|
||||||
void PrintGameHeader();
|
|
||||||
|
|
||||||
void PrintGameFooter();
|
|
||||||
|
|
||||||
void PrintSteps();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
int m_nDigits; // Number of digits into the guessed number
|
||||||
|
int m_nStepCounter;
|
||||||
|
int* m_nGuessedNumber;
|
||||||
|
std::vector<CStep> m_vSteps;
|
||||||
|
|
||||||
void Init(int);
|
void Init();
|
||||||
|
|
||||||
void Start();
|
void Start();
|
||||||
|
CGame();
|
||||||
|
~CGame();
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, typename N>
|
template<typename T, typename N>
|
||||||
bool CGame::IsEqual(const T number, const N digit, int i)
|
bool CGame::IsEqual(const T number, const N digit, int i)
|
||||||
{
|
{
|
||||||
i--;
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
while (i >= 0)
|
while ((--i) >= 0)
|
||||||
{
|
{
|
||||||
if (digit == number[i]) result = true;
|
if (digit == number[i]) result = true;
|
||||||
i--;
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
54
bulls-and-cows-cpp/CPrint.cpp
Normal file
54
bulls-and-cows-cpp/CPrint.cpp
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// CPrint.cpp
|
||||||
|
//
|
||||||
|
#include "CPrint.h"
|
||||||
|
#include "defines.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void CPrint::GameInitialQuery()
|
||||||
|
{
|
||||||
|
std::wcout << strINITIAL_QUERY_1;
|
||||||
|
std::wcout << strINITIAL_QUERY_2 << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPrint::GameHeader()
|
||||||
|
{
|
||||||
|
std::system("cls");
|
||||||
|
std::wcout << strGAME_NAME << ' ' << strGAME_VERSION << ' ' << VERSION << std::endl;
|
||||||
|
std::wcout << strGAME_COPYRIGHT << "\n\n" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPrint::GameFooter(CGame* const game)
|
||||||
|
{
|
||||||
|
std::wcout << "\n\n\t" << strVICTORY << std::endl;
|
||||||
|
std::wcout << strNUM_STEPS << game->m_nStepCounter << "\n\n" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPrint::Steps(CGame* const game)
|
||||||
|
{
|
||||||
|
if (game->m_vSteps.size() < 1) return;
|
||||||
|
|
||||||
|
for (CStep& s : game->m_vSteps)
|
||||||
|
{
|
||||||
|
std::wcout << '\t';
|
||||||
|
for (int& d : s.GetStepNumber()) std::wcout << d;
|
||||||
|
std::wcout << '\t' << s.GetStepAnimals().first << strBULLS << s.GetStepAnimals().second << strCOWS;
|
||||||
|
std::wcout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CPrint::GuessedNumber(CGame* const game, const bool show)
|
||||||
|
{
|
||||||
|
std::wcout << '\t';
|
||||||
|
for (int i = 0; i < game->m_nDigits; ++i)
|
||||||
|
{
|
||||||
|
show ? std::wcout << game->m_nGuessedNumber[i] : std::wcout << '#';
|
||||||
|
}
|
||||||
|
std::wcout << '\n' << '\t';
|
||||||
|
|
||||||
|
for (int i = 0; i < game->m_nDigits; ++i)
|
||||||
|
{
|
||||||
|
std::wcout << '-';
|
||||||
|
}
|
||||||
|
std::wcout << std::endl;
|
||||||
|
}
|
||||||
15
bulls-and-cows-cpp/CPrint.h
Normal file
15
bulls-and-cows-cpp/CPrint.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// CPrint.h
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include "CGame.h"
|
||||||
|
|
||||||
|
class CPrint
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static void GameInitialQuery();
|
||||||
|
static void GameHeader();
|
||||||
|
static void GameFooter(CGame* const);
|
||||||
|
static void Steps(CGame* const);
|
||||||
|
static void GuessedNumber(CGame* const, const bool);
|
||||||
|
};
|
||||||
@@ -1,41 +1,42 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CStep.cpp
|
// CStep.cpp
|
||||||
//
|
//
|
||||||
#include "CStep.h"
|
#include "CStep.h"
|
||||||
|
|
||||||
void CStep::StoreStepNumber(std::vector<int>& digits)
|
void CStep::StoreStepNumber(std::vector<int>& digits)
|
||||||
{
|
{
|
||||||
m_uStepNumber = digits;
|
m_nStepNumber = digits;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CStep::CheckForAnimals(const int* digits, int size)
|
void CStep::CheckForAnimals(const int* digits, int size)
|
||||||
{
|
{
|
||||||
int b = 0, c = 0;
|
int b = 0, c = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < size; i++) // m_uGuessedNumber
|
for (size_t i = 0; i < size; i++) // m_nGuessedNumber
|
||||||
{
|
{
|
||||||
for (size_t j = 0; j < size; j++) // m_uStepNumber
|
for (size_t j = 0; j < size; j++) // m_nStepNumber
|
||||||
{
|
{
|
||||||
if ((m_uStepNumber[i] == digits[j]) && (i == j))
|
if ((m_nStepNumber[i] == digits[j]) && (i == j))
|
||||||
{
|
{
|
||||||
b++;
|
b++;
|
||||||
}
|
}
|
||||||
else if (m_uStepNumber[i] == digits[j])
|
else if (m_nStepNumber[i] == digits[j])
|
||||||
{
|
{
|
||||||
c++;
|
c++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_uStepAnimals.first = b;
|
m_nStepAnimals.first = b;
|
||||||
m_uStepAnimals.second = c;
|
m_nStepAnimals.second = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<int> CStep::GetStepNumber()
|
std::vector<int> CStep::GetStepNumber()
|
||||||
{
|
{
|
||||||
return m_uStepNumber;
|
return m_nStepNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<int, int> CStep::GetStepAnimals()
|
std::pair<int, int> CStep::GetStepAnimals()
|
||||||
{
|
{
|
||||||
return m_uStepAnimals;
|
return m_nStepAnimals;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// CStep.h
|
// CStep.h
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
@@ -6,8 +7,8 @@
|
|||||||
class CStep
|
class CStep
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::vector<int> m_uStepNumber;
|
std::vector<int> m_nStepNumber;
|
||||||
std::pair<int, int> m_uStepAnimals;
|
std::pair<int, int> m_nStepAnimals;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void StoreStepNumber(std::vector<int>&);
|
void StoreStepNumber(std::vector<int>&);
|
||||||
|
|||||||
39
bulls-and-cows-cpp/CUserInput.cpp
Normal file
39
bulls-and-cows-cpp/CUserInput.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// CUserInput.cpp
|
||||||
|
//
|
||||||
|
#include "CUserInput.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <cwctype>
|
||||||
|
|
||||||
|
CUserInput::CUserInput(int numOfDigits)
|
||||||
|
{
|
||||||
|
m_nNumOfExpectedDigits = numOfDigits;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CUserInput::Get()
|
||||||
|
{
|
||||||
|
std::wstring sDigits;
|
||||||
|
wchar_t cDigit;
|
||||||
|
int nDigit;
|
||||||
|
|
||||||
|
std::getline(std::wcin, sDigits);
|
||||||
|
|
||||||
|
if (sDigits.length() != m_nNumOfExpectedDigits)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < m_nNumOfExpectedDigits; i++)
|
||||||
|
{
|
||||||
|
cDigit = sDigits.at(i);
|
||||||
|
if (!(bool)std::iswdigit(cDigit))
|
||||||
|
{
|
||||||
|
m_vUserInput.clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
nDigit = ((int)cDigit) - 48;
|
||||||
|
m_vUserInput.push_back(nDigit);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
15
bulls-and-cows-cpp/CUserInput.h
Normal file
15
bulls-and-cows-cpp/CUserInput.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// CUserInput.h
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class CUserInput
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int m_nNumOfExpectedDigits;
|
||||||
|
std::vector<int> m_vUserInput;
|
||||||
|
|
||||||
|
CUserInput(int);
|
||||||
|
bool Get();
|
||||||
|
};
|
||||||
22
bulls-and-cows-cpp/SetUserLocale.cpp
Normal file
22
bulls-and-cows-cpp/SetUserLocale.cpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
// SetUserLocale.cpp
|
||||||
|
//
|
||||||
|
#include <iostream>
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
bool SetUserLocale(void)
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
wchar_t localeName[LOCALE_NAME_MAX_LENGTH]{ 0 };
|
||||||
|
if (GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH))
|
||||||
|
{
|
||||||
|
_wsetlocale(LC_ALL, localeName);
|
||||||
|
ret = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DWORD ec = GetLastError();
|
||||||
|
std::wcout << L"GetUserDefaultLocaleName() failed! Error: " << ec << std::endl;
|
||||||
|
ret = false;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
7
bulls-and-cows-cpp/SetUserLocale.h
Normal file
7
bulls-and-cows-cpp/SetUserLocale.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
// SetUserLocale.h
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#ifndef SETUSERLOCALE_H
|
||||||
|
#define SETUSERLOCALE_H
|
||||||
|
bool SetUserLocale(void);
|
||||||
|
#endif // SETUSERLOCALE_H
|
||||||
@@ -77,10 +77,12 @@
|
|||||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<LanguageStandard>stdcpp17</LanguageStandard>
|
<LanguageStandard>stdcpp17</LanguageStandard>
|
||||||
|
<DisableSpecificWarnings>4018;%(DisableSpecificWarnings)</DisableSpecificWarnings>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<RandomizedBaseAddress>false</RandomizedBaseAddress>
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
@@ -129,15 +131,23 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\..\MyFunctions\SetUserLocale.cpp" />
|
|
||||||
<ClCompile Include="bulls-and-cows.cpp" />
|
<ClCompile Include="bulls-and-cows.cpp" />
|
||||||
<ClCompile Include="CGame.cpp" />
|
<ClCompile Include="CGame.cpp" />
|
||||||
|
<ClCompile Include="CPrint.cpp" />
|
||||||
<ClCompile Include="CStep.cpp" />
|
<ClCompile Include="CStep.cpp" />
|
||||||
|
<ClCompile Include="CUserInput.cpp" />
|
||||||
|
<ClCompile Include="SetUserLocale.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\..\..\MyFunctions\SetUserLocale.h" />
|
|
||||||
<ClInclude Include="CGame.h" />
|
<ClInclude Include="CGame.h" />
|
||||||
|
<ClInclude Include="CPrint.h" />
|
||||||
<ClInclude Include="CStep.h" />
|
<ClInclude Include="CStep.h" />
|
||||||
|
<ClInclude Include="CUserInput.h" />
|
||||||
|
<ClInclude Include="defines.h" />
|
||||||
|
<ClInclude Include="SetUserLocale.h" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="LICENSE" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|||||||
@@ -27,6 +27,12 @@
|
|||||||
<ClCompile Include="CStep.cpp">
|
<ClCompile Include="CStep.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="CUserInput.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="CPrint.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="CGame.h">
|
<ClInclude Include="CGame.h">
|
||||||
@@ -38,5 +44,17 @@
|
|||||||
<ClInclude Include="CStep.h">
|
<ClInclude Include="CStep.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="CUserInput.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="defines.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="CPrint.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="LICENSE" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,17 +1,15 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
// bulls-and-cows.cpp
|
// bulls-and-cows.cpp
|
||||||
//
|
//
|
||||||
#include <Windows.h>
|
#include "SetUserLocale.h"
|
||||||
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
|
||||||
#include "CGame.h"
|
#include "CGame.h"
|
||||||
//#include <iostream>
|
|
||||||
|
|
||||||
int wmain(int argc, wchar_t* argv[])
|
int wmain(int argc, wchar_t* argv[])
|
||||||
{
|
{
|
||||||
if (!SetUserLocale()) return 1;
|
if (!SetUserLocale()) return 1;
|
||||||
|
|
||||||
CGame game;
|
CGame game;
|
||||||
|
game.Init();
|
||||||
game.Init(4);
|
|
||||||
game.Start();
|
game.Start();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
28
bulls-and-cows-cpp/defines.h
Normal file
28
bulls-and-cows-cpp/defines.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// Bulls and Cows the game
|
||||||
|
// defines.h
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
#define VERSION 0.6
|
||||||
|
|
||||||
|
#ifdef _DEBUG
|
||||||
|
#define IS_SHOW true
|
||||||
|
#else
|
||||||
|
#define IS_SHOW false
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define strERROR_1 L"Цифры в числе не должны повторяться!"
|
||||||
|
#define strERROR_2_1 L"Вы должны ввести "
|
||||||
|
#define strERROR_2_2 L"-значное число!"
|
||||||
|
|
||||||
|
#define strBULLS L" Б., "
|
||||||
|
#define strCOWS L" К."
|
||||||
|
|
||||||
|
#define strINITIAL_QUERY_1 L"Введите количество цифр в числе (от 3 до 5).\n"
|
||||||
|
#define strINITIAL_QUERY_2 L"Чем больше цифр - тем сложнее. Обычно 4.\n"
|
||||||
|
|
||||||
|
#define strGAME_NAME L"Быки и Коровы."
|
||||||
|
#define strGAME_VERSION L"Версия"
|
||||||
|
#define strGAME_COPYRIGHT L"Copyright (c) 2023 W0LF aka 'dreamforce'"
|
||||||
|
|
||||||
|
#define strVICTORY L"!!! П О Б Е Д А !!!"
|
||||||
|
#define strNUM_STEPS L"\tКоличество ходов: "
|
||||||
Reference in New Issue
Block a user