Question
Hi ! I have this code almost finished for a javafx program that shows moves and counts calls to method (Tower of Hanoi). Only thing
Hi ! I have this code almost finished for a javafx program that shows moves and counts calls to method (Tower of Hanoi). Only thing I am missing is to be able to print "move number",count as each move is being done. Like on this picture:
Code provided below.Thank you!
public class TowerOfFx extends Application{
double paneWidth = 500;
double paneHeight = 300;
TextField tfNumDisk;
static TextArea taPrintArea;
@Override // Override startmethod
public void start(Stage primaryStage) {
// Textfield for entering user input, ex. 43223
tfNumDisk = new TextField();
tfNumDisk.setPromptText("Number of disks..."); // Prompttext background tf
tfNumDisk.getText();
// TextArea for displaying text
taPrintArea = new TextArea();
// Alignment for textfield
tfNumDisk.setAlignment(Pos.TOP_RIGHT);
// Create button "Find moves"
Button btFind = new Button("Find Moves");
btFind.setAlignment(Pos.TOP_RIGHT);
// New pane, add textfield,textarea and button
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.add(tfNumDisk, 1, 0);
pane.add(taPrintArea, 1, 1);
pane.add(btFind,1,2);
// Create a scene and place it in the stage
Scene scene = new Scene(pane, paneWidth, paneHeight);
primaryStage.setTitle("TOWER OF HANOI"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
// Eventhandler for button "find moves"
btFind.setOnAction(new EventHandler
public void handle(ActionEvent event) {
int n = Integer.parseInt(tfNumDisk.getText());
moveDisks(n, 'A', 'B', 'C');
taPrintArea.appendText(" Number of calls to the method is: " + count);
tfNumDisk.clear();
}});}
static int count = 0;
public static void moveDisks(int n, char fromTower, char toTower, char auxTower) {
count++;
if (n == 1){ // BASIS
taPrintArea.appendText(" Move Number: "+(count-1) +"Move disk " + n + " from " + fromTower + " to " + toTower);
}
else {
moveDisks(n - 1, fromTower, auxTower, toTower);
taPrintArea.appendText(" Move number: " + (count-1) +"Move disk " + n + " from " + fromTower + " to " + toTower);
moveDisks(n - 1, auxTower, toTower, fromTower);}
}
public static void main(String[] args) { // Main method
System.out.println("");
launch(args);
}}
Number of disks Find moves Moves are r 1move disk 1 from A to C Move number: 2move disk 2 from A to B Move number: 3 move disk 1 from C to B Move number: 4 move disk 3 from A to C Move number: 5 move disk 1 from B to A Move number: 6 ove disk 2 from B to C Move number: 7 hove disk 1 from A to C Move number: 8 ove disk 4 from A to B Move number: 9 move disk 1 from C to B Move number: 10 move disk 2 from C to A Move number: 11 move disk 1 from B to A Move number: 12 move disk 3 from C to B Move number: 13 move disk 1 from A to C Move number: 14 move disk 2 from A to B Move number: 15 move disk 1 from C to B Number of calls to the method is: 15Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started