Create a project that has a method called getSolutionint mov

Create a project that has a method called getSolution(int moveIndex, int discCount) that prints out on consecutive lines the moves for the solution. The moveIndex is 0-5, corresponding to the index of {\"10\",\"12\",\"02\",\"01\",\"21\",\"20\"}

The attached project is a starter. You will see that these methods aren\'t recursive. Those methods would have to be called from the getSolution method, which is recursive.

Also note that the getSolution method will have to handle recursion a little differently. Rather than returning values, each call to getSolution will update the relevant item in solutionArray with the index of the relevant move. Because finding the relevant item in solutionArray can be hard, I created some methods to help you (one of which has an enormous method name).

One final note: The project requires working with two arrays: (a) the move array (6 items), which holds the middle move (two digits: the peg you move from and the peg you move to) for each solution, and (b) the solution array, which holds the move index for each successive move in the game. Don\'t get these two arrays confused. The move array always has 6 items and the items never change. The solution array has 1 item when the game has 1 disk, 3 items when the game has two disks, 7 items when the game has three disks ... etc. The solution array holds the indexes for the relevant move item.

http://pastebin.com/j4mp12LC - Starter

Solution

package hanoi;

public class Hanoi {

    private static String[] move = new String[]{\"10\",\"12\",\"02\",\"01\",\"21\",\"20\"};


    public static void main(String[] args) {
        String[] solution = getSolution(0,3);
        System.out.println(\"SOLUTION:\");
        for(String s : solution){
            System.out.println(s);
        }
    }


    private static String[] getSolution(
            int moveIndex, int discCount){
     
        int solutionSize = (int)java.lang.Math.pow(2,discCount) - 1;
     
        int[] solutionArray = new int[solutionSize];
        int middleIndex = (int)Math.pow(2, discCount-1) - 1;
     
        getSolution(solutionSize,middleIndex,solutionArray,moveIndex);
     
        String[] stringSolutionArray = new String[solutionSize];
        for(int i=0;i<solutionSize;i++)
            stringSolutionArray[i] = move[solutionArray[i]];
     
        return stringSolutionArray;
    }

    private static void getSolution(int solutionSize,
            int middleIndex,
            int[] solutionArray,
            int moveIndex){

        //todo: add recursion implementation
        //note: rather than returning a number, you
        //      update the solutionArray at the appropriate index
        //      and make recursive calls to getSolution
        //      Make use of getChildSolutionSize,
        //        getMiddleIndexFromSolutionSizeAndParentMiddleIndex,
        //        getChildTopMoveIndex, getChildBottomMoveIndex

       if(solutionSize != 1)
           getSolution(getChildSolutionSize(solutionSize),getMiddleIndexFromSolutionSizeAndParentMiddleIndex(getChildSolutionSize(solutionSize),middleIndex,true),solutionArray,getChildTopMoveIndex(moveIndex));

       solutionArray[middleIndex] = (solutionSize-moveIndex)%(solutionSize-1);

       if(solutionSize != 1)
           getSolution(getChildSolutionSize(solutionSize),getMiddleIndexFromSolutionSizeAndParentMiddleIndex(getChildSolutionSize(solutionSize),middleIndex,false),solutionArray,getChildBottomMoveIndex(moveIndex));
    }

    private static int getChildSolutionSize(int solutionSize){
        return ((solutionSize + 1)/2) -1;
    }

    private static int getMiddleIndexFromSolutionSizeAndParentMiddleIndex(
            int solutionSize, int parentMiddleIndex, boolean isTop){
        if (isTop)
            return parentMiddleIndex - (int)Math.pow(2, logBase2(solutionSize+1)-1);
        else
            return parentMiddleIndex + (int)Math.pow(2, logBase2(solutionSize+1)-1);
    }

    private static int logBase2(int n){
        return (int)(Math.log10(n)/Math.log10(2));
    }

    private static int getChildTopMoveIndex(int moveIndex){
        return ( moveIndex%2==1 ) ? getLeftMoveIndex(moveIndex) : getRightMoveIndex(moveIndex) ;
    }

    private static int getChildBottomMoveIndex(int moveIndex){
        return ( moveIndex%2==0 ) ? getLeftMoveIndex(moveIndex) : getRightMoveIndex(moveIndex) ;
    }

    private static int getLeftMoveIndex(int moveIndex){
        return 5 - ((6-moveIndex)%6);
    }

    private static int getRightMoveIndex(int moveIndex){
        return (moveIndex+1) % 6;
    }


}

Create a project that has a method called getSolution(int moveIndex, int discCount) that prints out on consecutive lines the moves for the solution. The moveInd
Create a project that has a method called getSolution(int moveIndex, int discCount) that prints out on consecutive lines the moves for the solution. The moveInd

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site