mirror of
https://gitflic.ru/project/w0lf/bulls-and-cows-cpp.git
synced 2026-03-28 16:02:46 +03:00
The game are working!
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
// CGame.cpp
|
||||
// CGame.cpp
|
||||
//
|
||||
#include "CGame.h"
|
||||
//#include <vector>
|
||||
#include <string>
|
||||
#include <random>
|
||||
#include <cwctype>
|
||||
#include <iostream>
|
||||
|
||||
// Initialize the variables
|
||||
void CGame::Init()
|
||||
{
|
||||
m_fGameIsEnd = false;
|
||||
m_ucBulls = 0;
|
||||
m_ucCows = 0;
|
||||
m_uStepCounter = 0;
|
||||
@@ -23,29 +26,112 @@ void CGame::Init()
|
||||
n = d(e);
|
||||
if (i > 0)
|
||||
{
|
||||
while (CGame::IsEqual(n, i))
|
||||
while (IsEqual(n, i))
|
||||
{
|
||||
n = d(e);
|
||||
}
|
||||
}
|
||||
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);
|
||||
if (ret == -1) return;
|
||||
} while (ret < 0);
|
||||
}
|
||||
|
||||
PrintGameHeader();
|
||||
ShowGuessedNumber(true);
|
||||
PrintSteps();
|
||||
PrintGameFooter();
|
||||
}
|
||||
|
||||
void CGame::ShowGuessedNumber(bool show)
|
||||
{
|
||||
std::wcout << '\t';
|
||||
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()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
void CGame::PrintGameFooter()
|
||||
{
|
||||
std::wcout << L"\n\n\t" << L"!!! П О Б Е Д А !!!" << 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;
|
||||
|
||||
std::wcout << L"=>\t";
|
||||
std::wcin >> sNumber;
|
||||
if (sNumber == L"Q" || sNumber == L"q") return -1;
|
||||
if (sNumber.length() != m_ucDigits) return -2;
|
||||
|
||||
for (wchar_t& c : sNumber)
|
||||
{
|
||||
int r = std::iswdigit(c);
|
||||
if (!(bool)r)
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
else
|
||||
{
|
||||
int r = ((int)c) - 48;
|
||||
digits.push_back(r);
|
||||
}
|
||||
}
|
||||
|
||||
step.StoreStepNumber(digits);
|
||||
step.CheckForAnimals(m_uGuessedNumber, m_ucDigits);
|
||||
m_Steps.push_back(step);
|
||||
if (step.GetStepAnimals().first == 4) m_fGameIsEnd = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool CGame::IsEqual(int n, int j)
|
||||
@@ -61,7 +147,7 @@ bool CGame::IsEqual(int n, int j)
|
||||
return result;
|
||||
}
|
||||
|
||||
CGame::CGame(int digits)
|
||||
CGame::CGame(int d)
|
||||
{
|
||||
m_ucDigits = digits;
|
||||
m_ucDigits = d;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
//
|
||||
#pragma once
|
||||
#include "CStep.h"
|
||||
#include <vector>
|
||||
|
||||
#define VERSION 0.1
|
||||
|
||||
@@ -10,18 +9,23 @@ class CGame
|
||||
{
|
||||
private:
|
||||
unsigned char m_ucDigits;
|
||||
int m_uStepCounter;
|
||||
int* m_uGuessedNumber;
|
||||
std::vector<CStep> m_Step;
|
||||
|
||||
bool IsEqual(int, int);
|
||||
|
||||
public:
|
||||
unsigned char m_ucBulls;
|
||||
unsigned char m_ucCows;
|
||||
bool m_fGameIsEnd;
|
||||
int m_uStepCounter;
|
||||
int* m_uGuessedNumber;
|
||||
std::vector<CStep> m_Steps;
|
||||
|
||||
void Init();
|
||||
void ShowGuessedNumber();
|
||||
bool IsEqual(int, int);
|
||||
int GetNumber(CStep);
|
||||
void ShowGuessedNumber(bool);
|
||||
void PrintGameHeader();
|
||||
void PrintGameFooter();
|
||||
void PrintSteps();
|
||||
|
||||
public:
|
||||
void Init();
|
||||
void Start();
|
||||
|
||||
CGame(int);
|
||||
};
|
||||
|
||||
@@ -2,3 +2,44 @@
|
||||
//
|
||||
#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;
|
||||
}
|
||||
|
||||
CStep::CStep()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
// CStep.h
|
||||
//
|
||||
#pragma once
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
class CStep
|
||||
{
|
||||
private:
|
||||
std::vector<int> m_uStepNumber;
|
||||
std::pair<int, int> m_uStepAnimals;
|
||||
|
||||
public:
|
||||
std::tuple<int, int, int, int> m_uStepNumber;
|
||||
std::tuple<unsigned char, unsigned char> m_ucStepAnimals;
|
||||
void StoreStepNumber(std::vector<int>&);
|
||||
void CheckForAnimals(const int*, int);
|
||||
std::vector<int> GetStepNumber();
|
||||
std::pair<int, int> GetStepAnimals();
|
||||
CStep();
|
||||
};
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <Windows.h>
|
||||
#include "..\..\..\MyFunctions\SetUserLocale.h"
|
||||
#include "CGame.h"
|
||||
#include <iostream>
|
||||
|
||||
int wmain(int argc, wchar_t* argv[])
|
||||
{
|
||||
@@ -10,10 +11,7 @@ int wmain(int argc, wchar_t* argv[])
|
||||
|
||||
CGame game(4);
|
||||
game.Init();
|
||||
game.PrintGameHeader();
|
||||
|
||||
|
||||
|
||||
game.Start();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user