diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/HanoiServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/HanoiServer.java new file mode 100644 index 0000000..652c21d --- /dev/null +++ b/app/src/main/java/ch/nuth/zhaw/exbox/HanoiServer.java @@ -0,0 +1,25 @@ +package ch.nuth.zhaw.exbox; + +/** + * Provides the functionality to determine the solution of a hanoi puzzle. + */ +public class HanoiServer implements CommandExecutor { + @Override + public String execute(String command) throws Exception { + int count = Integer.parseInt(command); + return moveDisks(count, 'A', 'B', 'C'); + } + + public String moveDisks(int count, char sourcePole, char restPole, char destinationPole) { + String start = ""; + String mid = String.format("Move disk %d from %s to %s", count, sourcePole, destinationPole); + String end = ""; + + if (count > 1) { + start = moveDisks(count - 1, sourcePole, destinationPole, restPole); + end = moveDisks(count - 1, restPole, sourcePole, destinationPole); + } + + return String.join(System.lineSeparator(), start, mid, end).trim(); + } +}