vt-cs-projects / towerofhanoi / HanoiSolver.java
HanoiSolver.java
Raw
package towerofhanoi;

import java.util.Observable;

// 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 HanoiSolver extends Observable {

    private Tower left;
    private Tower right;
    private Tower middle;
    private int numDisks;

    /**
     * 
     * @param numDisks
     *            - the number of disks in the game
     */
    HanoiSolver(int numDisks) {
        this.numDisks = numDisks;
        left = new Tower(Position.LEFT);
        right = new Tower(Position.RIGHT);
        middle = new Tower(Position.MIDDLE);
    }


    /**
     * Getter for the number of disks
     * 
     * @return the number of disks
     */
    public int disks() {
        return numDisks;
    }


    /**
     * Getter for the right, left, and middle, towers
     * 
     * @param pos
     *            - the position of the tower
     * @return the tower at that position
     */
    public Tower getTower(Position pos) {
        if (pos == Position.RIGHT) {
            return right;
        }
        else if (pos == Position.LEFT) {
            return left;
        }
        return middle;
    }


    /**
     * toString method
     * 
     * @return a string signifying which disks are on which rod
     */
    public String toString() {
        StringBuilder str = new StringBuilder("");
        str.append(this.left.toString());
        str.append(this.middle.toString());
        str.append(this.right.toString());

        return str.toString();
    }


    /**
     * Allows you to move a disk from one tower to another
     * 
     * @param source
     *            - the tower you're moving the disk from
     * @param destination
     *            - the tower you're moving the disk to
     */
    private void move(Tower source, Tower destination) {
        Disk moved = source.pop();
        destination.push(moved);
        this.setChanged();
        notifyObservers(destination.position());
    }


    /**
     * Helper method to solve the game
     * 
     * @param currentDisks
     *            - the amount of disk being moved
     * @param startPole
     *            - the pole you start with
     * @param tempPole
     *            - the pole used to transfer
     * @param endPole
     *            - the end goal
     */
    private void solveTowers(
        int currentDisks,
        Tower startPole,
        Tower tempPole,
        Tower endPole) {

        if (currentDisks == 1) {
            move(startPole, endPole);
        }
        else {
            solveTowers(currentDisks - 1, startPole, endPole, tempPole);
            move(startPole, endPole);
            this.solveTowers(currentDisks - 1, tempPole, startPole, endPole);
        }

    }


    /**
     * solves the game
     */
    public void solve() {
        solveTowers(numDisks, right, middle, left);
    }

}