我一直在模拟《战争游戏》纸牌游戏,以便收集完成转弯次数的统计信息。
我创建了一个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();
}
DECK:
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;
}