pyuntae
pyuntae

Reputation: 832

Towers of Hanoi using Stacks

I am creating a simple text based game of Towers of Hanoi for users to play. The program should ask the user which peg to move from and which peg to move to. I have to use Stacks for this program. I also have to implement these given methods:

enter image description here

Here is what I have so far: Towers.java

import java.util.*;

public class Towers 
{   
    public class Towers {

        private List<Integer> peg1;
        private List<Integer> peg2;
        private List<Integer> peg3;

        Towers(int n){
            peg1 = new LinkedList<Integer>();
            peg2 = new LinkedList<Integer>();
            peg3 = new LinkedList<Integer>();
        }

        public int countRings(int pegNumber){

            if (pegNumber == 1)
            {
                return peg1.size();
            }
            if (pegNumber == 2)
            {
                return peg2.size();
            }
            if (pegNumber == 3)
            {
                return peg3.size();
            }

            return -1;
        }

        public int getTopDiameter(int pegNumber){

            if (countRings(pegNumber) > 0)
            {
                if (pegNumber == 1)
                {
                    return peg1.get(peg1.size()-1); // Return the last disc from peg1
                }
                if (pegNumber == 2)
                {
                    return peg2.get(peg2.size()-1); // Return the last disc from peg2
                }
                if (pegNumber == 3)
                {
                    return peg3.get(peg3.size()-1); // Return the last disc from peg3
                }
            }
            return 0;

        }

        public void move(int startPeg, int endPeg)
        {

            int pegMove = 0; // Temporary spot to hold disc

            if(startPeg == 1)
            {

                if(endPeg == 1)
                {
                    pegMove = peg1.get(peg1.size() - 1); // Take the last disc from startPeg, add to discMove

                    peg1.remove(peg1.size() - 1);// Remove the disc from startPeg

                    peg1.add(pegMove); // Add disc to the endPeg
                    System.out.printf( "\n%d --> %d", startPeg,  endPeg);
                }
                else if(endPeg == 2)
                {
                    pegMove = peg1.get(peg1.size() - 1); // Take the last disc from startPeg, add to discMove

                    peg1.remove(peg1.size() - 1); // Remove the disc from startPeg

                    peg2.add(pegMove); // Add disc to the endPeg
                    System.out.printf( "\n%d --> %d", startPeg,  endPeg);
                }
                else if(endPeg == 3)
                {
                    pegMove = peg1.get(peg1.size() - 1); // Take the last disc from startPeg, add to discMove

                    peg1.remove(peg1.size() - 1); // Remove the disc from startPeg

                    peg3.add(pegMove); // Add disc to the endPeg
                    System.out.printf( "\n%d --> %d", startPeg,  endPeg);
                }
            }


            if(startPeg == 2)
            {

                if(endPeg == 1)
                {
                    pegMove = peg2.get(peg2.size() - 1); // Take the last disc from startPeg, add to discMove

                    peg2.remove(peg2.size() - 1);// Remove the disc from startPeg

                    peg1.add(pegMove); // Add the disc to the endPeg
                    System.out.printf( "\n%d --> %d", startPeg,  endPeg);

                }
                else if(endPeg == 2)
                {
                    pegMove = peg2.get(peg2.size() - 1); // Take the last disc from startPeg, add to discMove

                    peg2.remove(peg2.size() - 1); // Remove the disc from startPeg

                    peg2.add(pegMove); // Add the disc to the endPeg
                    System.out.printf( "\n%d --> %d", startPeg,  endPeg);
                }
                else if(endPeg == 3)
                {
                    pegMove = peg2.get(peg2.size() - 1); // Take the last disc from startPeg, add to discMove

                    peg2.remove(peg2.size() - 1); // Remove the disc from startPeg

                    peg3.add(pegMove); // Add the disc to the endPeg
                    System.out.printf( "\n%d --> %d", startPeg,  endPeg);
                }
            }


            if(startPeg == 3) 
            {

                if(endPeg == 1)
                {
                    pegMove = peg3.get(peg3.size() - 1); // Take the last disc from startPeg, add to discMove

                    peg3.remove(peg3.size() - 1);// Remove the disc from startPeg

                    peg1.add(pegMove); // Add the disc to the endPeg
                    System.out.printf( "\n%d --> %d", startPeg,  endPeg);
                }
                else if(endPeg == 2)
                {
                    pegMove = peg3.get(peg3.size() - 1); // Take the last disc from startPeg, add to discMove

                    peg3.remove(peg3.size() - 1); // Remove the disc from startPeg

                    peg2.add(pegMove); // Add the disc to the endPeg
                    System.out.printf( "\n%d --> %d", startPeg,  endPeg);
                }
                else if(endPeg == 3)
                {
                    pegMove = peg3.get(peg3.size() - 1); // Take the last disc from startPeg, add to discMove

                    peg3.remove(peg3.size() - 1); // Remove the disc from startPeg

                    peg3.add(pegMove); // Add the disc to the endPeg
                    System.out.printf( "\n%d --> %d", startPeg,  endPeg);
                }
            }
        }
    }   

For some reason it won't compile correctly and I cannot figure out what to do next.

 ----jGRASP exec: javac -g Towers.java

Towers.java:160: error: reached end of file while parsing
}   
 ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

Upvotes: 0

Views: 412

Answers (1)

radoh
radoh

Reputation: 4815

Your class begins with

import java.util.*;

public class Towers 
{   
    public class Towers {

        private List<Integer> peg1;
        ...

While it should begin only with

import java.util.*;

public class Towers 
{   
    private List<Integer> peg1;
    ...

Upvotes: 2

Related Questions