## Class ReversePerfectShuffleNode

• java.lang.Object
• SearchNode
• ReversePerfectShuffleNode
• All Implemented Interfaces:
java.lang.Cloneable

```public class ReversePerfectShuffleNode
extends SearchNode```
ReversePerfectShuffleNode - class modeling perfect reverse (a.k.a. anti-faro) in- and out-shuffles in order to find a means of working the four top cards into the 5th, 10th, 15th, and 20th positions.

A perfect shuffle is one where the deck is cut perfectly in half and then "riffled" together alternating one card from each half like the teeth of a zipper before being pushed together. This is considered to be one of the most difficult sleights of hand in card magic, and it allows the performer to control the positions of all cards in the deck, rearranging the permutation in unexpected ways. There are two perfect shuffles that differ according to whether the first card down comes from the top or bottom half. A perfect out-shuffle leaves the top card on top, that is, outside the deck. A perfect in-shuffle leaves the top card at the second card, that is, inside the deck.

A reverse perfect shuffle is just the opposite. See Figure 3 from Magical Mathematics: The Mathematical Ideas That Animate Great Magic Tricks by Persi Diaconis and Ron Graham. While perfect shuffles are one of the most difficult sleights of hand, this simple process of alternate up- and down-jogging of cards can be performed by most anyone. As before, a reverse perfect out-shuffle leaves the top card on top, that is, outside the deck. A reverse perfect in-shuffle leaves the top card right after the middle of the deck, that is, inside the deck.

isGoal() specification: The goal of this SearchNode is to find a means of working the secretly stacked four top cards (e.g. Aces) into the 5th, 10th, 15th, and 20th positions of the deck. Then the performer can, after performing such a sequence of reverse perfect in- and/or out-shuffles, deal five five-card Poker hands (the 5th being dealt to the dealer), and reveal these same four cards in the dealer's hand.

Internally, you'll represent the current card positions as a length 52 int array initialized such that index i contains i. Thus, the goal state will have 0, 1, 2, and 3 at indices 4, 9, 14, and 19, respectively.

expand() specification: Each expand will return a list of two children. The first will be the result of the parent undergoing a reverse perfect out-shuffle. The second will be the result of the parent undergoing a reverse perfect in-shuffle. Order matters for correctness testing.

public String toString() specification: toString should return a comma-and-space-separated list of the current permutation of deck indices (0-51). The easiest way to do this is to make use of the Arrays.toString() method and return the result without the first ("[") and last ("]") characters.

main method specification: Perform a breadth-first search on the ReversePerfectShuffleNode and call printGoalPath on the resulting goal node. (Note: When two successive permutations have the same first integer, an out-shuffle was performed. Otherwise, an in-shuffle was performed.)

• ### Fields inherited from class SearchNode

`depth, parent`
• ### Constructor Summary

Constructors
Constructor and Description
`ReversePerfectShuffleNode()`
Initialize an array with 52-card deck indices such that index i contains integer i.
• ### Method Summary

All Methods
Modifier and Type Method and Description
`java.lang.Object` `clone()`
`clone` - return a deep copy of this node.
`java.util.ArrayList<SearchNode>` `expand()`
`expand` - return a (possibly empty) ArrayList of this node's children.
`boolean` `isGoal()`
`isGoal` - test whether or not the current node is a goal node.
`static void` `main(java.lang.String[] args)`
`java.lang.String` `toString()`
• ### Methods inherited from class SearchNode

`childClone`
• ### Methods inherited from class java.lang.Object

`equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`
• ### Constructor Detail

• #### ReversePerfectShuffleNode

`public ReversePerfectShuffleNode()`
Initialize an array with 52-card deck indices such that index i contains integer i.
• ### Method Detail

• #### isGoal

`public boolean isGoal()`
Description copied from class: `SearchNode`
`isGoal` - test whether or not the current node is a goal node.
Specified by:
`isGoal` in class `SearchNode`
Returns:
a `boolean` value - whether or not the current node is a goal node
• #### expand

`public java.util.ArrayList<SearchNode> expand()`
Description copied from class: `SearchNode`
`expand` - return a (possibly empty) ArrayList of this node's children. A new child is created by calling `childClone` and appropriately modifying the state of the returned node.
Specified by:
`expand` in class `SearchNode`
Returns:
an `ArrayList` of SearchNodes that are children of this node
• #### toString

`public java.lang.String toString()`
Overrides:
`toString` in class `java.lang.Object`
• #### clone

`public java.lang.Object clone()`
`clone` - return a deep copy of this node.
Overrides:
`clone` in class `SearchNode`
Returns:
an `Object` value
• #### main

`public static void main(java.lang.String[] args)`