Search code examples
javaarraylisttowers-of-hanoi

How to get the actual integer value from an array list


I am using an ArrayList of ArrayLists for a data structure to play a game of towers of hanoi. The game is constructed and initialized as follows:

private ArrayList<ArrayList> lists = new ArrayList<ArrayList>();
private ArrayList<Integer> peg1 = new ArrayList<Integer>();
private ArrayList<Integer> peg2 = new ArrayList<Integer>();
private ArrayList<Integer> peg3 = new ArrayList<Integer>();


//Constructor
public TowersOfHanoi() {

   lists.add(null);
   lists.add(peg1);
   lists.add(peg2);
   lists.add(peg3);
}

public ArrayList initializeGame(int n) {

    for (int i = 0; i < n; i++) {
        peg1.add(i+1);
    }

        return peg1;
    }
}

I am trying to use a Boolean method to do a check and make sure the user doesn't try to move a larger disc on top of a smaller disc, however, I don't understand how I would grab the the integer value stored in the arrayList. The integer values should serve as a way to gauge the diameter of the discs. I.E. 1 is smaller than two is smaller than 3 etc. This is the code I have come up with... I believe I am just getting the indexes and not the actual values of the integers stored there. How can I get the actual values?

public boolean isMoveLegal(int moveFrom, int moveTo){

  ArrayList<Integer> fromPeg = lists.get(moveFrom);
  int x = (fromPeg.remove(0)).intValue();

  ArrayList<Integer> toPeg = lists.get(moveTo);
  int y = (toPeg.get(0)).compareTo(x);

  if(x<y){

     System.out.println("illegal move");
  }

  return false;
}

Solution

  • The problem with your code is that isMoveLegal does not have a path where it would return true: the only return statement returns false. You should change the return statement as follows:

    return x >= y;
    

    This line is wrong too:

    int y = (toPeg.get(0)).compareTo(x);
    

    rather than getting the actual value into y, you are storing the result of comparison of y to x, which is not something you should compare to x again.

    Additionally, your check is invasive: it removes the disks at the top of your pegs (it is apparent from your code that the top of the tower corresponds to position zero; this is unorthodox, but you can certainly make it work).

    Rather than using remove(0), you should use get(0) after checking that the peg's content is not empty.

    If the "from" peg is empty, the move is invalid. If the "to" peg is empty, the move is valid. Otherwise, the regular comparison rules apply (i.e. a bigger disk cannot go on top of a smaller disk).

    public boolean isMoveLegal(int moveFrom, int moveTo){
        ArrayList<Integer> toPeg = lists.get(moveTo);
        if (toPeg.isEmpty()) return true; // You can move anything on an empty peg
        int y = toPeg.get(0).intValue();
        ArrayList<Integer> fromPeg = lists.get(moveFrom);
        if (fromPeg.isEmpty()) return false; // No disks on the "from" peg
        int x = fromPeg.get(0).intValue();
        if(x>y){
           System.out.println("illegal move");
           return false;
        }
        return true;
    }