This commit is contained in:
Alexander Zhirov 2021-07-08 17:54:37 +03:00
parent c451149748
commit 45ac9748ab
7 changed files with 285 additions and 0 deletions

121
lesson_6/Blackjack.hpp Normal file
View File

@ -0,0 +1,121 @@
#ifndef BLACKJACK_HPP_
#define BLACKJACK_HPP_
#include <iostream>
#include <vector>
using namespace std;
class Card
{
public:
enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};
Card(rank r = ACE, suit s, bool ifu = true): m_Rank(r), m_Suit(s), m_IsFaceUp(ifu) { }
int GetValue() const
{
int value = 0;
if (m_IsFaceUp)
{
value = m_Rank;
if (value > 10)
value = 10;
}
return value;
}
void Flip()
{
m_IsFaceUp = !m_IsFaceUp;
}
friend std::ostream& operator<<(std::ostream& s, const Card& aCard);
private:
rank m_Rank;
suit m_Suit;
bool m_IsFaceUp;
};
class Hand
{
public:
Hand()
{
m_Cards.reserve(7);
}
virtual ~Hand()
{
Clear();
}
void Add(Card* pCard)
{
m_Cards.push_back(pCard);
}
void Clear()
{
std::vector<Card*>::iterator iter = m_Cards.begin();
for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
{
delete *iter;
*iter = nullptr;
}
m_Cards.clear();
}
int GetTotal() const
{
if (m_Cards.empty() || m_Cards[0]->GetValue() == 0)
return 0;
int total = 0;
std::vector<Card*>::const_iterator iter;
for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
total += (*iter)->GetValue();
bool containsAce = false;
for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
if ((*iter)->GetValue() == Card::ACE)
containsAce = true;
if (containsAce && total <= 11) total += 10;
return total;
}
protected:
std::vector<Card*> m_Cards;
};
class GenericPlayer : public Hand
{
private:
friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);
protected:
string m_Name;
public:
GenericPlayer(const string& name = "") : m_Name(name) { }
virtual ~GenericPlayer();
virtual bool IsHitting() const = 0;
bool IsBusted() const
{
return (GetTotal() > 21);
}
void Bust() const
{
cout << m_Name << " перебор.\n";
}
};
#endif

35
lesson_6/exercise_1.hpp Normal file
View File

@ -0,0 +1,35 @@
#ifndef EXERCISE_1_HPP_
#define EXERCISE_1_HPP_
/*
* Едея заключается в том, чтобы проверять
* корректность ввода на наличие флага ошибок.
* Если good() == true - тогда чтение прошло без ошибок.
*/
#include <limits>
void exercise_1()
{
int num;
std::cout << "Введите целое число: ";
while (true)
{
std::cin >> num;
if (std::cin.good() && std::cin.peek() == '\n')
{
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
break;
}
std::cin.clear();
std::cout << "Введено некорректное число. Попробуйте ещё раз: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << "Введено целое число " << num << std::endl;
}
#endif

32
lesson_6/exercise_2.hpp Normal file
View File

@ -0,0 +1,32 @@
#ifndef EXERCISE_2_HPP_
#define EXERCISE_2_HPP_
#include <ostream>
//class endline
//{
//public:
// friend std::ostream& operator<<(std::ostream &out, const endline&);
//};
//
//std::ostream& operator<<(std::ostream &out, const endline& e)
//{
// out << "\n\n";
// out.flush();
//
// return out;
//}
//
//endline endll;
template<typename _CharT, typename _Traits>
inline std::basic_ostream<_CharT, _Traits>&
endll(std::basic_ostream<_CharT, _Traits>& __os)
{ return flush((__os.put(__os.widen('\n'))).put(__os.widen('\n'))); }
void exercise_2()
{
std::cout << "Hello" << endll << "world";
}
#endif

37
lesson_6/exercise_3.hpp Normal file
View File

@ -0,0 +1,37 @@
#ifndef EXERCISE_3_HPP_
#define EXERCISE_3_HPP_
#include "Blackjack.hpp"
class Player : public GenericPlayer
{
public:
Player(const string& name = "") : GenericPlayer(name) { }
virtual ~Player() { }
virtual bool IsHitting() const
{
cout << m_Name << ", do you want a hit? (Y/N): ";
char response;
cin >> response;
return (response == 'y' || response == 'Y');
}
void Win() const
{
cout << m_Name << " wins.\n";
}
void Lose() const
{
cout << m_Name << " loses.\n";
}
void Push() const
{
cout << m_Name << " pushes.\n";
}
};
#endif

27
lesson_6/exercise_4.hpp Normal file
View File

@ -0,0 +1,27 @@
#ifndef EXERCISE_4_HPP_
#define EXERCISE_4_HPP_
#include "Blackjack.hpp"
class House : public GenericPlayer
{
public:
House(const string& name = "House") : GenericPlayer(name) { }
virtual ~House() { }
virtual bool IsHitting() const
{
return (GetTotal() <= 16);
}
void FlipFirstCard()
{
if (!(m_Cards.empty()))
m_Cards[0]->Flip();
else
cout << "Нет карты для переворота!\n";
}
};
#endif

20
lesson_6/exercise_5.hpp Normal file
View File

@ -0,0 +1,20 @@
#ifndef EXERCISE_5_HPP_
#define EXERCISE_5_HPP_
#include "Blackjack.hpp"
ostream& operator<<(ostream& os, const Card& aCard)
{
const string RANKS[] = {"0", "A", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K"};
const string SUITS[] = {"c", "d", "h", "s"};
if (aCard.m_IsFaceUp)
os << RANKS[aCard.m_Rank] << SUITS[aCard.m_Suit];
else
os << "XX";
return os;
}
#endif

13
lesson_6/main.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
#include "exercise_1.hpp"
#include "exercise_2.hpp"
using namespace std;
int main()
{
exercise_1();
exercise_2();
return 0;
}