In: Computer Science
By using javaFX, write and design the Towers of Hanoi game with Redo and Undo features with play and solve options.
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