Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

It's me. Cave Johnson.

It's May 21st. You are working at Aperture Science, finest company on Earth, and tomorrow we're releasing Version 3.0 of our flagship windowing system (or WindowS): CavOS. Unfortunately, tomorrow morning, our chief competitor is releasing Version 3.0 of their WindowS too!

It's been leaked to me, Cave Johnson, CEO that the competing WindowS has one feature that CavOS lacks: A game of Klondike Solitaire. I, Cave Johnson, am concerned that this omission will lead to drastically lower sales. As such, I, Cave Johnson, have devised a competition between the programming interns to create a game of Solitaire. The programmer whose entry is chosen will get the opportunity to join the exciting GlaDOS AI project.

I, Cave Johnson, has a few requirements:

  • Due to the fact that the Software must ship tomorrow, the shortest program will be chosen such that it can fit in the spare sectors of the already printed, imaged floppies.
  • Don't worry about the gameplay. Our customers have printers don't they? All I need is for your program to produce a list showing: The order of the cards in the deck, and the contents of each of the 7 piles. The deck will be dealt from the top. The piles will be visible from the bottom. Use the abbreviations H, D, C, S for Hearts, Diamonds, Clubs and Spades. Use the abbreviations K, Q, J, A for King, Queen, Jack, and Ace. Each card should be separated by a space. The first line should show the deck, the second the card of the first and smallest pile, the third the cards of the second pile and so on. You are welcome to use T instead of 10 if you wish.

  • We're making lemonade here - not sucking lemons. I, Cave Johnson, don't like losing, and neither do our customers. Every game must be winnable

  • The game must be randomised. No attempting to encode a single game. There are 7000 trillion possible Klondike hands, and about 90% of them are winnable. I'd be happy with a program that can produce 256 or more different hands. If your language lacks a manner to get random numbers (from a timer or the like), then assume that you can get a seed in a manner appropriate for your language.

  • Our customers are reasonably clever. Assume for winnability that they're playing Draw 3, with no limits on times through the deck

On behalf of everybody (and everything) at Aperture, I wish you all good luck. We're counting on you.

"Caroline? How do I shut this damn thing off?"


Out of character: assume the rules of Windows, Draw 3, Not-vegas solitaire. I've attempted to cross every t and dot every i, but ask if you've any questions. Good luck.

Example output:

5H AS 7C 8S QH ...    
AH    
JS 10S    
JC JH 7C    
9D AC 8D 7C     
.    
.    
.
share|improve this question
    
Can we use 'T' for 10, so every card is 2 characters? –  CMP Jan 18 '13 at 2:27
    
I think our customers are clever enough to go for that. I'll add it to the description. –  lochok Jan 18 '13 at 2:34
add comment

1 Answer

Brainfuck - 1575 1365

I am horribly abusing the minimum number of unique deals. This will produce exactly 256 distinct outputs. It accepts one byte of input as its random seed.

,>++++[>++++++<-]<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>>[<+<<+>>>-]>[-]<
<-[>+<-]<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>[-]>[-]>[<<<+>>>-]>[-]+[>+<
+++++]>+++++[>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>
>+>>+>>+>>+>>+[<<]>-]>>>>>>>>>+>>+>>+>>+>++++++[>++++>>++++>>++++>>+++
+>>+++>>+++>>+++>>+++>>++++>>++++>>++++>>++++>>+++>>+++>>+++>>+++<<<<<
<<<<<<<<<<<<<<<<<<<<<<<<<<-]>++++>>++++>>++++>>++++>>>>>>>>>>+>>+>>+>>
+>>+>>+>>+>>+[<<]>>[>+<-<+>]>[<+>-]<<+++++++++++[>>+>>+>>+>>+>>+>>+>>+
>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+>>+[<<]>>-]>>>>+>>++++
+>>++++++++++++++++>>>>+>>+++++>>++++++++++++++++>>>>+>>+++++>>+++++++
+++++++++>>>>+>>+++++>>++++++++++++++++>>>>+>>+++++>>++++++++++++++++>
>>>+>>+++++>>++++++++++++++++[<<]<<<<<<[>>>>>>+<<<<<<-]>>>>>>[>[<<+>>-
]<[>+<-]>>[<<+>>-]<-[>+<-]>]>.[-]>.[-]>[[<<<<+>>>>-]>]<<<<<[<<]<++++[>
++++++++<-]>.[-]<<<[>>>+<<<-]>>>[>[<<+>>-]<[>+<-]>>[<<+>>-]<-[>+<-]>]>
.[-]>.[-]>[[<<<<+>>>>-]>]<<++++[>++++++++<-]>.[<<<+>>>-]<<<<<[.[-]>.[-
]>.[<<+>>-]<<<<]>>[>+>++>++<<<-]++++++++++.>>+>+++<.>.<<<.>>----------
-----.+>.<<.>.+>.<<<.>>.+>.<<.>.+>.<<.>.+>.<<<.>>.>.<<.>++++++++++.>+.
<<.>---------------.+>.<<.>.+>.<<<.>>.+>.<<.>.+>.<<.>.+>.<<.>.+>.<<.>+
++++++++.>++++.<<<.>>---------------.+>.<<.>.+>.<<.>.+>.<<.>.+>.<<.>.+
>.<<.>.+>.<<<.>>+++++++++.>+++++++++++.<<.>---------------.+>.<<.>.+>.
<<.>.+>.<<.>.+>.<<.>.+>.<<.>.+>.<<.

Could definitely be condensed more, but that is kinda the nature of the language. This is quick and dirty, and it works.

Example output (input = 98):

8H 9S KS KH KD KC QS QH QD QC JS JH JD JC TS TH TD TC 9H 9D 9C 8S 8D 8C 
AC
2C 3C
4C 5C 6C
7C AD 2D 3D
4D 5D 6D 7D AH
2H 3H 4H 5H 6H 7H
AS 2S 3S 4S 5S 6S 7S 

It is fairly intuitive that all decks of this format are winnable.

share|improve this answer
1  
+1: I salute you ! –  Paul R Jan 18 '13 at 8:28
2  
Wonderful codegolf typical rule bending solution. And it even is Brainfuck - well done! –  schnaader Jan 19 '13 at 0:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.