mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-29 00:12:46 +03:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ba43d72188 | |||
| 2d9c1c1d57 | |||
| a34da8d34f | |||
| 2bc245354c | |||
| c0dfdf53bb | |||
| 4b634caf52 | |||
| ae4230e721 |
@@ -1,15 +1,19 @@
|
|||||||
// CGame.cpp
|
// CGame.cpp
|
||||||
//
|
//
|
||||||
#include "CGame.h"
|
#include "CGame.h"
|
||||||
|
#include <string>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <cwctype>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// Initialize the variables
|
void CGame::Init(int digits)
|
||||||
void CGame::Init()
|
|
||||||
{
|
{
|
||||||
|
m_ucDigits = digits;
|
||||||
|
m_fGameIsEnd = false;
|
||||||
m_ucBulls = 0;
|
m_ucBulls = 0;
|
||||||
m_ucCows = 0;
|
m_ucCows = 0;
|
||||||
m_uStepCounter = 0;
|
m_uStepCounter = 0;
|
||||||
|
m_sLastError = L"";
|
||||||
|
|
||||||
std::random_device r;
|
std::random_device r;
|
||||||
std::default_random_engine e(r());
|
std::default_random_engine e(r());
|
||||||
@@ -23,45 +27,145 @@ void CGame::Init()
|
|||||||
n = d(e);
|
n = d(e);
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
{
|
{
|
||||||
while (CGame::IsEqual(n, i))
|
while (IsEqual(m_uGuessedNumber, n, i))
|
||||||
{
|
{
|
||||||
n = d(e);
|
n = d(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_uGuessedNumber[i] = n;
|
m_uGuessedNumber[i] = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGame::ShowGuessedNumber()
|
void CGame::Start()
|
||||||
{
|
{
|
||||||
|
CStep step;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
while (!m_fGameIsEnd)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
PrintGameHeader();
|
||||||
|
ShowGuessedNumber(false);
|
||||||
|
PrintSteps();
|
||||||
|
ret = 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;
|
||||||
|
}
|
||||||
|
case -4:
|
||||||
|
{
|
||||||
|
m_sLastError = L"Цифры в числе не должны повторяться!";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
m_sLastError = L"";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (ret < 0);
|
||||||
|
m_uStepCounter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintGameHeader();
|
||||||
|
ShowGuessedNumber(true);
|
||||||
|
PrintSteps();
|
||||||
|
PrintGameFooter();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CGame::ShowGuessedNumber(bool show)
|
||||||
|
{
|
||||||
|
std::wcout << '\t';
|
||||||
for (int i = 0; i < m_ucDigits; ++i)
|
for (int i = 0; i < m_ucDigits; ++i)
|
||||||
{
|
{
|
||||||
std::cout << m_uGuessedNumber[i];
|
show ? std::wcout << m_uGuessedNumber[i] : std::wcout << '#';
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::wcout << '\n' << '\t';
|
||||||
|
|
||||||
|
for (int i = 0; i < m_ucDigits; ++i)
|
||||||
|
{
|
||||||
|
std::wcout << '-';
|
||||||
|
}
|
||||||
|
std::wcout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CGame::PrintGameHeader()
|
void CGame::PrintGameHeader()
|
||||||
{
|
{
|
||||||
std::wcout << L"Áûêè è Êîðîâû. Âåðñèÿ " << VERSION << std::endl;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CGame::IsEqual(int n, int j)
|
void CGame::PrintGameFooter()
|
||||||
{
|
{
|
||||||
j--;
|
std::wcout << L"\n\n\t" << L"!!! П О Б Е Д А !!!" << std::endl;
|
||||||
bool result = false;
|
std::wcout << L"\tКоличество ходов: " << m_uStepCounter << L"\n\n" << std::endl;
|
||||||
|
|
||||||
while (j >= 0)
|
|
||||||
{
|
|
||||||
if (n == m_uGuessedNumber[j]) result = true;
|
|
||||||
j--;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CGame::CGame(int digits)
|
void CGame::PrintSteps()
|
||||||
{
|
{
|
||||||
m_ucDigits = digits;
|
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";
|
||||||
|
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 -3;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (IsEqual(sNumber, c, i))
|
||||||
|
{
|
||||||
|
return -4;
|
||||||
|
}
|
||||||
|
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 == 4) m_fGameIsEnd = true;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,26 +2,52 @@
|
|||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "CStep.h"
|
#include "CStep.h"
|
||||||
#include <vector>
|
#include <string>
|
||||||
|
|
||||||
#define VERSION 0.1
|
#define VERSION 0.2
|
||||||
|
|
||||||
class CGame
|
class CGame
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
unsigned char m_ucDigits;
|
int m_ucDigits;
|
||||||
|
int m_ucBulls;
|
||||||
|
int m_ucCows;
|
||||||
|
bool m_fGameIsEnd;
|
||||||
int m_uStepCounter;
|
int m_uStepCounter;
|
||||||
int* m_uGuessedNumber;
|
int* m_uGuessedNumber;
|
||||||
std::vector<CStep> m_Step;
|
std::wstring m_sLastError;
|
||||||
|
std::vector<CStep> m_Steps;
|
||||||
|
|
||||||
bool IsEqual(int, int);
|
template<typename T, typename N>
|
||||||
|
bool IsEqual(const T, const N, int);
|
||||||
|
|
||||||
|
int GetNumber(CStep);
|
||||||
|
|
||||||
|
void ShowGuessedNumber(bool);
|
||||||
|
|
||||||
|
void PrintGameHeader();
|
||||||
|
|
||||||
|
void PrintGameFooter();
|
||||||
|
|
||||||
|
void PrintSteps();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
unsigned char m_ucBulls;
|
|
||||||
unsigned char m_ucCows;
|
|
||||||
|
|
||||||
void Init();
|
void Init(int);
|
||||||
void ShowGuessedNumber();
|
|
||||||
void PrintGameHeader();
|
void Start();
|
||||||
CGame(int);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T, typename N>
|
||||||
|
bool CGame::IsEqual(const T number, const N digit, int i)
|
||||||
|
{
|
||||||
|
i--;
|
||||||
|
bool result = false;
|
||||||
|
|
||||||
|
while (i >= 0)
|
||||||
|
{
|
||||||
|
if (digit == number[i]) result = true;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,3 +2,40 @@
|
|||||||
//
|
//
|
||||||
#include "CStep.h"
|
#include "CStep.h"
|
||||||
|
|
||||||
|
void CStep::StoreStepNumber(std::vector<int>& digits)
|
||||||
|
{
|
||||||
|
m_uStepNumber = digits;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CStep::CheckForAnimals(const int* digits, int size)
|
||||||
|
{
|
||||||
|
int b = 0, c = 0;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < size; i++) // m_uGuessedNumber
|
||||||
|
{
|
||||||
|
for (size_t j = 0; j < size; j++) // m_uStepNumber
|
||||||
|
{
|
||||||
|
if ((m_uStepNumber[i] == digits[j]) && (i == j))
|
||||||
|
{
|
||||||
|
b++;
|
||||||
|
}
|
||||||
|
else if (m_uStepNumber[i] == digits[j])
|
||||||
|
{
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_uStepAnimals.first = b;
|
||||||
|
m_uStepAnimals.second = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> CStep::GetStepNumber()
|
||||||
|
{
|
||||||
|
return m_uStepNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<int, int> CStep::GetStepAnimals()
|
||||||
|
{
|
||||||
|
return m_uStepAnimals;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,11 +1,17 @@
|
|||||||
// CStep.h
|
// CStep.h
|
||||||
//
|
//
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <tuple>
|
#include <vector>
|
||||||
|
|
||||||
class CStep
|
class CStep
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
std::vector<int> m_uStepNumber;
|
||||||
|
std::pair<int, int> m_uStepAnimals;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::tuple<int, int, int, int> m_uStepNumber;
|
void StoreStepNumber(std::vector<int>&);
|
||||||
std::tuple<unsigned char, unsigned char> m_ucStepAnimals;
|
void CheckForAnimals(const int*, int);
|
||||||
|
std::vector<int> GetStepNumber();
|
||||||
|
std::pair<int, int> GetStepAnimals();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,17 +3,16 @@
|
|||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include "..\..\..\MyFunctions\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(4);
|
CGame game;
|
||||||
game.Init();
|
|
||||||
game.PrintGameHeader();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
game.Init(4);
|
||||||
|
game.Start();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user