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));
}
}