diff --git a/5.txt b/5.txt new file mode 100644 index 0000000000000000000000000000000000000000..179657fd771c83e18d5c379a2e674dc05140d18f --- /dev/null +++ b/5.txt @@ -0,0 +1,159 @@ +A: [5, 4, 3, 2, 1] +B: [] +C: [] + +Move disc 1 from A to C +A: [5, 4, 3, 2] +B: [] +C: [1] + +Move disc 2 from A to B +A: [5, 4, 3] +B: [2] +C: [1] + +Move disc 1 from C to B +A: [5, 4, 3] +B: [2, 1] +C: [] + +Move disc 3 from A to C +A: [5, 4] +B: [2, 1] +C: [3] + +Move disc 1 from B to A +A: [5, 4, 1] +B: [2] +C: [3] + +Move disc 2 from B to C +A: [5, 4, 1] +B: [] +C: [3, 2] + +Move disc 1 from A to C +A: [5, 4] +B: [] +C: [3, 2, 1] + +Move disc 4 from A to B +A: [5] +B: [4] +C: [3, 2, 1] + +Move disc 1 from C to B +A: [5] +B: [4, 1] +C: [3, 2] + +Move disc 2 from C to A +A: [5, 2] +B: [4, 1] +C: [3] + +Move disc 1 from B to A +A: [5, 2, 1] +B: [4] +C: [3] + +Move disc 3 from C to B +A: [5, 2, 1] +B: [4, 3] +C: [] + +Move disc 1 from A to C +A: [5, 2] +B: [4, 3] +C: [1] + +Move disc 2 from A to B +A: [5] +B: [4, 3, 2] +C: [1] + +Move disc 1 from C to B +A: [5] +B: [4, 3, 2, 1] +C: [] + +Move disc 5 from A to C +A: [] +B: [4, 3, 2, 1] +C: [5] + +Move disc 1 from B to A +A: [1] +B: [4, 3, 2] +C: [5] + +Move disc 2 from B to C +A: [1] +B: [4, 3] +C: [5, 2] + +Move disc 1 from A to C +A: [] +B: [4, 3] +C: [5, 2, 1] + +Move disc 3 from B to A +A: [3] +B: [4] +C: [5, 2, 1] + +Move disc 1 from C to B +A: [3] +B: [4, 1] +C: [5, 2] + +Move disc 2 from C to A +A: [3, 2] +B: [4, 1] +C: [5] + +Move disc 1 from B to A +A: [3, 2, 1] +B: [4] +C: [5] + +Move disc 4 from B to C +A: [3, 2, 1] +B: [] +C: [5, 4] + +Move disc 1 from A to C +A: [3, 2] +B: [] +C: [5, 4, 1] + +Move disc 2 from A to B +A: [3] +B: [2] +C: [5, 4, 1] + +Move disc 1 from C to B +A: [3] +B: [2, 1] +C: [5, 4] + +Move disc 3 from A to C +A: [] +B: [2, 1] +C: [5, 4, 3] + +Move disc 1 from B to A +A: [1] +B: [2] +C: [5, 4, 3] + +Move disc 2 from B to C +A: [1] +B: [] +C: [5, 4, 3, 2] + +Move disc 1 from A to C +A: [] +B: [] +C: [5, 4, 3, 2, 1] + diff --git a/Hanoi.class b/Hanoi.class index f4ed9a5db17bb0318b7850a44c42d602cde12260..b9ef82e6e196357102773d2612972fa77820eb0c 100644 Binary files a/Hanoi.class and b/Hanoi.class differ diff --git a/Hanoi.java b/Hanoi.java index ca13f23ff9f2ff9ccb4649b0cf8f8edda93be276..167b78f4a1ef3852ae27fbd8b4cf5182ab2efab4 100644 --- a/Hanoi.java +++ b/Hanoi.java @@ -1,16 +1,20 @@ import java.util.*; public class Hanoi { - Stack<String> a = new Stack<String>(); - Stack<String> b = new Stack<String>(); - Stack<String> c = new Stack<String>(); Stack<Integer>[] stacks = (Stack<Integer>[]) new Stack[3]; String[] names = {"A","B","C"}; + String output = ""; + int amount; - public Hanoi() { + public Hanoi(int amount) { for (int i = 0; i < names.length; i++) { stacks[i] = new Stack<>(); } + for (int i = amount; i >= 1; i--) { + stacks[0].push(i); + } + this.amount = amount; + printStacks(); } @@ -18,23 +22,35 @@ public class Hanoi { * @param from * @param to */ - public void move(int from, int to) { + private void move(int from, int to) { int disc = stacks[from].pop(); - stacks[to].push(disc); + if(stacks[to].empty() || disc < stacks[to].peek()){ + stacks[to].push(disc); - System.out.println("Move disc "+ disc +" from " + names[from] + " to " + names[to]); - printStacks(); + output += "Move disc "+ disc +" from " + names[from] + " to " + names[to] + "\n"; + printStacks(); + } else { + stacks[from].push(disc); + System.out.println("lmao no cheating"); + } + } + + public void move(String playerFrom, String playerTo){ + int from = 0; + int to = 0; + for (int i = 0; i < names.length; i++) { + if(names[i].equals(playerFrom)) from = i; + if(names[i].equals(playerTo)) to = i; + } + move(from,to); + amount++; } /** * @param amount */ - public void start(int amount) { - for (int i = amount; i >= 1; i--) { - stacks[0].push(i); - } - printStacks(); + public void start() { hanoi(amount, 0, 1, 2); } @@ -57,10 +73,20 @@ public class Hanoi { public void printStacks() { for (int i = 0; i < names.length; i++) { - System.out.print(names[i]+": "); - System.out.println(stacks[i]); + output += names[i]+": "; + output += stacks[i].toString()+"\n"; } - System.out.println(); + output += "\n"; + } + + /** + * @return the output + */ + public String getOutput() { + String tmp = output; + output = ""; + return tmp; + } } \ No newline at end of file diff --git a/Main.class b/Main.class index 8aaa34fe2b9e09b770d8ea1c2832692e1df6c0a0..8f28f72c9dff6700f15d1c379d522da00a2f8040 100644 Binary files a/Main.class and b/Main.class differ diff --git a/Main.java b/Main.java index d34a007c2365f1ab6c3a1f61b16ba45ff251a3da..52ffc3fd5d9e7863844709c58a25c993240d2ac3 100644 --- a/Main.java +++ b/Main.java @@ -1,9 +1,29 @@ +import java.util.*; public class Main { public static void main(String[] args) { - int amount = Integer.parseInt(args[0]); + String mode = "game"; + int amount = 4; - Hanoi test = new Hanoi(); - test.start(amount); + Scanner input = new Scanner(System.in); + String from, to; + + Hanoi hanoi = new Hanoi(amount); + if(mode.equals("game")){ + System.out.println(hanoi.getOutput()); + while(true){ + System.out.print("Flytta från: "); + from = input.nextLine(); + System.out.print("Flytta till: "); + to = input.nextLine(); + System.out.println(); + + hanoi.move(from,to); + System.out.println(hanoi.getOutput()); + } + } else { + hanoi.start(); + System.out.println(hanoi.getOutput()); + } } }