Question

In: Computer Science

By using javaFX, write and design the Towers of Hanoi game with Redo and Undo features...

By using javaFX, write and design the Towers of Hanoi game with Redo and Undo features with play and solve options.

Solutions

Expert Solution

package TowerOfHanoi;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.Comparator;
import java.util.Optional;

public class TowerHanoiApp extends Application{
   private static final int Num_CIRCLES=7;
   private optional<Circle> selectedCircle=Optional.empty();
   @Override
   public void start(Stage stage) throws Exception{
       stage.setScene(new Scene(creteContent()));
       stage.show();
   }
   private Parents creteContent(){
       Pane root =new Pane();
       root.setPrefsize(400*3,400);
      
       for(int i=0;i<3;i++){
           Tower tower =new Tower (i*400,0);
           if (i==0){
               fo (int j=NUM_CIRCLES;j>0; j--){
                   Circle circle =new Circle (30 +j*20.null);
                   circle.setStroke(Color.BLACK);
                   Circle.setStrokewidth(circle.getRadius()/30.0);
                  
       tower.addCircle(circle);
               }
           }
           root.getChildren().add(tower);
       }
       return root;
   }
  
   private class Tower extends StackPane{
       Tower (int x,int y){
          
           setTranslatex(x);
           setTranslatex(y);
           setPrefsize(400,400);
          
           Rectangle bg=new Rectangle (25,25);
           bg.setOnMouseClicked(e -> {
               if (selectedCircle.isPresent()){
                   addCircle(selectedCircle.get());
                  
                   selectedCircle = Optional.empty();
               }else{
                   selectedCircle = Optional.ofNullable(getTopMOST());
               }
           });
           getChildren().add(bg);
       }
      
       private Circle getTopMost(){
           return getChildren().stream()
           .filter(n -> n instanceof circle)
                       .map(n -> (Circle) n)
                       .min(Comparator.comparingDouble(circle::getRadius))
                       .orElse(null);
       }
       void addcircle(circle circle){
circle topMost = getTopMost();
  
if (topMost ==null){
getChildren().add(circle);
}else{
if (circle.getRadius() < topMost.getRadius()){
getChildren().add(circle);   
}
}
       }
   }   
public static void main(String [] args){
launch(args);
   }     

// AND THE OUTPUT IN SNAPSHOT OF THIS CODE IN PHOTOS   
      
  
  
  
  


Related Solutions

Write a java program to solve Towers of Hanoi with the condition that there are "m"...
Write a java program to solve Towers of Hanoi with the condition that there are "m" number of rods and "n" number of disks. Where m >= 3 and n >=1.
towers of hanoi c++ program using stacks and singly linked lists.
towers of hanoi c++ program using stacks and singly linked lists.
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the following declaration: void towers(int n, char source, char dest, char spare); The function outputs a message describing each move. The source, destination, and spare poles are indicated with a character identifier of your choosing ('A', 'B', 'C' are common). Write a MIPS assembly language program that demonstrates the Towers of Hanoi procedure. Your program should ask the user for the number of disks. The...
Apply JavaFX and exception handling to design a CircleApp class using proper JavaFX GUI components, such...
Apply JavaFX and exception handling to design a CircleApp class using proper JavaFX GUI components, such as labels, text fields and buttons(Submit, Clear, and Exit) to compute the area of circle and display the radius user entered in the text field and computing result in a proper location in the application windows. It will also verify invalid data entries for radius(No letters and must be a postive real number) using expection handling. Your code will also have a custom-designed exception...
By using javaFX as the GUI, design and implement java based algorithms using appropriate data structures...
By using javaFX as the GUI, design and implement java based algorithms using appropriate data structures for the following problem: Use depth-first search to find paths to all the vertices in a graph that are connected to a given start vertex s. A sample input file containing the number of vertices, number of edges and a list of edges called tinyCG.txt is provided for you to test your program.
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is...
Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
In Java, Write a JavaFX application that draws multiple circles using a rubberbanding technique. The circle...
In Java, Write a JavaFX application that draws multiple circles using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.Thank you and could you also show the finished product?
Using NetBeans IDE, write a JavaFX application that allows theuser to choose insurance options. Use...
Using NetBeans IDE, write a JavaFX application that allows the user to choose insurance options. Use a ToggleGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). Use CheckBoxes for dental insurance and vision insurance options; the user can select one option, both options, or neither option. As the user selects each option, display its name and price in a text field; the HMO costs $200 per month, the...
(Not in Swing) Write a JavaFX application that creates polyline shapes dynamically using mouse clicks. Each...
(Not in Swing) Write a JavaFX application that creates polyline shapes dynamically using mouse clicks. Each mouse click adds a new line segment to the current polyline from the previous point to the current mouse position. Allow the user to end the current polyline with the double click. Also, provide a button that clears the window and allows the user to begin again.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT