0

I'm working on a Tower of Hanoi project for school which needs to ask the user how many disks there are and then it needs to create and then solve the tower with a visual included. How I decided to do it is by using 2D arrays and for the most part its working, my only problem is that I don't know how to move the disks while keeping it modular. Here is my code.

public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter number of disks");
    int num = scan.nextInt(); 
    int temp = num-(num-1);

    int measure = num;
    //initializing the towers
    int[][] towers = new int[num][num];

    for(int i = 0 ; i < num; i++)
    {
        for(int j=0; j <3; j++)
        {
        }
    }
    createRings(towers, num, temp);
    moveDisk(towers,num);
}

// creating the rings
private static void createRings (int[][]towers, int num, int temp)
{
    for(int i = 0; i<num; i++)
    {
        for(int j=0; j<3;j++)
        {
            towers[i][0] = temp;    
        }
        temp = temp+1;

    }
    displayTower(towers, num);
}

// prints the array for display purposes
private static void displayTower (int[][] towers, int num)
{
    for(int i = 0; i<num; i++)
    {
        for(int j = 0; j<3; j++)
        {
            System.out.print(towers[i][j]+"\t");
        }
        System.out.println();
    }
}
//moves the numbers in the array that represents disks
private static void moveDisk(int[][]towers, int num)
{

    System.out.println();
    displayTower(towers, num);
}

Does anyone have any suggestions on what I could do?

  • What have you tried, and why doesn't it work? – Krease Apr 09 '18 at 14:42
  • https://stackoverflow.com/q/1223305/3579095 – Lars Apr 09 '18 at 14:56
  • @Krease I've tried using a recursion code like the one in the link that Lars provided and my idea was to modify it to take the 3 columns of my array but I wasn't able to figure out how to pass an individual column from a 2D array. – Skeletonized Apr 09 '18 at 15:15

1 Answers1

0

I've changed your code a bit to make it more readable for me.

  1. I changed the towers array. Now each tower is its own array inside the towers array (makes more sense IMHO).

  2. Arrays in Java know their size. So you don't have to pass the length of the array as a parameter to every method.

  3. I added a method getHighestIdx() which returns the index before the first 0 value in the array

  4. I don't know, how the function moveDisk() was intended to work. I changed the declaration so that it makes sense to me. It now moves a disk from tower i to tower j

This should help you to implement the algorithm from the linked question. Here is the changed code:

public static void main(String[] args) {
    int numberOfRings = 6;
    int[][] towers = new int[3][numberOfRings];

    createRings(towers);
    displayTowers(towers);
    moveDisk(towers, 0, 2);
    displayTowers(towers);

}

private static void createRings(int[][] towers) {
    for (int j = 0; j < towers[0].length; j++) {
        towers[0][j] = j + 1;
    }
}

private static void displayTowers(int[][] towers) {
    for (int i = 0; i < towers[0].length; i++) {
        for (int j = 0; j < towers.length; j++) {
            System.out.print(towers[j][i] + "    ");
        }
        System.out.println("");
    }

}

private static void moveDisk(int[][] towers, int fromIdx, int toIdx) {
    int valToMove = towers[fromIdx][getHighestIdx(towers[fromIdx])];
    towers[fromIdx][getHighestIdx(towers[fromIdx])] = 0;
    towers[toIdx][getHighestIdx(towers[toIdx]) + 1] = valToMove;
}

private static int getHighestIdx(int[] tower) {
    int i = 0;
    while (i < tower.length && tower[i] != 0) {
        i++;
    }
    return i - 1;
}
Lars
  • 1,750
  • 2
  • 17
  • 26