Code Adventures: Simulating One Handed Solitaire

My style in titling these things is to just present the subject on whatever it is I’m linking to in the title, so you might expect that this is about someone else doing that and me reporting on it. But no! This time it’s something I did myself!

First, you have to know of a card game called One Handed Solitaire, as reported by Metafilter member ChurchHatesTucker here. CHT’s been on a tear in presenting various card games lately, here’s the other recent posts they’ve made on solitaire card games, with a dungeon crawl flavor: Clear the Dungeon, Tomb of the Four Kings, Scoundrel.

One Handed Solitaire is very simple, and an example of a “zero player game.” There are no decisions to make; winning or losing is completely down to the initial state of the deck. Here are the rules in text:

You start with a shuffled deck of cards. Draw four to form your hand. Your hand is considered to be in sequence, you must keep them in the order drawn. Now:

  • If the first and fourth cards are the same suit, discard the second and third cards from your hand out of play. This of course moves the fourth card to be the second card.
  • If the first and fourth cards are the same rank, discard the first four cards from your hand.
  • If neither of these things are true, draw another card from the deck to the front of your hand. This makes a new first card, and changes which the fourth card is.
  • When the deck runs out and you can no longer remove cards, the game is over. If you clear your hand and there’s still cards in the deck you’re not done, draw four more.

Your score (lower is better) is how many cards are in your hand when you run out of deck and can no longer discard cards. The average score is about 13.32 cards left. If you get a score of zero, that is you discard all of the cards from your hand and the deck is empty, you win.

Here are the rules demonstrated by Gather Together Games in a Youtube video (1¾ minutes):

ChurchHatesTucker ran a simulation of 200,000 runs and found the win rate of the game is close to 0.7%. I ran my own simulation, in a Python script, and found that out as well. I’ll put my code at the end of this post. No AI was used in its writing, and permission is not given to use it to train AIs. In fact, that’s true of all the text in this blog.

My first attempt found a win rate of 0.94%, but that turns out to be because I left out the aces from the deck! I tried a run with only 20 cards in the deck the 2-6 of each suit, and the win rate became about 7.5%.

If you want to try yourself, here’s the Python code I used. If you have Python installed, just paste it into a text file, give it the extension .py, and run it. It assumes you’re running it in a Linux or other Unix-like system; if you’re on Windows, you might have to change the “shebang” line at the front to point to where your Python is.

#!/usr/bin/python
import random

def draw(deck):
if len(deck) > 0:
return deck.pop(0)
else:
return None

def gameend(deck, hand, verbose):
score = len(hand)
if verbose >= 1:
print("Deck exhaused. Final score:",score)
if score == 0:
print("Win!")
if verbose >= 2:
print("Deck state:",deck)
print("Hand state:",hand)
return score

def play(verbose = 0):
deck = ["2H","3H","4H","5H","6H","7H","8H","9H","TH","JH","QH","KH","AH",
"2D","3D","4D","5D","6D","7D","8D","9D","TD","JD","QD","KD","AD",
"2C","3C","4C","5C","6C","7C","8C","9C","TC","JC","QC","KC","AC",
"2S","3S","4S","5S","6S","7S","8S","9S","TS","JS","QS","KS","AS"]
hand = []
random.shuffle(deck)
for a in range(4):
hand.append(draw(deck))
if verbose >= 3:
print("Game starting--")
while True:
if len(hand) < 4:
drawcard = draw(deck)
if drawcard == None:
return gameend(deck, hand, verbose)
if verbose >= 2:
print("Drew a",drawcard)
hand.insert(0, drawcard)
continue
cardtop = hand[0]
cardfourth = hand[3]
if verbose >= 3:
print("********: Deck length:",len(deck), "Hand length:",len(hand))
if verbose >= 2:
print("CARDS:", cardtop, cardfourth)
# case 1: if the 1st and 4th cards match suit, discard the second and third cards
if cardtop[1] == cardfourth[1]:
d1 = hand.pop(1)
d2 = hand.pop(1)
if verbose >= 2:
print("Discarded",d1,"and",d2)
continue
# case 2: if the 1st and 4th cards match rank, discard the top four
if cardtop[0] == cardfourth[0]:
d1 = hand.pop(0)
d2 = hand.pop(0)
d3 = hand.pop(0)
d4 = hand.pop(0)
if verbose >= 2:
print("Discarded:",d1,d2,d3,d4)
continue
# case 3: if neither is true, draw a card
drawcard = draw(deck)
if drawcard == None:
return gameend(deck, hand, verbose)
else:
if verbose >= 2:
print("Drew a card")
hand.insert(0, drawcard)
# end of loop

if __name__ == "__main__":
numgames = 10000000
wins = 0
scores = []
scoresum = 0
for count in range(numgames):
score = play(verbose = 0)
scores.append(score)
scoresum += score
if score == 0:
wins += 1
#print("A win on game #",count+1)
if count % 500000 == 0:
print("Played",count,"games...")
print("Finished playing",numgames,"games")
print("Wins:",wins)
print("Win rate:",wins/numgames)
print("Total score:",scoresum)
print("Average score:",scoresum/numgames)
print("Run compete.")

How To Play Pocket Card Jockey: Ride On

Yeah, it’s another Youtube video, but there’s an important difference this time: I made it myself. It’s seven minutes long.

I’m new at this kind of editing, and I spent a lot of effort trying to give viewers enough time to read all the text, but I failed somewhat in that regard. I suggest pausing the video if you need to catch up. It’s about the recently released Switch version of Pocket Card Jockey, a horse racing and breeding simulation that search-and-replaced all the fiddly HORSE RIDING parts with CARD GAME: a form of Solitaire called Golf, a.k.a. One Foundation.

Yeah, I’m new at this kind of editing. It was good practice though!

Game Freak has mostly had its legacy taken up by The House that Pikachu built, Pokèmon. Pokè this and Pokè that. But they had a history before they made their absurdly popular critter catching/fighting RPG, beginning as an obscure  Japanese fanzine from 1983, and they sometimes publish a game that has nothing to do with their monstrous progeny. The best known of these is probably the Gameboy Advance game Drill Dozer, but lately they’ve sallied forth into mobile gaming with a title called, in English, Pocket Card Jockey. It got a 3DS port beloved of the few that tried it, and now an update of that is on the Switch ($15). It seems a bit easier now, but it’s still wonderful.

I’ve seen Pocket Card Jockey described as not complicated, and rarely do I see a take that I disagree with more. Pocket Card Jockey is very complicated, each horse has almost a dozen characteristics to be cognizant of, each race is full of tension, and success in G-I (the hardest type) of races usually comes through executing a good strategy. I’ve also seen people say that it must have been easy to make, and I disagree with that assumption too: I think it must have been terribly difficult to construct, and most of that difficulty was in design and playtesting. Games like this don’t just happen, not if they’re any good, and Pocket Card Jockey is good.

Pocket Card Jockey: Ride On has an extensive tutorial, with three whole practice races, but there’s still a lot to learn. That’s why I made this video, to try to infect you with some of my own 100-level enthusiasm for it. I know of few games that work better in practice. You should give it a try.