Overview: The goal of the first project is to build a war card game simulator fo
ID: 3638959 • Letter: O
Question
Overview: The goal of the first project is to build a war card game simulator for two computer players. The implementation of the project is up to you, but you must include a Card class and a Deck class. The Card class should store the value and suit of the card. The Deck class should be able to hold up to 52 Cards and should be capable of being shuffled and drawn from.
Card Class: The card class should store the value and suit of the card. The values of a card may be 2, 3, 4, 5, 6, 7, 8, 9, 10, J (Jack), Q (Queen), K (King), or A (Ace). The suits of a card may be Diamonds (?), Hearts (?), Clubs (?), or Spades (?). It doesn’t matter how you store this, as long as your method is explained in your comments. Additionally, your Card class should implement the Java Comparable interface. The comparable class allows you to use your cards in other Java classes such as ordered lists. Methods that your card class should include are as follows:
? Card- This constructor should accept the value and suit of the card.
? print- This should print the value of the card to the screen. ASCII includes a few characters that
can be used to print the suit of cards.
? compareTo- Your cards should implement the Java comparable class. This method should accept
a second card and return a 0 if the cards are equal, a 1 if this object is larger than the card passed in, or a -1 if this object is smaller than the card passed in. In War, cards with higher
values are larger (2 < 3 < ... < 9 < 10 < J < Q < K <A) regardless of suit. Your class should implement this comparison scheme.
Deck Class: The deck class should hold a number of cards that maintain an order. I don’t care how this is internally implemented, as long as it includes at least the methods listed below and your implementation is explained in your comments.
? shuffle –This should randomize the locations of all of the cards in your deck. There should be no obvious order of the cards once the deck is shuffled.
? draw – This should remove the first card from the top of the deck and return it.
? add – This should accept a card as its parameter and add it to the bottom of deck.
? getSize – This should return the number of cards currently in the deck.
Main:
The structure of your main method might look like this:
? First, create a deck with 52 cards. Shuffle it.
? Deal 26 cards to two more decks for each of the players.
? Loop until one of the decks is empty or 500 battles occur.
o Draw a card from each of the players. o Determinethewinnerofthebattle.
? Add the cards to the bottom of the victor’s deck.
? If there is a tie, declare a war. (See above)
o Print the value of each of the cards in the battle, the winner of the battle, the number of
cards that the winner took, and the current size of the winner’s deck.
? Print the winner of the game (or print tie if the game was not over in at least 500 battles), the
number of battles that occurred, the number of wars that occurred, and the number of battles and wars that each player won.
Sample Output:
Battle 1, Plyr.A: A?, Plyr.B: 3?, Win: Plyr.A, Deck Size: 53 Battle 2, Plyr.A: 5?, Plyr.B: 6?, Win: Plyr.B, Deck Size: 52
Battle 3, Plyr.A: 10? K? 6? 9?, Plyr.B: 10? A? A? 2?, Win: Plyr.A, Deck Size: 59
(And so forth...)
End game print out:
Winner: Plyr.B
Tot. Battles: 167
Tot. Wars: 3
Plyr.A Battles: 70
Plyr.B Battles: 97
Plyr.A Wars: 1
Plyr.B Wars: 2
Explanation / Answer
Classes The abstract base class, CardGame, contains generic fields, properties, and methods, which could be reused to create many types of card games. CardGame class contains methods for creating a new deck of 52 cards, shuffling a set of cards (see code below) and dealing cards to each player. Fields and properties in the base class includeDictionary objects to store the player’s cards, and player, battle, and game stats. protected Dictionary ShuffleCards(Dictionary cardsToShuffle) { protected Dictionary ShuffleCards(Dictionary cardsToShuffle) { Dictionary ShuffledCards = new Dictionary(cardsToShuffle); Random RandomNumber = new Random(); ShuffledCards = ShuffledCards.Select(cards => cards) .OrderBy(cards => RandomNumber.Next()) .ToDictionary(item => item.Key, item => item.Value); return ShuffledCards; }} The WarCardGame class inherits the CardGame class, and adds additional fields, properties,and methods, specific to the game of War. WarCardGame class adds methods for starting a game,executing a battle (see code below), and determining the type of battles – single, double, etc. The WarCardGame class’s fields store values such as wartallies and number of cards to be placed facedown in war. One might argue that more of the WarCardGame class’s fields, properties, and methods could be placed in the base class and overridden if necessary.public bool Battle() { //Fill temp decks with players cards tempPlayer1Deck = Player1Deck.Select(cards => cards).ToDictionary(item => item.Key, item => item.Value); tempPlayer2Deck = Player2Deck.Select(cards => cards).ToDictionary(item => item.Key, item => item.Value); if (Player1Deck.Count() > 0) { Player1CardKey = Player1Deck.ElementAt(0).Key; } if (Player2Deck.Count() > 0) { Player2CardKey = Player2Deck.ElementAt(0).Key; } if (tempPlayer1Deck.Count() == 0) { StatusMessage = "Player 2 wins the game!"; OutcomeCode = 3; return false; } else if (tempPlayer2Deck.Count() == 0) { StatusMessage = "Player 1 wins the game!"; OutcomeCode = 1; return false; } else if (tempPlayer1Deck.Count() < (2 + cardsPlacedFaceDown) && tempPlayer1Deck.ElementAt(0).Value == tempPlayer2Deck.ElementAt(0).Value) { // Game ended in war (tied in battle) // Player 1 doesn't have enough cards for a war Player2TricksWon++; TotalTricksWon++; UpdateWarTotals(tempCardsOnTheTable.Count()); StatusMessage = "Player 2 wins the game! Game ended in a war."; OutcomeCode = 4; return false; } else if (tempPlayer2Deck.Count() < (2 + cardsPlacedFaceDown) && tempPlayer1Deck.ElementAt(0).Value == tempPlayer2Deck.ElementAt(0).Value) { Player1TricksWon++; TotalTricksWon++; UpdateWarTotals(tempCardsOnTheTable.Count()); StatusMessage = "Player 1 wins the game! Game ended in a war."; OutcomeCode = 2; return false; } else // Game hasn't ended so continue { if (CardsOnTheTable != null) // If last battle ended in war, // collect cards left on table { tempCardsOnTheTable = CardsOnTheTable.Select(cards => cards).ToDictionary(item => item.Key, item => item.Value); } Player1CardValue = tempPlayer1Deck.ElementAt(0).Value; Player2CardValue = tempPlayer2Deck.ElementAt(0).Value; // Begin battle, each player lays down face-up card tempCardsOnTheTable.Add(tempPlayer1Deck.ElementAt(0).Key, tempPlayer1Deck.ElementAt(0).Value); tempPlayer1Deck.Remove(tempPlayer1Deck.ElementAt(0).Key); tempCardsOnTheTable.Add(tempPlayer2Deck.ElementAt(0).Key, tempPlayer2Deck.ElementAt(0).Value); tempPlayer2Deck.Remove(tempPlayer2Deck.ElementAt(0).Key); // Randomizes order of card on table before they are placed // back onto the bottom of the winning player's deck tempCardsOnTheTable = ShuffleCards(tempCardsOnTheTable); if (Player1CardValue > Player2CardValue) { StatusMessage = "Player 1 wins the battle."; // Add cards on table to winning player's hand var playerDeck1LINQ = tempPlayer1Deck.Select(cards => cards).Concat(tempCardsOnTheTable.Select(cards => cards)); tempPlayer1Deck = playerDeck1LINQ.ToDictionary(item => item.Key, item => item.Value); Player1TricksWon++; TotalTricksWon++; UpdateWarTotals(tempCardsOnTheTable.Count()); tempCardsOnTheTable.Clear(); // There is a winner so clear // the table of cards } else if (Player1CardValueRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.