From 44a72d05ac093f0945442cd84cfe9648d2875fc8 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sun, 4 Jul 2021 03:58:03 +0300 Subject: [PATCH] lesson 5 --- lesson_5/exercise_1.hpp | 29 ++++++++++ lesson_5/exercise_2.hpp | 29 ++++++++++ lesson_5/exercise_3.hpp | 14 +++++ lesson_5/exercise_4.hpp | 121 ++++++++++++++++++++++++++++++++++++++++ lesson_5/main.cpp | 43 ++++++++++++++ 5 files changed, 236 insertions(+) create mode 100644 lesson_5/exercise_1.hpp create mode 100644 lesson_5/exercise_2.hpp create mode 100644 lesson_5/exercise_3.hpp create mode 100644 lesson_5/exercise_4.hpp create mode 100644 lesson_5/main.cpp diff --git a/lesson_5/exercise_1.hpp b/lesson_5/exercise_1.hpp new file mode 100644 index 0000000..09af3fd --- /dev/null +++ b/lesson_5/exercise_1.hpp @@ -0,0 +1,29 @@ +#ifndef EXERCISE_1_HPP_ +#define EXERCISE_1_HPP_ + +template +class Pair1 +{ +private: + T m_first; + T m_second; +public: + Pair1(const T& first, const T& second) : m_first(first), m_second(second) { } + + const T& first() const; + const T& second() const; +}; + +template +const T& Pair1::first() const +{ + return m_first; +} + +template +const T& Pair1::second() const +{ + return m_second; +} + +#endif diff --git a/lesson_5/exercise_2.hpp b/lesson_5/exercise_2.hpp new file mode 100644 index 0000000..1be4339 --- /dev/null +++ b/lesson_5/exercise_2.hpp @@ -0,0 +1,29 @@ +#ifndef EXERCISE_2_HPP_ +#define EXERCISE_2_HPP_ + +template +class Pair +{ +private: + T1 m_first; + T2 m_second; +public: + Pair(const T1& first, const T2& second) : m_first(first), m_second(second) { } + + const T1& first() const; + const T2& second() const; +}; + +template +const T1& Pair::first() const +{ + return m_first; +} + +template +const T2& Pair::second() const +{ + return m_second; +} + +#endif diff --git a/lesson_5/exercise_3.hpp b/lesson_5/exercise_3.hpp new file mode 100644 index 0000000..51a062d --- /dev/null +++ b/lesson_5/exercise_3.hpp @@ -0,0 +1,14 @@ +#ifndef EXERCISE_3_HPP_ +#define EXERCISE_3_HPP_ + +#include "exercise_2.hpp" + +template +class StringValuePair : public Pair +{ +public: + StringValuePair(const std::string& first, const T2& second) : + Pair(first, second) {} +}; + +#endif diff --git a/lesson_5/exercise_4.hpp b/lesson_5/exercise_4.hpp new file mode 100644 index 0000000..e6fab5b --- /dev/null +++ b/lesson_5/exercise_4.hpp @@ -0,0 +1,121 @@ +#ifndef EXERCISE_4_HPP_ +#define EXERCISE_4_HPP_ + +#include +#include + +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::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::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 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 diff --git a/lesson_5/main.cpp b/lesson_5/main.cpp new file mode 100644 index 0000000..f5c2fc7 --- /dev/null +++ b/lesson_5/main.cpp @@ -0,0 +1,43 @@ +#include +#include "exercise_1.hpp" +#include "exercise_2.hpp" +#include "exercise_3.hpp" + +using namespace std; + +void ex_1(); +void ex_2(); +void ex_3(); + +int main() +{ + ex_1(); + ex_2(); + ex_3(); + + return 0; +} + +void ex_1() +{ + Pair1 p1(6, 9); + cout << "Pair: " << p1.first() << ' ' << p1.second() << '\n'; + + const Pair1 p2(3.4, 7.8); + cout << "Pair: " << p2.first() << ' ' << p2.second() << '\n'; +} + +void ex_2() +{ + Pair p1(6, 7.8); + cout << "Pair: " << p1.first() << ' ' << p1.second() << '\n'; + + const Pair p2(3.4, 5); + cout << "Pair: " << p2.first() << ' ' << p2.second() << '\n'; +} + +void ex_3() +{ + StringValuePair svp("Amazing", 7); + std::cout << "Pair: " << svp.first() << ' ' << svp.second() << '\n'; +} -- 2.40.1