Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

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.

Cut the deck

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.


Make

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.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the main program so that:

  1. The random number generator is created.
  2. Copies the list of cards into a playing deck identified as deck.
  3. Calls the play_game procedure.

Complete the subprogram called play_game that:

  1. Shuffles the deck.
  2. Reads the player’s chosen index (between 0 and 34) from stdin.
  3. Draws the player’s card at that index, and draws one of the remaining cards (those after it) for the CPU.
  4. Outputs the cards revealed.
  5. Calls the output_winner procedure with the two cards as parameters.

Complete the subprogram called output_winner that:

  1. Takes the two cards drawn as parameters.
  2. Calculates who won.
  3. Outputs the winner.

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.

Typical inputs and outputs from the program would be:

What number card do you want to draw? 0-34 :
20
You reveal the 10S
The CPU reveals the 8C
You win!
🆘 If you're really stuck, use this Parsons code sorting exercise
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();
}