The core code is pretty cool:
public class BackendAlgorithm { public static void move(int n, String from, String to, String using) { if (n == 0) return; move(n-1, from, using, to); HanoiContext.getInstance().fireMoveRequired(from, to); // Broadcast the move move(n-1, using, to, from); } }Again, it's mostly about rendering... 99% of the code of the applet above is for the graphical display!
The simplest user interface would look like this:
package hanoitower; public class BackendAlgorithm { public static void move(int n, String from, String to, String using) { if (n == 0) return; move(n-1, from, using, to); System.out.println("Moving from " + from + " to " + to); move(n-1, using, to, from); } public static void main(String[] args) throws Exception { move(Integer.parseInt(args[0]), "A", "C", "B"); // Moving A to C using B } }Run it this way:
Prompt> java hanoitower.BackendAlgorithm 4 Moving from A to B Moving from A to C Moving from B to C Moving from A to B Moving from C to A Moving from C to B Moving from A to B Moving from A to C Moving from B to C Moving from B to A Moving from C to A Moving from B to C Moving from A to B Moving from A to C Moving from B to CBoom!
No comments:
Post a Comment