-2

I need to write this code to the Hanoi tower game and I have some parts that are give so that they simply have to be that way(which I do not neccesserally agree with but whatever) I'm working in hungarian so some word are hungarian aswell but i don't think they can cause problems for the understanding of the code. The error I get is: EDIT: added some comments to find ther errors my bad

Exception in thread "main" EmptyTowerException
    at HanoiTower.pop(HanoiTower.java:49)
    at HanoiSimulator.move(HanoiSimulator.java:58)
    at HanoiSimulator.main(HanoiSimulator.java:72)

And here's my code:

import java.util.Scanner;

public class HanoiSimulator {
    public HanoiTower elso = new HanoiTower();
    public HanoiTower masodik = new HanoiTower();
    public HanoiTower harmadik = new HanoiTower();


    public HanoiSimulator(int x) throws InvalidMoveException {
        for (int i = x; i > 0; i--) {
            elso.put(x);
            x-=1;
        }

    }


    public boolean isFinished(){
        boolean win = false;
        if(masodik.korongSzam == 3)
        {
            win = true;
        }
        else{
            win = false;
        }
        return win;
    }

    public boolean move(int from, int to) throws EmptyTowerException, InvalidMoveException {
        HanoiTower A = new HanoiTower();
        HanoiTower B = new HanoiTower();
        System.out.println(elso);
        //System.out.println(A);
        if (from == 1) {
            A = elso;
            System.out.println(A);
        }
        else {
            if (from == 2) {
                A = masodik;
            }
            else {
                A = harmadik;
            }
        }
        if (to == 1) {
            B = elso;
        }
        else {
            if (to == 2) {
                B = masodik;
            }
            else {
                B = harmadik;
            }
        }
        int x = A.pop();         //ERROR 2
        B.put(x);
        return true;
    }

    public static void main(String[] args) throws InvalidMoveException, EmptyTowerException {
        HanoiSimulator simulator = new HanoiSimulator(4);
        Scanner sc=new Scanner(System.in);
        while(!simulator.isFinished()) {
            System.out.println(simulator);
            System.out.print("Which tower should I take the top disk from? (1-3) ");
            int from = sc.nextInt();
            System.out.print("On which tower should I put it down? (1-3) ");
            int to = sc.nextInt();
            if(simulator.move(from,to)) System.out.println("Move successful."); //ERROR 3
            else System.out.println("This move can not be carried out.");
        }
        System.out.println("Congrats, you win. You've just brought the end of the world. Thanks.... -.-");
    }
}

And here's the HanoiTower class:

import java.util.Arrays;

class EmptyTowerException extends Exception {

}

class InvalidMoveException extends Exception {

}

public class HanoiTower {
      private int magassag = 10;
      private int[] Torony = new int[magassag];
      public int korongSzam;

    @Override
    public String toString() {
        return "HanoiTower{" +
                "magassag=" + magassag +
                ", Torony=" + Arrays.toString(Torony) +
                ", korongSzam=" + korongSzam +
                '}';
    }

    public HanoiTower() {
        for (int i = 0; i < Torony.length; i++) {
            Torony[i] = 0;
        }

    }

    public HanoiTower(int h) {
        for (int i = 0; i < h; i++) {
            magassag = h;
            Torony[i] = h-i;
        }
    }

    public int pop() throws EmptyTowerException {
        int meret=0;
        for (int i = magassag-1; i < -1; i--) {
            if (Torony[i] > 0) {
                meret = Torony[i];
                Torony[i] = 0;
                break;
            }
        }
        if(meret == 0){
            throw new EmptyTowerException();   // ERROR 1
        }
        else {
            return meret; 
        }
    }

    public void put(int size) throws InvalidMoveException {
        for (int i = 0; i < Torony.length ; i++) {
            if(Torony[i] == 0) {
                Torony[i] = size;
                korongSzam +=1;
                break;
            }
            else
                if(Torony[i] < size)
                    throw new InvalidMoveException();
        }
        //System.out.println(Arrays.toString(Torony));
    }


    public static void main(String[] args) {
        HanoiTower test= new HanoiTower();
        try{
            test.pop();
            System.err.println("[ERROR] pop succesful from empty tower");
        } catch (EmptyTowerException e){
            System.err.println("[OK] pop unsuccesful from empty tower");
        }
        try{
            test.put(6);
            System.err.println("[OK] put 6 succesful on empty tower");
        } catch (InvalidMoveException e){
            System.err.println("[ERROR] put 6 succesful on empty tower");
        }
        try{
            test.put(4);
            System.err.println("[OK] put 4 succesful on tower [6]");
        } catch (InvalidMoveException e){
            System.err.println("[ERROR] put 4 succesful on tower [6]");
        }
        try{
            test.put(1);
            System.err.println("[OK] put 1 succesful on tower [6 4]");
        } catch (InvalidMoveException e){
            System.err.println("[ERROR] put 1 succesful on tower [6 4]");
        }
        try{
            test.put(2);
            System.err.println("[ERROR] put 2 succesful on tower [6 4 1]");
        } catch (InvalidMoveException e){
            System.err.println("[OK] put 1 succesful on tower [6 4 1]");
        }
        System.out.println(Arrays.toString(test.Torony));
    }

}
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
kurucza
  • 1
  • 4
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Andy Turner May 12 '20 at 21:14
  • @MarsAtomic I think that is the full text of the exception. – Andy Turner May 12 '20 at 21:16
  • @AndyTurner Yeah, I see what he's doing. – MarsAtomic May 12 '20 at 21:18
  • @MarsAtomic my bad i added some comments with the errors, but you also have to understand that I'm just a student that have been taught not enough and I had enough that no one in the Uni is capable or trying to help(including teachers) . I can't possibly figure this out on my own with the knowledge i have. – kurucza May 12 '20 at 21:22
  • Catch the exception from `A.pop()` and return false as handler. – Johannes Kuhn May 12 '20 at 21:25

1 Answers1

1

I have found the problem in your code. It should be:

i > -1

insted of

i < -1

in this method.

public int pop() throws EmptyTowerException {
    int meret=0;
    for (int i = magassag-1; i > -1; i--) {
        if (Torony[i] > 0) {
            meret = Torony[i];
            Torony[i] = 0;
            break;
        }
    }
    if(meret == 0){
        throw new EmptyTowerException();   // ERROR 1
    }
    else {
        return meret; 
    }
}

Also I would suggest to handle the Exception when it is thrown and tell user to change their choice. Good luck :)

Tudny
  • 96
  • 4
  • omg can't believe i missed that, been sitting over it for an hour but I'm just so tired, thank you <3 3rd phase of the assignment is to make a gui for it so maybe there i'll handle the exceptions with an answer – kurucza May 12 '20 at 21:49