Random rand = new Random(); // value for randomness public void PlayWar() { // create a pile for a war Pile warPile = new Pile(); // create a deck of cards and shuffle Deck theDeck = new Deck(); theDeck.Shuffle(rand); // while there is at least 2 cards in the deck while (theDeck.GetNumberOfCards() >= 2) { // deal the entire deck to the players one card at a time player1.CollectCard(theDeck.Deal()); player2.CollectCard(theDeck.Deal()); } // replace the current piles being played with the collected piles player1.ReplaceCurrentPile(); player2.ReplaceCurrentPile(); // the game will continue for 100 rounds or until a player runs out of cards for (int round = 1; round <= 100; round++) { // check if players have at least 1 card each if (!HasEnoughCards(1)) { // exit for loop break; } // assign the current card values Card card1 = player1.PlayNextCard(); Card card2 = player2.PlayNextCard(); // output what round it is and the players' cards Console.WriteLine("Round " + round + ": "); Console.Write(player1.GetName() + " put down "); card1.Display(); Console.Write(player2.GetName() + " put down "); card2.Display(); if (CompareCards(card1, card2) > 0) { // player 1 wins this hand // put the played cards in player 1's win pile and output results player1.CollectCard(card1); player1.CollectCard(card2); Console.WriteLine(player1.GetName() + " won this hand!\n"); } else if (CompareCards(card1, card2) < 0) { // player 2 wins this hand // put the played cards in player 2's win pile and output results player2.CollectCard(card1); player2.CollectCard(card2); Console.WriteLine(player2.GetName() + " won this hand!\n"); } else { // the cards have the same value, so a war has begun // reset the war pile and add the cards that were just played warPile.Reset(); warPile.AddCard(card1); warPile.AddCard(card2); Boolean warIsOver = false; // while the war has not ended, do { // check if the players have at least 4 cards each if (!HasEnoughCards(4)) { // exit the do while break; } Console.WriteLine("\nWar! Players, put down 4 cards!"); // each player adds 4 cards to the war pile for (int j = 1; j <= 4; j++) { card1 = player1.PlayNextCard(); card2 = player2.PlayNextCard(); warPile.AddCard(card1); warPile.AddCard(card2); } // output players' fourth card values Console.Write(player1.GetName() + "'s Fourth Card: "); card1.Display(); Console.Write(player2.GetName() + "'s Fourth Card: "); card2.Display(); if (CompareCards(card1, card2) > 0) { // player 1's fourth card is higher than player 2's fourth card // player 1 wins the war player1.CollectMultipleCards(warPile); warIsOver = true; Console.WriteLine(player1.GetName() + " won this war!\n"); } else if (CompareCards(card1, card2) < 0) { // player 2's fourth card is higher than player 1's fourth card // player 2 wins the war player2.CollectMultipleCards(warPile); warIsOver = true; Console.WriteLine(player2.GetName() + " won this war!\n"); } } while (!warIsOver); } // output player card totals Console.WriteLine(player1.GetName() + "'s total cards: " + player1.GetTotalNumberOfCards()); Console.WriteLine(player2.GetName() + "'s total cards: " + player2.GetTotalNumberOfCards()); Console.WriteLine(""); } }
public void CollectCard(Card theCard) { // collect a card by adding a card to the won pile wonPile.AddCard(theCard); }