Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C# create a console-based computerized game of War named WarCardGame in wh

ID: 3665519 • Letter: U

Question

Using C# 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 players and computers score will be 52. Use an array of 52 integers to store unique values for each card. Write a method named FillDeck() 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().) Needs to not have a Class defined.

Explanation / Answer

/*

suppose:

1 to 4 denotes Ace of clubs, diamonds, hearts, spades

5 to 8 denotes Deuce etc.

...

45 to 48 denotes Queen etc.

49 to 52 denotes King etc.

*/

using System;

class War

{

static int[] deck = new int[52]; // all zero by default

static int lastCardIndex = 0;

static string[] cards = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

static string[] suits = {"clubs", "diamonds", "hearts", "spades"};

static Random rand = new Random();

static void Main()

{

int totalComputer = 0; // records total points for computer

int totalPlayer = 0; // ditto for player

FillDeck();

Console.Clear();

  

// 26 deals

for(int i = 0; i < 26; i++)

{

string computerSuit;

int computer = SelectCard(out computerSuit) - 1;

string playerSuit;

int player = SelectCard(out playerSuit) - 1;

if (computer > player)

{

totalComputer += 2;

}

else if (computer < player)

{

totalPlayer += 2;

}

else

{

totalComputer += 1;

totalPlayer += 1;

}

Console.WriteLine("Deal # {0}", i + 1);

Console.WriteLine(" Computer has {0} of {1}", cards[computer], computerSuit);

Console.WriteLine(" Player has {0} of {1}", cards[player], playerSuit);

Console.WriteLine("Computer Score is {0}", totalComputer);

Console.WriteLine("Player Score is {0}", totalPlayer);

Console.WriteLine();

Console.ReadKey();

}

}

static void FillDeck()

{

for(int i = 0; i < 52; i++)

{

while(true)

{

int num = rand.Next(1, 53); // get number between 1 and 52 inclusive

if (Array.IndexOf(deck, num) == -1) // not already used

{

deck[i] = num;

break;

}

}

}

}

static int SelectCard(out string suit)

{

int num = deck[lastCardIndex];

lastCardIndex++;

suit = suits[(num - 1) % 4];

return (num - 1) / 4 + 1;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote