1. 程式人生 > >漢諾塔小遊戲—圖形介面

漢諾塔小遊戲—圖形介面

 

執行效果: 

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javax.swing.text.html.ImageView;
import java.util.Stack;
class MoveRoute{
    int a,b;
    public MoveRoute(int a,int b){
        this.a=a;this.b=b;
    }
}
class Dish extends Ellipse{
    int size;
    int stk=0;
    boolean pd=false;
    public Dish(int x,int y,int size){
        stk=x-1;
        this.size=size;
        setCenterX(x*200);
        setCenterY(390-y*20);
        setRadiusX(15+size*5);
        setRadiusY(10);
        setFill(Color.rgb(60,50+(200/size),255));
    }
}
public class Hannoi extends Application {
    int num=7;
    Stack<Dish> stack[]=new Stack[3];
    Stack<MoveRoute> route=new Stack<>();
    Pane pane=new Pane();
    Scene scene=new Scene(pane);
    Dish dish[];
    Line line[]=new Line[3];
    Label txNum=new Label("盤子個數");
    TextField tfNum=new TextField();
    Button btBack=new Button("撤銷"),btChangeNum=new Button("修改");
    public void init(){
        Rectangle bg=new Rectangle(100,10,600,400);
        bg.setFill(Color.rgb(232,240,255));
        pane.getChildren().add(bg);
        for(int i=0;i<3;i++)
            stack[i]=new Stack<>();
        LoadButton();
        LoadLine();
        LoadDish();
    }
    public void LoadButton(){
        btBack.setLayoutX(800);
        btBack.setLayoutY(300);
        btChangeNum.setLayoutX(900);
        btChangeNum.setLayoutY(200);
        pane.getChildren().add(btBack);
        pane.getChildren().add(btChangeNum);
        tfNum.setLayoutX(800);
        tfNum.setLayoutY(200);
        tfNum.setMaxWidth(80);
        txNum.setLayoutX(810);
        txNum.setLayoutY(180);
        pane.getChildren().add(txNum);
        pane.getChildren().add(tfNum);
        btChangeNum.setOnMouseClicked(e->{
            num=Integer.valueOf(tfNum.getText());
            for(int i=0;i<3;i++){
                pane.getChildren().remove(line[i]);
                while (!stack[i].empty())
                    stack[i].pop();
            }
            while (!route.empty())
                route.pop();
            for(int i=0;i<dish.length;i++)
                pane.getChildren().remove(dish[i]);
            LoadLine();
            LoadDish();
        });
        btBack.setOnMouseClicked(e->{
            if(!route.empty()){
                move(route.peek().b,route.peek().a);
                route.pop();
            }
        });
    }
    public  void LoadLine(){
        for(int i=0;i<3;i++){
            line[i]=new Line((i+1)*200,400,(i+1)*200,400-num*25);
            pane.getChildren().add(line[i]);
        }
    }
    public void LoadDish(){
        dish=new Dish[num+1];
        for(int i=num;i>0;i--){
            dish[i]=new Dish(1,num-i,i);
            stack[0].push(dish[i]);
            pane.getChildren().add(dish[i]);
            int t=i;
            dish[i].setOnMouseDragged(e->{
                if(stack[dish[t].stk].peek().equals(dish[t])){
                    dish[t].setCenterX(e.getX());
                    dish[t].setCenterY(e.getY());
                }
            });
            dish[t].setOnMouseReleased(e->{
                if(stack[dish[t].stk].peek().equals(dish[t])){
                    int x=(int)(e.getX()-100)/200;
                    if(e.getX()<700&&(stack[x].empty()||stack[x].peek().size>dish[t].size)){
                        route.push(new MoveRoute(dish[t].stk,x));
                        move(dish[t].stk,x);
                        gameOver();
                    }
                    else{
                        dish[t].setCenterY(390-(stack[dish[t].stk].size()-1)*20);
                        dish[t].setCenterX((dish[t].stk+1)*200);
                    }
                }
            });
        }
    }
    public void move(int a,int b){
        stack[a].peek().setCenterX((b+1)*200);
        stack[a].peek().setCenterY(390-stack[b].size()*20);
        stack[a].peek().stk=b;
        stack[b].push(stack[a].pop());
    }
    public void gameOver(){
        if(stack[2].size()==num){
            Stage newStage=new Stage();
            Pane p=new Pane();
            Scene newScene=new Scene(p);
            Button btYes=new Button("yes"),btNo=new Button("no");
            btYes.setOnMouseClicked(e->{ pane.getChildren().clear();init();newStage.close(); });
            btNo.setOnMouseClicked(e->{System.exit(0);});
            btYes.setLayoutX(50);
            btYes.setLayoutY(100);
            btNo.setLayoutX(120);
            btNo.setLayoutY(100);
            Label over=new Label("遊戲結束,是否重來");
            over.setLayoutY(20);
            over.setLayoutX(45);
            p.getChildren().add(over);
            p.getChildren().add(btYes);
            p.getChildren().add(btNo);
            newStage.setWidth(200);
            newStage.setHeight(150);
            newStage.setResizable(false);
            newStage.setScene(newScene);
            newStage.show();
        }
    }
    public void start(Stage stage){
        stage.setTitle("漢諾塔");
        stage.setScene(scene);
        stage.setWidth(970);
        stage.setHeight(460);
        stage.setResizable(false);
        stage.show();
    }
}