import java.beans.DesignMode;

public class Game
{
    private static final char REMOVED_CUP = ' ';

    public Game (boolean debug)
    {
        _debug = debug;
    }

    /**
     * Each move, the crab does the following actions:
     * 
     * -The crab picks up the three cups that are immediately clockwise of the current cup. They are removed from the circle; cup spacing is adjusted as necessary to maintain the circle.
     * - The crab selects a destination cup: the cup with a label equal to the current cup's label minus one. If this would select one of the cups that was just picked up, the crab will keep subtracting one until it finds a cup that wasn't just picked up. If at any point in this process the value goes below the lowest value on any cup's label, it wraps around to the highest value on any cup's label instead.
     * - The crab places the cups it just picked up so that they are immediately clockwise of the destination cup. They keep the same order as when they were picked up.
     * - The crab selects a new current cup: the cup which is immediately clockwise of the current cup.
     */

    public final String play (String cups, int numberOfRounds)
    {
        int currentCupIndex = 0;
        char[] theCups = cups.toCharArray();

        for (int round = 0; round < numberOfRounds; round++)
        {
            if (_debug)
            {
                System.out.println("\n-- move "+(round+1)+" --");
                System.out.print("cups: ");

                for (int i = 0; i < theCups.length; i++)
                {
                    if (i == currentCupIndex)
                        System.out.print("("+theCups[i]+") ");
                    else
                        System.out.print(theCups[i]+" ");
                }

                if (_debug)
                    System.out.println();
            }

            char[] pickup = new char[3];

            if (_debug)
                System.out.print("pick up: ");

            for (int i = 0; i < 3; i++)
            {
                int index = currentCupIndex +i +1;

                if (index >= theCups.length)  // in case it wraps.
                    index -= theCups.length;

                pickup[i] = theCups[index];
                theCups[index] = REMOVED_CUP;

                if (_debug)
                    System.out.print(pickup[i]+", ");
            }

            if (_debug)
                System.out.println();

            String remainingCups = new String(theCups).replaceAll(" ", "");
            int currentCup = Character.getNumericValue(theCups[currentCupIndex]);
            int destinationCup = getDestination(currentCup, remainingCups.toCharArray(), pickup);
            int index = findCupLocation(destinationCup, remainingCups.toCharArray());

            if (_debug)
                System.out.println("destination: "+destinationCup+" at "+index);

            int remainingIndex = 0;

            theCups = new char[theCups.length];

            for (int i = 0; i < theCups.length; i++)
            {
                System.out.println("theCups "+i+" will be "+remainingCups.charAt(remainingIndex)+" from "+remainingIndex);

                theCups[i] = remainingCups.charAt(remainingIndex);

                if (i == index)
                {
                    for (int j = 0; j < pickup.length; j++)
                    {
                        System.out.println("theCups "+(i+j+1)+" will be "+pickup[j]);

                        theCups[i+j+1] = pickup[j];
                    }

                    i += pickup.length;
                }

                remainingIndex++;
            }

            currentCupIndex++;

            if (currentCupIndex == theCups.length)
                currentCupIndex -= theCups.length;
        }

        return null;
    }

    // find destination cup index in those remaining
    
    private final int findCupLocation (int destinationCup, char[] cups)
    {
        for (int i = 0; i < cups.length; i++)
        {
            if (Character.getNumericValue(cups[i]) == destinationCup)
                return i;
        }

        System.err.println("Error! Can't find destination cup!!");

        return -1;
    }

    // find destination cup label in those remaining

    private final int getDestination (int currentCup, char[] theCups, char[] pickup)
    {
        int destinationCup = currentCup -1;
        int lowest = lowestLabel(theCups);
        boolean done = false;

        while (!done)
        {
            if (_debug)
                System.out.println("Comparing "+destinationCup+" and "+lowest);
                
            if (destinationCup < lowest)
            {
                destinationCup = highestLabel(theCups);
                done = true;
            }
            else
            {
                boolean conflict = false;

                for (int i = 0; (i < pickup.length) && !conflict; i++)
                {
                    if (Character.getNumericValue(pickup[i]) == destinationCup)
                    {
                        destinationCup--;
                        conflict = true;
                    }
                }

                if (!conflict)
                    done = true;
            }
        }

        return destinationCup;
    }

    private final int lowestLabel (char[] theCups)
    {
        int lowest = Character.getNumericValue(theCups[0]);

        for (int i = 1; i < theCups.length; i++)
        {
            if (_debug)
                System.out.println("Current lowest: "+lowest+" and "+Character.getNumericValue(theCups[i]));

            if (Character.getNumericValue(theCups[i]) < lowest)
                lowest = Character.getNumericValue(theCups[i]);
        }

        return lowest;
    }

    private final int highestLabel (char[] theCups)
    {
        int highest = Character.getNumericValue(theCups[0]);

        for (int i = 1; i < theCups.length; i++)
        {
            if (_debug)
                System.out.println("Current highest: "+highest+" and "+Character.getNumericValue(theCups[i]));

            if (Character.getNumericValue(theCups[i]) > highest)
                highest = Character.getNumericValue(theCups[i]);
        }

        return highest;
    }

    private boolean _debug;
}
@@ -242,16 +242,17 @@
      * -
+ 
 The crab

Update Game.java