Object Transfer and Object Oriented Design

I study programming at university. For practice, I am writing a program for blackjack. I use C ++ and do an object oriented approach to this.

I have a Deck class that basically creates and shuffles a deck of cards. The created deck consists of an array of 52 objects of the card class. This is what I still have.

My plan is to have a Dealer object that has a deck of 52 cards, makes a card to the second game object, and then processes the Dealer’s own hand.

My first question is: Is it nice to make an array of Card objects open in the Deck class?

I ask about this because I consider the array as an attribute and taught that most attributes should be private. I don’t want to start using bad or lazy practices in my projects and I want to do it right.

Another question: How do objects, such as the map object used in my blackjack program, usually move from an object, such as a dealer, to a second object, such as a player?

+3
source share
4 answers

My first question: is the practice of creating an array of Card objects in the Deck class inconvenient?

. . , , , , . , . , , , Card , , , , , . Card , ; , , , .

: , , , , , , , ?

, , . , , ( ). , , . , , , :

, :

, , .

- const T& T - if:

  • .
  • .
  • , , , , .

- T& T - if:

  • .
  • / .
  • , , , , .

const - const shared_ptr<const T>& T - if:

  • , .
  • , , , .

- const shared_ptr<T>& T - if:

  • , .

; , , , . , , boost:: call_traits <T> :: param_type ( , , ).

+3

, , . - . , , ; , . - std::vector<card>, ( ).

std::vector<card> . , deal ( ).

+1

1) , . , , , .

2) :

struct Card
{
    Suit suit;
    Rank rank;
};

class Player
{
private:
    void AddCard(Card card);
    friend class Dealer;
};

class Dealer : public Player
{
public:
    void DealTo(Player& player);
};

Dealer dealer;
Player player2;
dealer.DealTo(player2);
0

: Card Deck?

. , , . , , , .

, . (, , , . , , , ).

: .
.

Another question: how do objects, such as the map object used in my blackjack program, usually move from an object, such as a dealer, to a second object, such as a player?

Remove it from the array in one object (and reduce the array to show that it has fewer cards (so you might want a container type that can vary in size)) Then put it in another array (container) in the target object.

0
source

Source: https://habr.com/ru/post/1754539/


All Articles