//comments: shuffles a card deck of user entered size //predictable shuffle. once shuffle reaches original //position or is shuffled ten times suffling stops import cs1.Keyboard; public class Lab5Part2 { public static void main(String[] args) { int[] deck, deck2, deck3, shuffled; int deckSize = 0, inputSize; int halfSize; int index, index2, shuffleNumber = 0; int xT = 0; System.out.print("Enter the size of the deck: "); inputSize = Keyboard.readInt(); halfSize = deckSize / 2; if (inputSize != 0) { deckSize = inputSize; halfSize = deckSize / 2; while (deckSize != 0) { shuffleNumber = 0; while (deckSize < 0 || halfSize * 2 != deckSize) { System.out.print("Enter an even integer greater than zero, or"); System.out.println(" zero to stop."); System.out.println(); System.out.print("Enter the size of the deck: "); deckSize = Keyboard.readInt(); halfSize = deckSize / 2; } if (deckSize > 0 || halfSize * 2 == deckSize) { deck = new int[deckSize]; for (index = 0; index < deckSize; index++) { deck[index] = index; } for (index = 0; index < deckSize; index++) { System.out.print(index + " "); } System.out.println(); shuffled = new int[deckSize]; System.arraycopy(deck, 0, shuffled, 0, deckSize); do { index2 = 0; index = 0; deck2 = new int[halfSize]; deck3 = new int[halfSize]; System.arraycopy(shuffled, 0, shuffled, 0, deckSize); System.arraycopy(shuffled, 0, deck2, 0, halfSize); System.arraycopy(shuffled, halfSize, deck3, 0, halfSize); for (index = 0; index < halfSize; index++) { shuffled[index2] = deck2[index]; shuffled[index2 + 1] = deck3[index]; index2 = index2 + 2; } printArray(shuffled, " "); shuffleNumber++; for (xT = 0; shuffled[xT] == deck[xT] && xT < deckSize - 1; xT++); } while (xT < deckSize - 1 && shuffleNumber < 10); if (xT == deckSize - 1) { System.out.print("After " + shuffleNumber + " shuffles, "); System.out.println("the deck is in sorted order again."); } if (xT < deckSize - 1) { System.out.print("After 10 shuffles the deck is not in the "); System.out.println("original order"); } } System.out.println(); System.out.print("Enter the size of the deck: "); deckSize = Keyboard.readInt(); halfSize = deckSize / 2; while (deckSize < 0 || halfSize * 2 != deckSize) { System.out.print("Enter an even integer greater than zero, or"); System.out.println(" zero to stop."); System.out.println(); System.out.print("Enter the size of the deck: "); deckSize = Keyboard.readInt(); halfSize = deckSize / 2; } } } } private static void printArray(int[] array, String prompt) { for (int index = 0; index < array.length; index++) { System.out.print(array[index]); System.out.print(prompt); } System.out.println(); } }