mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-29 00:12:46 +03:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d034dea3af | |||
| f52d7bd6a3 | |||
| b0607e3a05 | |||
| 126cc44276 | |||
| 54952ba062 | |||
| 808c45b999 | |||
| c759cd76e4 | |||
| d69a8f2e68 |
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>
|
||||||
|
описание игры.
|
||||||
|
|||||||
@@ -10,9 +10,9 @@
|
|||||||
|
|
||||||
void CGame::Init()
|
void CGame::Init()
|
||||||
{
|
{
|
||||||
m_ucBulls = 0;
|
m_nBulls = 0;
|
||||||
m_ucCows = 0;
|
m_nCows = 0;
|
||||||
m_uStepCounter = 0;
|
m_nStepCounter = 0;
|
||||||
m_fGameIsEnd = false;
|
m_fGameIsEnd = false;
|
||||||
m_sLastError = L"";
|
m_sLastError = L"";
|
||||||
|
|
||||||
@@ -20,21 +20,21 @@ void CGame::Init()
|
|||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ void CGame::Start()
|
|||||||
CPrint::Steps(this);
|
CPrint::Steps(this);
|
||||||
} while (!GetNumber(step));
|
} while (!GetNumber(step));
|
||||||
|
|
||||||
m_uStepCounter++;
|
m_nStepCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
CPrint::GameHeader();
|
CPrint::GameHeader();
|
||||||
@@ -67,7 +67,7 @@ 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";
|
||||||
std::wcout << "=>\t";
|
std::wcout << "=>\t";
|
||||||
|
|
||||||
CUserInput userInput(m_ucDigits);
|
CUserInput userInput(m_nDigits);
|
||||||
|
|
||||||
if (userInput.Get())
|
if (userInput.Get())
|
||||||
{
|
{
|
||||||
@@ -82,15 +82,15 @@ bool CGame::GetNumber(CStep step)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_sLastError = strERROR_2_1 + std::to_wstring(m_ucDigits) + strERROR_2_2;
|
m_sLastError = strERROR_2_1 + std::to_wstring(m_nDigits) + strERROR_2_2;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
step.StoreStepNumber(userInput.m_vUserInput);
|
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 == m_ucDigits) m_fGameIsEnd = true;
|
if (step.GetStepAnimals().first == m_nDigits) m_fGameIsEnd = true;
|
||||||
|
|
||||||
m_sLastError = L"";
|
m_sLastError = L"";
|
||||||
return true;
|
return true;
|
||||||
@@ -112,7 +112,7 @@ CGame::CGame()
|
|||||||
{
|
{
|
||||||
if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 5))
|
if ((userInput.m_vUserInput.at(0) >= 3) && (userInput.m_vUserInput.at(0) <= 5))
|
||||||
{
|
{
|
||||||
m_ucDigits = userInput.m_vUserInput.at(0);
|
m_nDigits = userInput.m_vUserInput.at(0);
|
||||||
ok = true;
|
ok = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -125,5 +125,5 @@ CGame::CGame()
|
|||||||
|
|
||||||
CGame::~CGame()
|
CGame::~CGame()
|
||||||
{
|
{
|
||||||
delete[] m_uGuessedNumber;
|
delete[] m_nGuessedNumber;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
class CGame
|
class CGame
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int m_ucBulls;
|
int m_nBulls;
|
||||||
int m_ucCows;
|
int m_nCows;
|
||||||
bool m_fGameIsEnd;
|
bool m_fGameIsEnd;
|
||||||
std::wstring m_sLastError;
|
std::wstring m_sLastError;
|
||||||
|
|
||||||
@@ -19,10 +19,10 @@ private:
|
|||||||
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_nDigits; // Number of digits into the guessed number
|
||||||
int m_uStepCounter;
|
int m_nStepCounter;
|
||||||
int* m_uGuessedNumber;
|
int* m_nGuessedNumber;
|
||||||
std::vector<CStep> m_Steps;
|
std::vector<CStep> m_vSteps;
|
||||||
|
|
||||||
void Init();
|
void Init();
|
||||||
void Start();
|
void Start();
|
||||||
|
|||||||
@@ -21,14 +21,14 @@ void CPrint::GameHeader()
|
|||||||
void CPrint::GameFooter(CGame* const game)
|
void CPrint::GameFooter(CGame* const game)
|
||||||
{
|
{
|
||||||
std::wcout << "\n\n\t" << strVICTORY << std::endl;
|
std::wcout << "\n\n\t" << strVICTORY << std::endl;
|
||||||
std::wcout << strNUM_STEPS << game->m_uStepCounter << "\n\n" << std::endl;
|
std::wcout << strNUM_STEPS << game->m_nStepCounter << "\n\n" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CPrint::Steps(CGame* const game)
|
void CPrint::Steps(CGame* const game)
|
||||||
{
|
{
|
||||||
if (game->m_Steps.size() < 1) return;
|
if (game->m_vSteps.size() < 1) return;
|
||||||
|
|
||||||
for (CStep& s : game->m_Steps)
|
for (CStep& s : game->m_vSteps)
|
||||||
{
|
{
|
||||||
std::wcout << '\t';
|
std::wcout << '\t';
|
||||||
for (int& d : s.GetStepNumber()) std::wcout << d;
|
for (int& d : s.GetStepNumber()) std::wcout << d;
|
||||||
@@ -40,13 +40,13 @@ void CPrint::Steps(CGame* const game)
|
|||||||
void CPrint::GuessedNumber(CGame* const game, const bool show)
|
void CPrint::GuessedNumber(CGame* const game, const bool show)
|
||||||
{
|
{
|
||||||
std::wcout << '\t';
|
std::wcout << '\t';
|
||||||
for (int i = 0; i < game->m_ucDigits; ++i)
|
for (int i = 0; i < game->m_nDigits; ++i)
|
||||||
{
|
{
|
||||||
show ? std::wcout << game->m_uGuessedNumber[i] : std::wcout << '#';
|
show ? std::wcout << game->m_nGuessedNumber[i] : std::wcout << '#';
|
||||||
}
|
}
|
||||||
std::wcout << '\n' << '\t';
|
std::wcout << '\n' << '\t';
|
||||||
|
|
||||||
for (int i = 0; i < game->m_ucDigits; ++i)
|
for (int i = 0; i < game->m_nDigits; ++i)
|
||||||
{
|
{
|
||||||
std::wcout << '-';
|
std::wcout << '-';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,38 +5,38 @@
|
|||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,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>&);
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <cwctype>
|
#include <cwctype>
|
||||||
|
|
||||||
CUserInput::CUserInput(int nNumOfDigits)
|
CUserInput::CUserInput(int numOfDigits)
|
||||||
{
|
{
|
||||||
m_nNumOfExpectedDigits = nNumOfDigits;
|
m_nNumOfExpectedDigits = numOfDigits;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CUserInput::Get()
|
bool CUserInput::Get()
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ public:
|
|||||||
int m_nNumOfExpectedDigits;
|
int m_nNumOfExpectedDigits;
|
||||||
std::vector<int> m_vUserInput;
|
std::vector<int> m_vUserInput;
|
||||||
|
|
||||||
CUserInput(int nNumOfExpectedDigits);
|
CUserInput(int);
|
||||||
bool Get();
|
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
|
||||||
@@ -131,20 +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="CPrint.cpp" />
|
||||||
<ClCompile Include="CStep.cpp" />
|
<ClCompile Include="CStep.cpp" />
|
||||||
<ClCompile Include="CUserInput.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="CPrint.h" />
|
||||||
<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="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">
|
||||||
|
|||||||
@@ -54,4 +54,7 @@
|
|||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="LICENSE" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
// Bulls and Cows the game
|
// Bulls and Cows the game
|
||||||
// bulls-and-cows.cpp
|
// bulls-and-cows.cpp
|
||||||
//
|
//
|
||||||
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
#include "SetUserLocale.h"
|
||||||
#include "CGame.h"
|
#include "CGame.h"
|
||||||
|
|
||||||
int wmain(int argc, wchar_t* argv[])
|
int wmain(int argc, wchar_t* argv[])
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// defines.h
|
// defines.h
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#define VERSION 0.4
|
#define VERSION 0.6
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
#define IS_SHOW true
|
#define IS_SHOW true
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
#define strGAME_NAME L"Быки и Коровы."
|
#define strGAME_NAME L"Быки и Коровы."
|
||||||
#define strGAME_VERSION L"Версия"
|
#define strGAME_VERSION L"Версия"
|
||||||
#define strGAME_COPYRIGHT L"Copyright (c) 2023 by W0LF aka 'dreamforce'"
|
#define strGAME_COPYRIGHT L"Copyright (c) 2023 W0LF aka 'dreamforce'"
|
||||||
|
|
||||||
#define strVICTORY L"!!! П О Б Е Д А !!!"
|
#define strVICTORY L"!!! П О Б Е Д А !!!"
|
||||||
#define strNUM_STEPS L"\tКоличество ходов: "
|
#define strNUM_STEPS L"\tКоличество ходов: "
|
||||||
|
|||||||
Reference in New Issue
Block a user