The rules of Old Maid are simple, so it is perfect for programming practice. In this article, we write a Python program that plays a four-player game of Old Maid all the way through automatically, and play 100,000 games to check the odds. The rules are the same as in the Old Maid app.
- The 53 cards (with one Joker) are dealt one at a time to four players, starting with seat 0.
- After everyone discards their first pairs, play starts with seat 0.
- The turn order is seat 0, 1, 2, 3, and each player draws one card from the hand of the player before them. Players who are out are skipped.
- Everyone draws at random.
- The game ends when only the Joker is left.
The Program
import random
def make_deck():
deck = [rank for rank in range(1, 14) for _ in range(4)]
deck.append(0) # 0 is the Joker
return deck
def discard_pairs(hand):
rest = []
for card in hand:
if card != 0 and card in rest:
rest.remove(card) # if there is the same number, discard both cards
else:
rest.append(card)
return rest
def play(players=4):
deck = make_deck()
random.shuffle(deck)
hands = [discard_pairs(deck[i::players]) for i in range(players)]
turn = 0
draws = 0
while sum(len(hand) for hand in hands) > 1:
if not hands[turn]: # skip players who are out
turn = (turn + 1) % players
continue
source = (turn - 1) % players # draw from the player before
while not hands[source]:
source = (source - 1) % players
card = hands[source].pop(random.randrange(len(hands[source])))
draws += 1
if card != 0 and card in hands[turn]:
hands[turn].remove(card) # discard if a pair is made
else:
hands[turn].append(card)
turn = (turn + 1) % players
loser = next(i for i, hand in enumerate(hands) if hand)
return loser, draws
random.seed(1)
games = 100000
counts = [0, 0, 0, 0]
total = 0
for _ in range(games):
loser, draws = play()
counts[loser] += 1
total += draws
print("Share of games left with the Joker (%):", [round(c / games * 100, 1) for c in counts])
print("Average number of draws:", round(total / games, 1)) How It Works
How the Cards Are Represented
Cards are represented by their number only. A to K are 1 to 13, and the Joker is 0. In Old Maid, the suit (spades or hearts) doesn’t matter for pairs, so the number is enough. make_deck builds a 53-card deck with four each of 1 to 13, plus one 0.
The Deal and the First Pairs
deck[i::players] takes cards from the deck starting at position i, every 4th card. Seat 0 gets the cards at positions 0, 4, 8, and so on, which is the same as dealing one card at a time in turn.
discard_pairs looks at the cards in a hand one by one. If the same number is already there, it discards both cards, and if not, it keeps the card. With three cards of the same number, one is left, and with four, none are left. The Joker (0) is never paired.
Turn Order and Who You Draw From
turn is the seat whose turn it is. The seat before is found with (turn - 1) % players. The % gives the remainder after dividing, so the seat before seat 0 is seat 3. If the player before has finished, you draw from the player before that.
If the drawn card has the same number as a card in the hand, both are discarded. If not, the card is added to the hand.
The End and the Result
The game ends when the total number of cards in all hands is 1. The player who still has a card in hand is the one holding the Joker.
The Output
With the random seed set by random.seed(1), running it on Python 3.14 gave the following.
Share of games left with the Joker (%): [16.1, 28.7, 38.9, 16.2]
Average number of draws: 31.9 Seat 0 (you, in the app) and seat 3 (Oscar) were at about 16%, seat 1 (Leo) at about 29%, and seat 2 (Rose) at about 39%. Even though everyone draws at random, there is a difference of more than double between seats. The average number of draws is about 32.
This result also matched the result of 200,000 games calculated with a separate program. Why the seats differ is explained in Who Gets Stuck with the Joker?.
Try Changing It
- Changing the 4 in
play(players=4)changes the number of players. When you change the number of players, also change the size ofcountsto match. - Shifting the i in
deck[i::players]changes which player is dealt 14 cards. Try it and see how the results change. - What happens if, instead of
random.randrange, a player always draws the leftmost card?
