public class ReversePerfectShuffleNode
extends SearchNode
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.)
Constructor and Description |
---|
ReversePerfectShuffleNode()
Initialize an array with 52-card deck indices such that index i contains integer i.
|
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() |
public ReversePerfectShuffleNode()
public boolean isGoal()
SearchNode
isGoal
- test whether or not the current node is a
goal node.isGoal
in class SearchNode
boolean
value - whether or not the
current node is a goal nodepublic java.util.ArrayList<SearchNode> expand()
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.expand
in class SearchNode
ArrayList
of SearchNodes that are children
of this nodepublic java.lang.String toString()
toString
in class java.lang.Object
public java.lang.Object clone()
clone
- return a deep copy of this node.clone
in class SearchNode
Object
valuepublic static void main(java.lang.String[] args)