{"id":25,"date":"2005-05-31T23:20:58","date_gmt":"2005-05-31T13:20:58","guid":{"rendered":"http:\/\/somethinkodd.com\/oddthinking\/?p=25"},"modified":"2007-10-07T21:01:11","modified_gmt":"2007-10-07T11:01:11","slug":"beating-the-clock-solitaire","status":"publish","type":"post","link":"https:\/\/www.somethinkodd.com\/oddthinking\/2005\/05\/31\/beating-the-clock-solitaire\/","title":{"rendered":"Beating the Clock Solitaire"},"content":{"rendered":"<p><!-- UnMarkedDown_2_01139627702--><\/p>\n<h3>The Game of Clock Solitaire<\/h3>\n<p><a href=\"http:\/\/ja-games.co.uk\/java-card-games\/solitaire\/clock-solitaire-game.php\">Clock Solitaire<\/a> (also known as Clockwork Patience) has got to be the stupidest card game ever invented.<\/p>\n<p>Actually, come to think of it, <a href=\"http:\/\/www.pagat.com\/war\/beggar_my_neighbour.html\">Beggar My Neighbour<\/a> is worse. <\/p>\n<p>Both of them are completely mechanical games. There is no skill. There is not even any opportunity to make a choice!  However, at least, Clock Solitaire is guaranteed to finish in 52 moves or less. Beggar My Neighbour (also known as Strip Jack Naked) can last forever, and  wastes the time of two people.<\/p>\n<h3>The Rules<\/h3>\n<p>Clock Solitaire is set-up by dealing 12 face-down piles of four cards in a circle. The piles are numbered 1 &#8211; 12, like a clock face. The remaining four cards are left in a pile in the centre. Play begins by turning over the top card of the centre pile.<\/p>\n<p>The face-up card is moved to the pile corresponding to the face value of the card (A=1, J=11, Q=12, K=centre pile). The card is placed at the bottom of the pile, face-up, and the top-card is turned over, and play continues with that card.<\/p>\n<p>When the fourth King finally turns up, there are no more moves possible, and the game stops. If all the other piles are completed (only face-up cards left) then you win, otherwise you lose.<\/p>\n<h3>The Optimal Implementation<\/h3>\n<p>As an undergraduate, pondering various data structures, I started thinking about this game, and how it would be optimally implemented.<\/p>\n<p>The obvious implementation would consist of 13 arrays of 4 cards. We can ignore suits &#8211; they are irrelevant, so each card is a single integer, ranging from 1 to 13.<\/p>\n<p>However, once the card is actually placed in the appropriate pile, we don&#8217;t care about it anymore. We can dispose of any record of it &#8211; the pile is empty when there are no cards in it. So we don&#8217;t really need arrays &#8211; we can use bounded-size queues (max length = 4), which could well be faster because there is no need to rotate all the cards.<\/p>\n<p>But here is the interesting step&#8230; stick with me and we will come back to it later.<\/p>\n<p><strong>We don&#8217;t actually need to deal out the cards to the various piles beforehand!<\/strong><\/p>\n<p>We could have a central pile of cards, ready to be dealt out, and as a card is needed, we can ask the central pile of cards to provide the next card. Just think of the time we will save by not actually wasting time moving the cards to the various decks when we start!  Now each pile is just a counter telling us how many there are in the pile.<\/p>\n<p>But now that we have made this optimisation &#8211; this change in the way we look at the cards &#8211; we can see another short-cut. You lose the game if the final king appears anywhere but the last card in the centralised pile. Conversely, you only win if the last card in the centralised pile is a King.<\/p>\n<p>The whole game boils down to that last card, so that&#8217;s the only one you really need to deal out.  <\/p>\n<p>The optimal implementation is a random number generator that returns True one time in thirteen!<\/p>\n<h3>The Controversy<\/h3>\n<p>I was very impressed with how a little bit of thought saved a whole lot of implementation effort, and I mentioned it to some fellow students.<\/p>\n<p>To my surprise, they disagreed with my insight. They argued that the &#8220;interesting step&#8221; wasn&#8217;t valid.<\/p>\n<p>There was a <a href=\"http:\/\/c2.com\/cgi\/wiki?GedankenExperiment\">Gedankenexperiment<\/a> that highlighted our differences in opinion.<\/p>\n<p>Imagine you dealt out a deck and you started playing for the final round of the Clock Solitaire championships. Halfway through the exciting game, a judge stops you and asks you to pick up the remaining face-down cards, shuffle them well, and re-deal them to the same locations.<\/p>\n<p>I argued that, you should have no objection. From an <em>expected value<\/em> perspective, the game is unchanged. Once you accept that, it is a small step to accepting &#8220;lazy-evaluation dealing&#8221;, where you don&#8217;t deal a card until you absolutely need to, and the rest of the analysis holds.<\/p>\n<p>My friends argured that shuffling the cards would change the outcome. It would be a different game and you should object to having to re-shuffle them mid-game.<\/p>\n<p>I tried to explain that the chance of the outcome being made worse and being made better was equal, and there was no issue. For the same reason, if I am playing a multi-player card game, I don&#8217;t care who actually deals the cards, and which order the piles are passed out to the players, as long as I trust the people shuffling and dealing the cards not to cheat. [In many games, however, it is important to track who is the nominal dealer (as opposed to the actual dealer), in order to decide who starts playing.]<\/p>\n<p>I failed to be convincing. The room was split, as it so often is, between the people who agreed with me wholeheartedly, and the people who were just plain wrong.<\/p>\n<p>It went further, however. Not only did they believe that my <em>analysis<\/em> was wrong, but they believed my <em>conclusion<\/em> was wrong. They believed that, in their experience, it came out more often than once in every thirteen games.<\/p>\n<h3>The Experiment<\/h3>\n<p>There was only one resolution possible &#8211; the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Monte_Carlo_method\" title=\"Wikipedia definition of Monte_Carlo_method\" class=\"wikipedia\">Monte Carlo method<\/a> &#8211; like backtracking (which <a href=\"http:\/\/somethinkodd.com\/oddthinking\/?p=20\">I have dismissed before<\/a>), the Monte Carlo method skips the niceties of logical analysis to use sheer brute force to solve a problem.  <\/p>\n<p>I wrote an implementation to play the obvious way. The code was carefully reviewed by my opponents. It was then set to run 10,000 times, and tally the results.<\/p>\n<h3>The Result<\/h3>\n<p>The result?  Everyone lost. <\/p>\n<p>The opponents lost because the experiment proved that Clock Solitaire came out, on average, one time in thirteen.<\/p>\n<p>I lost because all of my superior optimisations that implemented Clock Solitaire in a single line of code were useless because no-one believed in them. I ended up implementing the whole thing long and slow way. I hadn&#8217;t even really proven that my analysis was correct; merely that my conclusion was right and their memory of their experiences were not consistent with what would be expected.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An analysis of Clock Solitaire &#8211; beyond merely &#8220;stupid&#8221;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"","footnotes":""},"categories":[23,35,33,34],"tags":[55,69,54],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-based-on-a-true-story","category-heroic-failures","category-puzzle-solving","category-software-development","tag-puzzle","tag-software","tag-solution"],"_links":{"self":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":0,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.somethinkodd.com\/oddthinking\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}