Amazingly, this is the easier of the three card tricks I know off the top of my head...
The aim of this is to have your compiler perform a specific card trick. There's no slight of hand, and the compiler won't be memorizing the choice and saying "Voila, this is your card!" The code will basically manipulate a stack of cards and the final card drawn from this stack will be the card chosen at random:
If you want to understand this trick further, feel free to follow this with an actual deck of cards so that you can learn the trick. I will be displaying the order of the cards in the face-down stack in my example in curly braces {}
so that you can see what's happening.
The standards set in this trick:
A standard deck of cards is generated, 52 cards (no jokers required). From this deck, nine cards will be chosen at random to form our trick's card stack.
The backs of the cards will be displayed as
@@
.The face-size of the cards will be displayed in the format
XY
whereX
is the value,A-9
,T
,J
,Q
,K
(T
= 10); andY
is the suit,S
pades,C
lubs,D
iamonds,H
earts. SoAS
for the Ace of Spades,TC
for 10 of Clubs,8H
for the 8 of Hearts, etc.In displaying the stack of cards, from left to right, the left-most card will be located on the bottom of the stack, and the right-most card will be at the top of the stack. Similarly, if cards are in smaller sets, left-most = bottom card, right-most = top card.
Given these standards, my randomly chosen stack for this example looks like this: @@ @@ @@ @@ @@ @@ @@ @@ @@
(cards facing down)
...And when facing up: TD JD 8C 7D 8S 3C QH 9S 3S
Note, this means my facing up stack has the ten of diamonds on the bottom, and the three of spades at the top.
Your deck may vary from mine (see Standards 1 above re random generated stack)
I will go through this trick step-by-step, and how the code needs to work:
- Accept a number between 1 and 9 as input, to be used later on:
2
- Generate your nine-card stack and display the cards face down first, in sets of 3, four spaces in between each set. This is so that the user can see they've chosen a specific card out of a specific set:
@@ @@ @@ @@ @@ @@ @@ @@ @@
- Flip the cards over, so that the user can see which card they chose:
TD JD 8C 7D 8S 3C QH 9S 3S
- Announce the card chosen, full wording of the card, e.g. the second card (derived from our input of
2
earlier on):
JACK OF DIAMONDS
- Take the chosen card and place it on top of that specific set, facing up; remembering that the right-most card in each set is the top-most in each set facing up:
TD 8C JD 7D 8S 3C QH 9S 3S
- Put the other two sets on top of the set with the chosen card, also facing up, to form one stack. It does not matter which of the two sets goes first:
TD 8C JD 7D 8S 3C QH 9S 3S
- Flip the stack over so that the cards are facing down:
@@ @@ @@ @@ @@ @@ @@ @@ @@
Note: At this point, the stack of cards will have now reversed in order:
{3S 9S QH 3C 8S 7D JD 8C TD}
Our ten of diamonds is on the top of the pile now... And our chosen card is third from the top... The chosen card will ALWAYS be third from the top at this point.
Now, we have to spell out the card in question, in our case the
JACK OF DIAMONDS
. Cards are dealt from the top of the deck to another pile, spelling out the card as we go. After the spelling of each word, the rest of the pile goes straight on top with each word. I've shown how the cards in the face-down stack are ordered after each word in curly braces:
J @@ A @@ C @@ K @@, @@ @@ @@ @@ @@ {TD 8C JD 7D 3S 9S QH 3C 8S}
O @@ F @@, @@ @@ @@ @@ @@ @@ @@ {8S 3C TD 8C JD 7D 3S 9S QH}
D @@ I @@ A @@ M @@ O @@ N @@ D @@ S @@, @@ {QH 9S 3S 7D JD 8C TD 3C 8S}
- Here's what they call the prestige, the final part of the trick. Deal out the first five cards from the top of the stack, face down to the words
HERE'S THE <NUMBER> OF <SUIT>! <FIFTH CARD FACE UP>
, flipping the final card to show face up...
HERE'S @@ THE @@ JACK @@ OF @@ DIAMONDS! JD
The fifth card from the stack should now be the card originally chosen.
Example output, using a new randomly chosen stack, and 4
as the input.
@@ @@ @@ @@ @@ @@ @@ @@ @@
3D AD JS TH KD QS JC 6D 4S
TEN OF HEARTS
3D AD JS KD QS TH JC 6D 4S
KD QS TH 3D AD JS JC 6D 4S
@@ @@ @@ @@ @@ @@ @@ @@ @@
T @@ E @@ N @@, @@ @@ @@ @@ @@ @@
O @@ F @@, @@ @@ @@ @@ @@ @@ @@
H @@ E @@ A @@ R @@ T @@ S @@, @@ @@ @@
HERE'S @@ THE @@ TEN @@ OF @@ HEARTS! TH
Please note, the final card you are producing at the end is pulled from the card stack (array), and will always be the fifth card in the stack, not from some assigned variable; and the code needs to reflect this. Note, that if you're dealing with a zero-based array language that this would mean, element 4, as opposed to element 5 in a one-based array language.
The key to this code-golf (yes, this is going to be code-golf, muahahahahaha!) is through the manipulation of the nine-card stack. With each word spelled out, the affected cards at the top of the stack will be reversed, thrust to the bottom of the stack and the remaining cards will be placed on top; refer to the examples above to see how that works. Run through the logic with nine playing cards if you have to in order to understand how it works.
Rules
- This is code-golf. Lowest byte-count will win; in the event of a tie in byte count, highest votes will win.
- No silly loopholes.
- The input can be implemented in whatever way you see fit: STDIN, an array, a parameter in a function, whatever takes your fancy...
- Please provide a link to your executing code on an online IDE like TIO so that we can marvel at your work. Learning from one another is something I encourage, and I'd love to see how you all can code this up!
- The chosen card is drawn correctly at the end providing you have the array/stack manipulations worked out. Please do not simply assign the chosen card into a variable and pull it out in the end... This will be regarded as an invalid answer.