Michael Queue
Michael Queue

Reputation: 1400

ArrayList of ArrayLists for pegs and discs in towers of hanoi game

I am building a towers of hanoi game that can be played from the console or the command line. Note this isn't a recursive program; I am trying to build a GAME that can be played by the user. I am using an ArrayList of ArrayLists to store the pegs(1,2,3) and the discs(N) chosen by the user.

My instance variables and constructor:

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

//Constructor
public TowersOfHanoi() {

   ArrayList<Integer>[] lists = new ArrayList[3];
   lists[0]= this.peg1;
   lists[1]= this.peg2;
   lists[2]= this.peg3;

}

How I initialize the game

public ArrayList initializeGame(int n) {

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

I am trying to make a move method that will take two INTS as parameters for the from and to posts. I.E. user moves disc from post N to post N, and checks if that move is legal.

I have tried to figure this out for the last couple hours but I can't even figure out how to get the appropriate peg and move the disk from one peg to another let alone check to see that a larger disk isn't being moved on top of a smaller disk. This is the method I came up with and I am getting a null pointer exception which makes me think that lists is not initialized correctly. This is the method I have which is obviously wrong.

public void move(int moveFrom, int moveTo){

    lists.get(moveTo)
    lists.add(0, moveTo);
}

So can anyone help me understand how to use an int as an index to get the appropriate peg (int is entered by user) and move a disc?

Upvotes: 1

Views: 1470

Answers (1)

Tejay Cardon
Tejay Cardon

Reputation: 4223

lists is not being initialized because you declare an instance object of type ArrayList

private ArrayList lists;

Which is null. Then, in your constructor, you declare and initialize a local object of type ArrayList[] - as in an array of ArrayLists

ArrayList<Integer>[] lists = new ArrayList[3];

You then put your pegs into this array of ArrayList's

lists[0]= this.peg1;
lists[1]= this.peg2;
lists[2]= this.peg3;

and then you exit the constructor without doing anything with the array. You need to either:

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

//Constructor
public TowersOfHanoi() {

   lists = new ArrayList[3];
   lists[0]= this.peg1;
   lists[1]= this.peg2;
   lists[2]= this.peg3;

}

OR

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

//Constructor
public TowersOfHanoi() {

   lists.add(this.peg1);
   lists.add(this.peg2);
   lists.add(this.peg3);

}

OR most likely

private ArrayList[] lists = new ArrayList[3];
private ArrayList peg1 = new ArrayList<>();
private ArrayList peg2 = new ArrayList<>();
private ArrayList peg3 = new ArrayList<>();

//Constructor
public TowersOfHanoi() {

   lists[0]= this.peg1;
   lists[1]= this.peg2;
   lists[2]= this.peg3;

}

Upvotes: 1

Related Questions