CARD TRICK

Computers seem like magic to many people, but the following program goes one stage further and turns the computer into a magician enabling it to find a card chosen secretly by an onlooker. The presentation of the trick is as follows: the thirteen cards of one suit are fanned out face down, and the onlooker takes one and remembers it. The remaining pile of cards is cut once, and the onlooker then replaces the chosen card wherever he likes. The pile is then divided in two, and the two halves are shuffled together. Finally, the cards are fanned out face-up on the table, and the order of the cards is typed into the computer, representing Ace as 1, Jack as 11, Queen as 12, and King as 13. After a brief pause the computer announces which was the chosen card! The trick can be repeated any number of times, and the computer will almost always be right.

For example, suppose the cards are in the sequence shown below:

Cards

Having typed in the sequence of cards the program will print.

YOU PICKED THE 3

The trick depends for its success on the cards being shuffled only once, and the shuffle should be of the sort that divides the packet into two halves, and merges the two halves back into one pile; this shuffle is sometimes called a riffle shuffle. The cards can be cut at any time, but only into two piles.


Program Operation

The program works by comparing the new order of the cards with their order the previous time the trick was performed; for each card a number is calculated which represents how far the processes of shuffling and cutting have moved that card from its previous neighbours. The higher this score, the more out of sequence is the card concerned. The card with the highest score is likely to be the one that was chosen.

When the programs are first executed they assume that the cards were in numerical order, ace up to king. If the cards are not in order when the trick is performed the computer will, most likely, get the trick wrong at the first attempt, but in some ways this adds to the mystery and can be attributed to "warming up"! Subsequently, the initial order is replaced by the new order of the cards, as typed in; therefore the order of the cards should not be disturbed between presentations of the trick.

There are cases in which the computer cannot be certain about which card was the chosen one. For example, if the card is returned to its original position then no information is available to the computer. Less obviously, if the card is replaced next to its previous neighbour, it is ambiguous whether it or its neighbour was the chosen card. Cutting the pack before asking the onlooker to replace the card encourages him to replace the card in a different position, minimising the chances of these events occurring.


BBC Computer Version

10  REM ... Card Trick ... 
20  DIM A(13),B(13),S(13)

The cards are represented by the numbers 1 (for ace) to 13 (for king). First time, assume cards in order. Then the sequence of cards is read in.

30  FOR J=l TO 13: A(J)=J: NEXT J
40  PRINT "ENTER YOUR CARDS"
50  FOR J=l TO 13: S(J)=0
60  INPUT B(J): NEXT J

Each of the cards in the previous sequence A(J) is searched for in the new sequence, and its position there is subtracted from the positions of each of the cards that were its neighbours in the previous sequence. The sequences are considered as circular, so 13 is added to any difference that turns out negative.

 70  T=A(13): PROCFIND
 80  FOR J=l TO 13: L=X: R=T
 90  T=A(J): PROCFIND
100  Q=X-L: IF Q<0 THEN Q=Q+13

The cards distance from one neighbour, plus its distance from the other neighbour, is saved as that card's score.

110  S(T)=S(T)+Q: S(R)=S(R)+Q 
120  NEXT J

The card with the maximum score is found and displayed as the chosen card.

130  M=0
140  FOR J=l TO 13: A(J)=B(J) 
150  IF S(J)>=M THEN Z=J: M=S(J) 
160  NEXT J
170  PRINT "YOU PICKED THE "; Z 
180  GOTO 40

PROCFIND - Find card T in array B, and return number in X.

200  DEF PROCFIND
210  FOR K=l TO 13: IF T=B(K) THEN X=K 220  NEXT K: ENDPROC

Variables:

A(1)..A(13) - Old array of cards
B(1)..B(13) - New array of cards
J - Counter
M - Maximum score
S(1)..S(13) - Scores for each card

Atom Version

10 REM ... CARD TRICK ...
20 DIM AA(13),BB(13),SS(13); @=0

The cards are represented by the numbers 1 (for ace) to 13 (for king). First time, assume cards in order. Then the sequence of cards is read in.

30 FOR J=l TO 13; AA(J)=J; NEXT J
40 PRINT "ENTER YOUR CARDS"'
50 FOR J=l TO 13; SS(J)=0
60 INPUT B; BB(J)=B; NEXT J

Each of the cards in the previous sequence AA(J) is searched for in the new sequence, and its position there is subtracted from the positions of each of the cards that were its neighbours in the previous sequence. The sequences are considered as circular, so 13 is added to any difference that turns out negative.

 70 T=AA(13); GOSUB f
 80 FOR J=1 TO 13; L=X; R=T
 90 T=AA(J); GOSUB f
100 Q=X-L; IF Q<0 THEN Q=Q+13

The cards distance from one neighbour, plus its distance from the other neighbour, is saved as that card's score.

110 SS(T)=SS(T)+Q; SS(R)=SS(R)+Q
120 NEXT J

The card with the maximum score is found and displayed as the chosen card.

130 M=G
140 FOR J=l TO 13; AA(J)=BB(J) 
150 IF SS(J)>=N THEN Z=J; M=SS(J) 160 NEXT J
170 PRINT "YOU PICKED THE " Z'
180 GOTO 40

f - Find card T in array BB, and return number in X.

210fFOR K=l TO 13; IF T=BB(K) THEN X=K 220 NEXT K; RETURN

Variables:

AA(1)..AA(13) - Old array of cards
B - Card entered
BB(1)..BB(13) - New array of cards
J - Counter
M - Maximum score
SS(1)..SS(13) - Scores for each card