C# Chapter 8 11 a+b. Create a console based computerized game of war named WarCa
ID: 3553486 • Letter: C
Question
C# Chapter 8 11 a+b. Create a console based computerized game of war named WarCardGame
In the cam game war, a deck of playing cards is divided between two players. Each player exposes a card; the player whose card has the higher value wYns possession of both exposed cards. Create a console-based computerized game of War named WarCardGame in which a standard 52-card deck is randomly divided between two players, one of which is the computer. Reveal one card for the computer and one card for the player at a time. Award two points for the player whose card has the higher value. (For this game the king is the highest card, followed by the queen and jack, then the numbers 10 down to 2, and finally the ace.) If the computer and player expose cards with equal values in the same turn, award one point to each. At the end of the game, all 52 cards should have been played only once, and the sum of the player's and computer's score will be 52. Use an array of 52 integers to store unique values for each card. Write a method named Fill Deck () that places 52 unique values into this array. Write another method named SelectCard() that you call twice on each deal to select a unique card for each player, with no repetition of cards in 26 deals (To pause the play between each dealt hand, use a call to ReadLine().) The left side of Figure 8-31 shows the start of a typical program execution. (Caution: This is a difficult exercise!)Explanation / Answer
using System; using System.Collections; namespace WarCardGame { public class Game { public class Hand { public int handValue = 0; ArrayList handCards = new ArrayList(); public void addCard(Card card) { if (card.getFace().Equals("Ace") && handValue > 10) card.value = 1; handCards.Add(card); handValue += card.value; Bust(); } bool Bust() { if (handValue > 21) return true; else return false; } public string printCards() { string CardList = ""; foreach (Card x in handCards) { CardList += x.toString() + " "; }return CardList; } } public CardDeck Deck; public int card; public Random rand = new Random(); public Hand DealerHand, PlayerHand; public void initialDeal(){ initializeDeck(); DealerHand = new Hand(); PlayerHand = new Hand(); DealPlayer(); DealDealer(); DealPlayer(); DealDealer(); //System.out.println(printHand()); } public void initializeDeck() { Deck.fillDeck(); } public void DealPlayer() { card = rand.Next(52); Card newCard = new Card(Deck.cards[card].value, Deck.cards[card].suit, Deck.cards[card].face); if (newCard.value == 0) DealPlayer(); else { PlayerHand.addCard(newCard); Deck.cards[card].value = 0; } } public void DealDealer() { card = rand.Next(52); Card newCard = new Card(Deck.cards[card].value, Deck.cards[card].suit, Deck.cards[card].face); if (newCard.value == 0) DealDealer(); else { DealerHand.addCard(newCard); Deck.cards[card].value = 0; } } //void Bust(int Hand){ // if (Hand > 21) System.out.println("BUST"); //} public String printHand() { return (" Dealer: " + DealerHand.handValue + " " + "Player: " + PlayerHand.handValue + " "); } public String printAllHands() { return ("Player Hand: " + PlayerHand.printCards() + " Dealer Cards: " + DealerHand.printCards()); } public Boolean playerWins() { if (DealerHand.handValue > 21 || (PlayerHand.handValue > DealerHand.handValue && PlayerHand.handValue 3 && face.ToString().Contains("Face")) words = face.ToString().Substring(4) + " of " + suit + ": " + value; else words = face + " of " + suit + ": " + value; return words; } public Faces getFace() { return face; } } public class CardDeck { public Card[] cards; int count = 0; int MaxCards = 52; CardDeck() { cards = new Card[MaxCards]; } void addCard(int value, Suits suit, Faces face) { cards[count++] = new Card(value, suit, face); } public void fillDeck(){ foreach (Suits S in Enum.GetValues(typeof(Suits))){ int counter = 2; foreach (Faces F in Enum.GetValues(typeof(Suits))){ if (counter < 10){ addCard(counter,S,F); counter++; }else addCard(10, S,F); } } } public String toString() { String CardList = ""; for (int x = 0; xRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.