CS-PROJECTS / towerofhanoi / PuzzleWindow.java
PuzzleWindow.java
Raw
package towerofhanoi;

import java.awt.Color;
import java.awt.Rectangle;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import CS2114.Button;
import CS2114.Shape;
import CS2114.Window;
import CS2114.WindowSide;

// Virginia Tech Honor Code Pledge:
//
// As a Hokie, I will conduct myself with honor and integrity at all times.
// I will not lie, cheat, or steal, nor will I accept the actions of those who
// do.
// -- Jordan Harrington (jordanha23)

/**
 * @author Jordan Harrington
 * @version <3/25/2020>
 */

public class PuzzleWindow implements Observer {

    private HanoiSolver game;
    private Shape left;
    private Shape right;
    private Shape middle;
    private Window window;
    public static final int WIDTH_FACTOR = 10;
    public static final int DISK_GAP = 0;
    public static final int DISK_HEIGHT = 5;

    /**
     * Constructor
     * 
     * @param game
     *            - the Hanoi game you want displayed
     */
    PuzzleWindow(HanoiSolver game) {
        this.game = game;
        this.game.addObserver(this);
        window = new Window("Tower of Hanoi");

        left = new Shape(100, 50, 5, 100, Color.RED);
        middle = new Shape(300, 50, 5, 100, Color.RED);
        right = new Shape(500, 50, 5, 100, Color.RED);

        for (int i = game.disks(); i > 0; i--) {
            int x = i * DISK_HEIGHT;
            Disk newDisk = new Disk(x);

            window.addShape(newDisk);
            game.getTower(Position.RIGHT).push(newDisk);
            moveDisk(Position.RIGHT);
        }

        window.addShape(this.left);
        window.addShape(this.middle);
        window.addShape(this.right);

        Button solveButton = new Button("Solve");
        window.addButton(solveButton, WindowSide.SOUTH);
        solveButton.onClick(this, "clickedSolve");

    }


    /**
     * Updates the window when disks are moved
     */
    public void update(Observable o, Object arg) {
        if (arg.getClass() == Position.class) {
            Position args = (Position)arg;
            moveDisk(args);
            sleep();
        }
    }


    /**
     * Pauses between disk movements
     */
    private void sleep() {
        try {
            Thread.sleep(500);
        }
        catch (Exception e) {
        }
    }


    /**
     * Gives the solve button a function
     * 
     * @param button
     *            - the button to be clicked
     */
    public void clickedSolve(Button button) {
        button.disable();
        new Thread() {
            public void run() {
                game.solve();
            }
        }.start();
    }


    /**
     * Moves the disk in the game
     * 
     * @param position
     *            - the position you want the disk moved to
     */
    private void moveDisk(Position position) {
        Disk currentDisk = game.getTower(position).peek();
        Shape currentPole = new Shape(0, 0);

        if (position == Position.LEFT) {
            currentPole = left;
        }
        else if (position == Position.RIGHT) {
            currentPole = right;
        }
        else {
            currentPole = middle;
        }

        int nextX = currentPole.getX() - (currentDisk.getWidth() / 2)
            + (currentPole.getWidth() / 2);
        int nextY = currentPole.getY() - game.getTower(position).size()
            * DISK_HEIGHT + currentPole.getHeight();
        currentDisk.moveTo(nextX, nextY);

    }

}