lesson 5 #5

Open
alexander wants to merge 1 commits from lesson_5 into master
5 changed files with 236 additions and 0 deletions
Showing only changes of commit 44a72d05ac - Show all commits

29
lesson_5/exercise_1.hpp Normal file
View File

@ -0,0 +1,29 @@
#ifndef EXERCISE_1_HPP_
#define EXERCISE_1_HPP_
template <typename T>
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 <typename T>
const T& Pair1<T>::first() const
{
return m_first;
}
template <typename T>
const T& Pair1<T>::second() const
{
return m_second;
}
#endif

29
lesson_5/exercise_2.hpp Normal file
View File

@ -0,0 +1,29 @@
#ifndef EXERCISE_2_HPP_
#define EXERCISE_2_HPP_
template <typename T1, typename T2>
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 <typename T1, typename T2>
const T1& Pair<T1, T2>::first() const
{
return m_first;
}
template <typename T1, typename T2>
const T2& Pair<T1, T2>::second() const
{
return m_second;
}
#endif

14
lesson_5/exercise_3.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef EXERCISE_3_HPP_
#define EXERCISE_3_HPP_
#include "exercise_2.hpp"
template <typename T2>
class StringValuePair : public Pair<std::string, T2>
{
public:
StringValuePair(const std::string& first, const T2& second) :
Pair<std::string, T2>(first, second) {}
};
#endif

121
lesson_5/exercise_4.hpp Normal file
View File

@ -0,0 +1,121 @@
#ifndef EXERCISE_4_HPP_
#define EXERCISE_4_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

43
lesson_5/main.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <iostream>
#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<int> p1(6, 9);
cout << "Pair: " << p1.first() << ' ' << p1.second() << '\n';
const Pair1<double> p2(3.4, 7.8);
cout << "Pair: " << p2.first() << ' ' << p2.second() << '\n';
}
void ex_2()
{
Pair<int, double> p1(6, 7.8);
cout << "Pair: " << p1.first() << ' ' << p1.second() << '\n';
const Pair<double, int> p2(3.4, 5);
cout << "Pair: " << p2.first() << ' ' << p2.second() << '\n';
}
void ex_3()
{
StringValuePair<int> svp("Amazing", 7);
std::cout << "Pair: " << svp.first() << ' ' << svp.second() << '\n';
}