2. Card guessing game

2.1. Rules of the game

Consider a deck of 52 cards:

1 (A) 2 3 4 5 6 7 8 9 10 11 (J) 12 (Q) 13 (K)
Diamond Cards-A-Diamond Cards-2-Diamond Cards-3-Diamond Cards-4-Diamond Cards-5-Diamond Cards-6-Diamond Cards-7-Diamond Cards-8-Diamond Cards-9-Diamond Cards-10-Diamond Cards-J-Diamond Cards-Q-Diamond Cards-K-Diamond
Club Cards-A-Club Cards-2-Club Cards-3-Club Cards-4-Club Cards-5-Club Cards-6-Club Cards-7-Club Cards-8-Club Cards-9-Club Cards-10-Club Cards-J-Club Cards-Q-Club Cards-K-Club
Heart Cards-A-Heart Cards-2-Heart Cards-3-Heart Cards-4-Heart Cards-5-Heart Cards-6-Heart Cards-7-Heart Cards-8-Heart Cards-9-Heart Cards-10-Heart Cards-J-Heart Cards-Q-Heart Cards-K-Heart
Spade Cards-A-Spade Cards-2-Spade Cards-3-Spade Cards-4-Spade Cards-5-Spade Cards-6-Spade Cards-7-Spade Cards-8-Spade Cards-9-Spade Cards-10-Spade Cards-J-Spade Cards-Q-Spade Cards-K-Spade
  • Each card is in one of the four suits: Diamond, Club, Heart, and Spade.

  • Each card has a value \(1 \text{ (A)} < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 \text{ (J)} < 12 \text{ (Q)} < 13 \text{ (K)}\).

The following code creates a deck of cards. (You do not need to understand the code for now.)

# Create a deck of cards
from collections import namedtuple

suits = ("Diamond", "Club", "Heart", "Spade")
values = range(1, 14)
Card = namedtuple('Card', ['value', 'suit'])

deck = [Card(value, suit) for value in values for suit in suits]
print(deck)

To play the game, a dealer randomly pick a card without letting you know, and you’re going to guess what exactly that card is.

# Randomly draw a card from the deck with replacement
import random
print(random.choice(deck))

You are allowed to make an informed guess after the dealer answers some of your yes/no questions.

For instance, you may ask:

  • Is the suit club?

  • Is the card diamond 1 (ace)?

  • Is the value at least 10?

However, you cannot ask:

  • What is the value?

  • What is the suite?

Exercise You win if you can guess the card correctly with no more than 6 questions. What is the winning strategy?

YOUR ANSWER HERE

Hint 1: Obviously, you should not ask whether the card is precisely certain card, e.g., Is it Diamond Ace? Is it Diamond 2? … Why not? The card may be one of the remaining \(52-6=46\) possibilities you did not ask.

Hint 2: Think of each Yes/No question as splitting the set of possible cards into two smaller groups of possible cards corresponding to each possible answer Yes/No.

Hint 3: How many questions is required to split the set of 52 cards into groups of size \(1\), i.e., with only one possible card?

2.2. Challenge the computer

Play the role of the dealer and test if the program below can guess the card correctly after 6 questions.

suitIdx = 0
number = 0

if "y" == input(
        "Is the suite either heart or spade? (y/[n]) ").strip().lower():
    suitIdx += 2

if "y" == input("Is the suite either club or spade? (y/[n]) ").strip().lower():
    suitIdx += 1

if "y" == input(
        f"Is the number {number+8} or above? (y/[n]) ").strip().lower():
    number += 8

if "y" == input(
        f"Is the number {number+4} or above? (y/[n]) ").strip().lower():
    number += 4

if "y" == input(
        f"Is the number {number+2} or above? (y/[n]) ").strip().lower():
    number += 2

if "y" == input(
        f"Is the number {number+1} or above? (y/[n]) ").strip().lower():
    number += 1

print(f"The card is {suits[suitIdx]} {number}")

Exercise Does the above program always win? Explain your answer?

YOUR ANSWER HERE

2.3. Challenge your understanding

The following table gives the binary representions of unsigned decimal integers from 0 to 7.

BinaryDecimal
0000
0011
0102
0113
1004
1015
1106
1117

To convert binary to decimal, think of the conversion as a guessing game where

  • the binary sequence is a sequence of yes (1) or no (0) answers to certain yes/no questions, and

  • the informed guess is the integer represented by the binary sequence.

For instance, observe that the binary representation of 4, 5, 6, and 7 actually have 1 in the leftmost (most significant) bit. Therefore we can consider that bit as the answer to the following yes/no question:

Is the integer 4 or above?

Exercise What are the yes/no questions corresponding to the 2nd bit and 3rd bit?

YOUR ANSWER HERE

References