Reveal a card in a short deck.
★★★Cutting the deck is a common way to resolve draws in playing card games. One player takes any number of cards (usually approximately half) from a face down full deck and then reveals the card on the bottom of their pile. The second player then takes any card from those remaining in the deck and reveals their card.
Higher numbers beat lower numbers regardless of suit. J is 11, Q is 12, K is 13 and A is 14. The alphabetical order of the suit resolves draws. Clubs (C) is lowest followed by diamonds (D), hearts (H) and spades (S) is the highest.
In a short deck the lowest number is a 6. Therefore the 6C (the six of clubs) is the lowest card and AS (the ace of spades) is the highest card.
Write a program that asks the user to pick one of the first thirty-five cards in the deck. All the previous cards are discarded. The computer then picks one of the remaining cards. Whoever has the highest card wins. The result is output to the user.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
deck.play_game procedure.play_game that:output_winner procedure with the two cards as parameters.output_winner that:Hint
The numeric value of a card is its index modulo 9 (because there are 9 cards in each suit). The suit (
C,D,H,S) breaks ties.
What number card do you want to draw? 0-34 :
20
You reveal the 10S
The CPU reveals the 8C
You win!
output_winner
// Output who won the game
static void output_winner(string player_card, string cpu_card)
{
---
// Find the value of each card from its position in the ordered array
int player_card_value = Array.IndexOf(cards, player_card);
int cpu_card_value = Array.IndexOf(cards, cpu_card);
if (player_card_value % 9 == cpu_card_value % 9)
{
---
// Same value; the higher suit (later in the array) wins
if (player_card_value > cpu_card_value)
{
Console.WriteLine("You win!");
}
else
{
Console.WriteLine("CPU wins.");
}
}
---
else if (player_card_value % 9 > cpu_card_value % 9)
{
Console.WriteLine("You win!");
}
---
else
{
Console.WriteLine("CPU wins.");
}
}
play_game, main program
// Play the game
static void play_game()
{
---
random_generator.Shuffle(deck);
// Player cuts the deck
Console.WriteLine("What number card do you want to draw? 0-34 :");
int player_choice = Convert.ToInt32(Console.ReadLine());
string player_card = deck[player_choice];
Console.WriteLine("You reveal the " + player_card);
---
// CPU cuts one of the remaining cards
int cpu_choice = random_generator.Next(player_choice + 1, deck.Length);
string cpu_card = deck[cpu_choice];
Console.WriteLine("The CPU reveals the " + cpu_card);
output_winner(player_card, cpu_card);
}
---
public static void Main(string[] args)
{
---
play_game();
}