Experimental Analysis of
Ali Baba Solitaire

Jan Wolter
April 15, 2013

[General Introduction]

The Game

Ali Baba Solitaire is a one deck variation of Forty Thieves. The tableau contains 10 columns of 4 face-up cards, leaving 12 cards in a rather small stock pile which is dealt one card at a time to the waste. You can read the rules or play the game on politaire.com or many other places.

Since there is only one deck, since you build down in the same suit, and since you can move sequences, this plays rather like Trigon and Storehouse in that the only time the player really needs to make a choice is when there is an empty tableau column to fill. However, in Ali Baba the choices at that point are a bit more complex, making for a more interesting game.

There is some variation in the rules of Ali Baba as described on different web sites.

The name "Big Forty" is used by some people for some variations.


The depth-first-search solver that I built for Trigon, and extended to play Storehouse, was easily adapted to play Ali Baba. Because the game is so simple, and you rarely have to backtrack the solver ran quite quickly.

Random Deals

I ran the solver on the first million Politaire Ali Baba games, seeds zero through 999999. Here are the results for four different variations:

One PassUnlimited Passes
No Aces Won 39.7% (396,809)
22.6 cards off (43.5%)
Won 43.2% (432,614)
26.4 cards off (50.8%)
Aces Dealt Won 45.0% (450,159)
25.3 cards off (44.4%)
Won 45.3% (453,392)
27.3 cards off (48.5%)

Note that the count of cards off includes the four aces that are dealt to the foundation in the variations in the bottom row. The percentages shown adjust for this, being n/52 for the top row and (n-4)/48 for the bottom row.

Note that predealing the aces to the foundation has the side effect of reducing the size of the stock from 12 cards to 8 cards. Making a small stock even smaller reduces it's usefulness, so it's no surprise that being able to do an unlimited number of passes through it only slightly increases our win rate.

The histogram below shows the full distribution of the numbers of cards which could be removed for all four games. Note that our counts of removed cards include the four cards that were dealt to the foundation in the "Aces Dealt" cases. I omitted the "52 card" column from the graph, because it is too tall. No unwinnable game ever left less than 24 cards on the tableau, so I omitted those columns from our graph too.

Numbers of Removable Cards in Unwinnable Random Ali Baba Games
Blue=No Aces/One Pass; Green=No Aces/Unlimited;
Orange=Aces/One Pass; Red=Aces/Unlimited;

One thing that is obvious to anyone who has played the game a few times is that you often get stuck rather early, but if you don't, if you get far enough to open an empty tableau column, then you can usually find a way to win. Though it is theoretically possible to be stuck with as few as eleven cards still on the tableau, this is extremely unlikely.

Natural Shuffling of Gathered Cards

If the game is played with real cards instead of on a computer, then it is unlikely that the cards will be completely randomized between games. So we consider the case where, when a game has been completed, we pick up the cards one stack at a time, reversing the stacks that were faceup, shuffle n times, cut once, and deal the next hand.

For each number of shuffles from one to ten I ran the solver on one million games. The first game in each run used a randomized deck, and after that the procedure above was followed to produce the deck for the next game.

I ran the test two ways. In the first set of tests, I always dealt the cards column by column. In the second set of tests, I always dealt them one row by row.

I did this only for the one pass/no aces variation, because that variation is the best.

The resulting win rates are shown in the graph below:

Ali Baba (One Pass, No Aces) Win Rate
with n Shuffles of Cards Gathered from Previous Game

Green=Deal by Columns; Blue=Deal by Rows; Red=Fully Randomized

When dealing by columns, it appears that inadequate shuffling doesn't make a huge difference in this game. Even if you shuffle only once between, your win rate differs only by five percent. But unusually, it always makes your games harder, never easier. This rather makes sense, as your tableau piles tend to end up with lower cards buried under higher cards.

If you deal one row at a time, then things are very different. If you don't shuffle much, you'll win lots and lots of hands, probably because lots of adjacent cards of the same suit start out on the tops of the piles.

So, deal by columns if you want a fair game, deal by rows if you want to win. Or shuffle five or more times between games and make it moot.

Natural Shuffling of Ordered Decks

As usual, in my next test, I started each hand with a sorted deck (each suit in increasing order), or a reversed deck (each suit in decreasing order), then shuffled that deck n times, and cut once before dealing. I did this for the case where we allow only one pass through the stock, do not deal aces to the foundation, and deal the tableau by columns. We again ran a million iterations for each value of n.
Ali Baba Win Rate with n Shuffles of a Ordered/Reversed Deck
Red line is win rate with fully randomized deck

It is interesting to compare this to the graph for Storehouse. In both games, residual order in the deck can make the game harder, but for Storehouse the game was easier if we had done just a few shuffles. I think the larger and more flexible stock/waste in Storehouse allows sequences to be reversed more easily, so even backwards sequences are still helpful. Also in Storehouse there are only 12 cards that are initially inaccessible, being buried in the reserve, while in Ali Baba there are 30 inaccessible cards in the initial tableau. This may make backwards sequences more harmful (remember, sequences are reversed during the deal).