首页 > 解决方案 > 以一种有凝聚力的方式将班级聚集在一起

问题描述

我一直在做一个模拟战争纸牌游戏的工作,以收集关于需要多少回合才能完成的统计数据。

我创建了一个 Player 类和一个 Deck 类来处理它们各自的角色,但我对如何以一种有凝聚力的方式将它们组合在一起感到困惑。我需要一个可以玩牌的环境。我想主要处理这一切,但似乎那将是一团糟。

我还想过制作另一个由 Player 和 Deck 对象组成的类。我只是不知道将这些课程放在一起的最佳方法是什么。

玩家:

class Player
{
public:
    Player();
    void placeCardInHand(int); //cards placed in hand will be placed pushed to the front
    int getCardFromHand();  // cards leaving the hand will be popped from the front
    bool handIsEmpty(); //if the players hand is empty return true
private:
    deque<int> hand; // the players hand can hold an entire deck of cards.

};



 void Player::placeCardInHand(int aCard)
{
    hand.push_front(aCard); //card is pushed to the front of the hand
}

int Player::getCardFromHand()
{
    int temp = hand.front(); //get the first item in the hand
    hand.pop_front();   // remove the first item from the hand

    return temp;    //return that item
}

bool Player::handIsEmpty()
{
    return this->hand.empty(); 
}

甲板:

class Deck
{
public:
    Deck(); //deck starts with 52 cards in order 0 - 51

    void shuffleTheDeck();  //one call to this completely shuffles the deck
    int drawCard(); //gives the card on top then pops from the deck

    void showDeck();        //nessesary for testing

private:
    deque<int> theDeck;
};

Deck::Deck()
{
    for (int i = 0; i < 52; i++)
    {
        this->theDeck.push_front(i); //fill the deck with 52 cards 0 - 51
    }
}

void Deck::shuffleTheDeck() //pick two random positions and swap the cards
{
    int temp;
    int position1;
    int position2;
    for (int i = 0; i < 500; i++)
    {

        position1 = rand() % 52;    //there is only 52 positions in the deck this prevents going out of bounds
        position2 = rand() % 52;

        temp = this->theDeck[position1];
        this->theDeck[position1] = this->theDeck[position2];
        this->theDeck[position2] = temp;
    }

}

int Deck::drawCard()
{
    int temp = theDeck.front(); //gets the top card
    theDeck.pop_front();    //pops top card

    return temp;    //returns top card
}

void Deck::showDeck()
{
    deque<int> temporaryDeck = this->theDeck;
    while (temporaryDeck.empty() != true)
    {
        cout << temporaryDeck.front() << " ";
        temporaryDeck.pop_front();
    }
    cout << endl;
}

    Deck::Deck()
{
    for (int i = 0; i < 52; i++)
    {
        this->theDeck.push_front(i); //fill the deck with 52 cards 0 - 51
    }
}

void Deck::shuffleTheDeck() //pick two random positions and swap the cards
{
    int temp;
    int position1;
    int position2;
    for (int i = 0; i < 500; i++)
    {

        position1 = rand() % 52;    //there is only 52 positions in the deck this prevents going out of bounds
        position2 = rand() % 52;

        temp = this->theDeck[position1];
        this->theDeck[position1] = this->theDeck[position2];
        this->theDeck[position2] = temp;
    }

}

int Deck::drawCard()
{
    int temp = theDeck.front(); //gets the top card
    theDeck.pop_front();    //pops top card

    return temp;    //returns top card
}

void Deck::showDeck()
{
    deque<int> temporaryDeck = this->theDeck;
    while (temporaryDeck.empty() != true)
    {
        cout << temporaryDeck.front() << " ";
        temporaryDeck.pop_front();
    }
    cout << endl;
}

目前主要是我正在处理游戏的所有动作的地方。例如,我已经实现了交易的工作方式。但是有没有更好的方法让我可以重用代码?

int main() {
Deck cards;
Player player1, player2;
deque<int> field;

cards.shuffleTheDeck(); //shuffle the deck
for (int i = 0; i < 52; i++)    //deal the cards
{
    if (i % 2 == 0)
    {
        player1.placeCardInHand(cards.drawCard());
    }
    else
    {
        player2.placeCardInHand(cards.drawCard());
    }
}

while (!player1.handIsEmpty() && !player2.handIsEmpty()) //turns start
{




} //game over


return 0;
}

标签: c++classinheritancesimulatorcomposition

解决方案


推荐阅读