Saturday, June 26, 2010

Solitaire Strategies

I’ve discovered some solitaire games. One of them is “Flood It”, which I saw Nat playing the other day. You can go see the rules for yourself, it is very simple.

Not surprisingly, this game has been analysed by computer scientists who have found the complexity involved in searching for solutions. It is NP hard, which means, to the best of current computer science knowledge, the amount of time taken to find all solutions increases exponentially with respect to the problem size. There are some other interesting results which state how quickly the average number of steps required to solve the problem increases with respect to the size of the problem.

I was thinking about what are the best strategies for playing this game, and I have found a strategy that works well, that will win the game around 75% of the time. This doesn’t have to do with brute force computation but just some guidelines.

The guiding principle in this strategy is that all the blocks can be arranged in a stack, that every block has a “distance”, meaning, how many moves does it take to get to that block. Always pay attention to the blocks that have the greatest depth because winning the game is the same thing as being able to convert those blocks within 25 moves.

To be sure, the exact definition of “distance” will depend upon what is the sequence of colours you choose. But as a rule of thumb, it’s the number of squares between your territory and the edge. If there are bigger blocks in that unconverted zone, then there is less distance (since that zone can be converted with fewer moves). If those big blocks are perpendicular to the perimeter of your territory, then the distance decreases further, since with 1 or 2 clicks, you can reach the edge (And afterwards, a really big surface area is exposed).

Early in the game, the key is “depth first”. Aim to capture an area which looks like the shape of a river. Always try to convert the blocks that are deep rather than wide – the blocks which are perpendicular to your territory. Maximise your perimeter, because the greater your perimeter, the more blocks you can convert. Concentrate on maximising the depth, try to convert the blocks that are large.

There are pictures which show the destruction of the Amazonian rainforest. (Sorry to use such an emotionally traumatic example). The denuded areas are at first narrow and deep, then after that they widen, until the rainforest is gone. That’s the way you should be doing things.

Why is it important to go depth first early on? Because when you expand your perimeter early, it will pay back many times in subsequent moves. The order in which you click the colours does not matter because naturally you will go through all the six colours. All those blocks next to your “river” shaped territory will be converted. Go for the quick returns – the high depth, low distance blocks. Don’t think too hard about the other blocks, because they will take care of themselves.

A corollary of the depth first principle is that you shouldn’t convert the blocks close to the walls too early, because in that way your perimeter will only have exposed blocks on 1 side.

Another other concept is degree, which means the same thing as in graph theory. How many other blocks is your block connected to. A certain block may be only 1 square large, but it could give you access to 3 large blocks. Then that block suddenly becomes valuable because it is strategic.

Obviously, you also play the colour that captures as much territory as possible. A corollary to this is that if you have just played a colour, don’t play that same colour 1 or 2 moves later, since not many blocks of that colour can be exposed in 1 or 2 moves. But I would actually say that early in the game, depth and degree are more important.

Yet another concept is the order of colours. Suppose there are 2 or 3 possible colours to play, then you just have to work out what is the best order in which to play them, in order to capture as much territory as possible.

You may find that there will be 1 colour which does not have blocks that fit the above criteria. This is perfectly OK. Do not convert colours that do not impede your progress. Ignore that colour until one of them obstructs your progress, then you can convert a large number of colours with 1 fell swoop.

So early in the game, you have 5 strategies – Go for depth, go for degree, go for high conversion, optimise your order of colours and convert colours as late as possible.

Gradually, when we have reached squares which are near the south or the east wall, we come to the end game. In the end game, the objective is not to expand your territory as quickly, but to convert all the squares with as few moves as possible.

First, if there are any colours which are totally exposed, convert them now. It’s the end for 1 of the colours. One way of thinking is that winning is the same thing as finishing off the 6 colours. The move that finishes off a colour is necessary, and it does not matter whether it is played earlier or later. But it’s better to play this move earlier since this will probably give your access to more colours.

As a corollary, if there is a move which can convert all squares of the same colour except for 1, it would be a waste to play that move now. Unless that 1 block is buried so deep that you probably have to wait a while before you get that block.

However, if there is a move that can lead you to finishing off 1 colour, then it might be worth playing that move.

Secondly, thinking about distance is even more crucial in your end game. For every colour, you should be thinking about how many moves you need to finish it off. This principle is a more broad version of the earlier “if you can finish off a colour in 1 or 2 moves, do it”. If not all blocks of the colour are exposed, you can think about whether it is possible to finish off half of them now, and whether there will be a situation later on where the other half is all exposed. Then that colour would count as a 2 move colour.

Most importantly, though, you have to concentrate on the most distant blocks, since these blocks will have the greatest impact on your endgame. You should have an end strategy in mind for every colour, and you should try to access those most deeply buried blocks in the least number of moves.

Which brings us to the last important point of the endgame: the order of the colours is very important. You should always know what colour exposes what other colour, and based on this knowledge, work out an optimum order of moves.

Now that I have worked out the winning strategy for Flood it, hopefully my short but intense addiction to the game would have come to an end.

My other favourite solitaire game comes with every Windows from XP onwards. It’s “Spider Solitaire”. I play the medium level, because 1 colour is too easy, and 4 colours is too difficult.

The strategy for spider solitaire is really simple.

The main strategy is this: empty a stack as quickly as you can. Once you have an empty stack, the number of possibilities increases greatly. Do not waste your empty stack unwisely, and preserve it for as long as you can.

Use your undo as many times as you need to discover what’s behind the cards, and to find the best possible sequence of moves. Remember that in the XP version, there are some moves that cannot be undone, such as dealing cards, or finishing a suit. (In the Windows 7 version, all moves are undoable). Not very elegant, but make sure that you know as much as there is to know

Experienced players will know that when you manage to empty a stack, the odds of solving a game is around 70%, and when you manage to uncover all the cards, the odds of solving the game is around 100%.

I don’t aim to maximise my statistics. If there are games which are stubbornly hard to solve, I consider it a challenge to solve them, and I will incur a lot of “losses”. I think this is OK. It’s more satisfying to solve a hard game than an easy one. But if you have to try a new game, try taking a different route by making different moves. It is often the case that in certain card layouts, playing move A earlier on will result in a difficult endgame, while playing move B earlier on will result in the same endgame. People familiar with chaos theory will know that small changes to the system could have unintentional really huge effects, and at the same time large shocks to the system could have completely negligible outcomes.