Sunday, 9 October 2011

4.10-4.13


STUDENT TASKS
4_10. The minimum number of moves for a one disk game is one, and for a two disk game three. Try to find a formula, in terms of the number of disks (n), which correctly expresses the minimum number of moves required to complete the game for any given (n).

4.10
f(n) = 2n-1 where n is the number of disks being played with.

4_11. Modify method play() so that the player is unable to place a larger disk on a smaller dsk.. Modify play() so that the game will only accept correct input from the user ('b,'g' or 'y').

4.11
public void play()
{
    setUpPegs();

    ArrayStack blue=new ArrayStack(maxPegs);
    ArrayStack green=new ArrayStack(maxPegs);
    ArrayStack yellow=new ArrayStack(maxPegs);

    int difficulty;
    SimpleInput inputDifficulty = new SimpleInput();
    difficulty=inputDifficulty.getInt("How many disks would you like?");

    for (int i=0;i<difficulty;i++)
    {
        disk temp=new disk(20,100-(i*10),0+(i*5),180-(i*20),"red",true);
        blue.push(temp);
        temp.makeVisible();
    }//for

    reDraw(blue,green,yellow);


    while(!(blue.isEmpty() && green.isEmpty()))
    {
        char from, to;
        SimpleInput moveCharacter = new SimpleInput();
        from = moveCharacter.getChar("from: b=blue, g=green, y=yellow");
        to = moveCharacter.getChar("to: b=blue, g=green, y=yellow");

        disk carry=null;

        switch (from)
        {
            case 'b': carry=(disk)blue.pop(); break;
            case 'g': carry=(disk)green.pop();break;
            case 'y': carry=(disk)yellow.pop();break;
            default : System.out.println("error");
        }//switch

        switch (to)
        {
            case 'b':     if(blue.peekTop() != null && ((disk)blue.peekTop()).getWidth()<carry.getWidth()){
                          System.out.println("Please put a smaller disk on a larger disk only."); }
                          else{
                          carry.changePosition(carry.getXPosition()%100,180-(stackHeight(blue)*20));
                          blue.push(carry); break; }
            case 'g':     if(green.peekTop() != null && ((disk)green.peekTop()).getWidth()<carry.getWidth()){
                          System.out.println("Please put a smaller disk on a larger disk only."); }
                          else{
                          carry.changePosition((carry.getXPosition()%100)+100,180-(stackHeight(green)*20));
                          green.push(carry); break; }
            case 'y':     if(yellow.peekTop() != null && ((disk)yellow.peekTop()).getWidth()<carry.getWidth()){
                          System.out.println("Please put a smaller disk on a larger disk only."); }
                          else{
                          carry.changePosition((carry.getXPosition()%100)+200,180-(stackHeight(yellow)*20));
                          yellow.push(carry); break; }
            default : System.out.println("error");
        }//switch

        reDraw(blue,green,yellow);
    }//while(main loop)

    System.out.println("Well Done");

}//play

}//class TowersOfHanoi

4_12. Create a new class named TowersDemonstration which will prompt the user for the number of disks, as in TowersOfHanoi, but which will demonstrate the game by playing automatically. As you develop the algorithm for this class you should consider both iterative and recursive approaches.
There is a classic recursive solution to Towers of Hanoi. Try to find this solution for yourself, and then try to code the solution. If you have difficulty working out the solution for yourself, search for a solution on the web.

4.12
I tried integrating this step for a long time. I don't understand how to put the algorithm I found into the method... and I also can't understand where the method switch() came from (I looked through all the other classes and was unable to find it).

4_13. Compile class ArithmeticExpression. Create an instance of the class on the BlueJ workbench with a valid arithmetic expression String parameter. Execute the evaluate() method with the following expressions. Do you get the predicted results?

4.13
Yes, except for the last one. The expression was "(a/b)" where a = 1 and b = 0. I expected to get either "null" or a string message saying "undefined" (although Mr. Reid would argue "infinity" would be a more accurate message). Instead however, the program could not process the arithmetic expression and broke the method giving me an error. This could easily be coded into giving me either "null" or "undefined".

No comments:

Post a Comment