導航:首頁 > 編程語言 > javafx繪制棋盤

javafx繪制棋盤

發布時間:2024-06-20 13:38:13

Ⅰ 用分治法求解Tromino謎題

import java.lang.Math;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class termino extends Application {
public static void main(String[] args) {
launch(args);
}
GridPane pane = new GridPane();
static ArrayList<Integer> list = new ArrayList<>();
TextField ktf = new TextField();
TextField xtf = new TextField();
TextField ytf = new TextField();
Button qrbutton = new Button("確認");
static int c=1;
int i=0;
int t=1;
static int [][] a= new int[100][100];

@Override
public void start(Stage primaryStage)
{
pane.add(getHBox(),0,0);
qrbutton.setOnAction(e->run());
Scene scene = new Scene(pane);
primaryStage.setTitle("termino");
primaryStage.setScene(scene);
primaryStage.show();
}

void run()
{
pane.getChildren().clear();
int dr = Integer.parseInt(xtf.getText());
int dc = Integer.parseInt(ytf.getText());
int s = Integer.parseInt(ktf.getText());
Tromino(0,0,dr,dc,(int)Math.pow(2,s));
for(int t = 0; t <(int)Math.pow(2,s) ; t++)
{
for (int j = 0; j <(int)Math.pow(2,s); j++)
System.out.printf("%-3d ", a[t][j]);
System.out.print("\n");
}
System.out.print("\n");
for(int j=0;j<list.size();j+=2)
{
Rectangle r = new Rectangle(30,30);
r.setFill(Color.GRAY);
pane.add(r,list.get(j+1),list.get(j));
}
for(int j=0;j<list.size();j++)
{
System.out.println(list.get(j));
}
Button button = new Button("下一步");
pane.add(button, list.size(), list.size());
button.setOnMouseClicked(e->{
i=0;
for(i=0;i<list.size();i+=2)
{
if(a[list.get(i)][list.get(i+1)]==t)
{
Rectangle r1 = new Rectangle(30,30);
r1.setFill(Color.RED);
pane.add(r1,list.get(i+1),list.get(i));
}
}
t++;
});

}

static void Tromino(int tr, int tc, int dr, int dc, int size)
{
if (size == 1)
return;

int t = c++;
int s = size/2;

//左上角
if (dr < tr + s&&dc < tc + s)
Tromino(tr, tc, dr, dc, s);
else
{
a[tr + s - 1][tc + s - 1] = t;
list.add(tr+s-1);
list.add(tc+s-1);
Tromino(tr, tc, tr + s - 1, tc + s - 1, s);
}

//右上角
if (dr < tr + s&&dc >= tc + s)
Tromino(tr, tc + s, dr, dc, s);
else
{
a[tr + s - 1][tc + s] = t;
list.add(tr+s-1);
list.add(tc+s);
Tromino(tr, tc + s, tr + s - 1, tc + s, s);
}

//左下角
if (dr >= tr + s&&dc < tc + s)
Tromino(tr + s, tc, dr, dc, s);
else
{
a[tr + s][tc + s - 1] = t;
list.add(tr+s);
list.add(tc+s-1);
Tromino(tr + s, tc, tr + s, tc + s - 1, s);
}

//右下角
if (dr >= tr + s&&dc >= tc + s)
Tromino(tr + s, tc + s, dr, dc, s);
else
{
a[tr + s][tc + s] = t;
list.add(tr+s);
list.add(tc+s);
Tromino(tr + s, tc + s, tr + s, tc + s, s);
}
}

private HBox getHBox()
{
HBox hbox= new HBox(10);
hbox.setPadding(new Insets(15,15,15,15));
hbox.setStyle("-fx-background-color:gold");
hbox.getChildren().add(new Label("輸入行數"));
hbox.getChildren().add(ktf);
hbox.getChildren().add(new Label("輸入空位置行數"));
hbox.getChildren().add(xtf);
hbox.getChildren().add(new Label("輸入空位置列數"));
hbox.getChildren().add(ytf);
hbox.getChildren().add(qrbutton);
return hbox;
}
}

閱讀全文

與javafx繪制棋盤相關的資料

熱點內容
win10回收站文件找回 瀏覽:750
手機數據如何恢復到最新的 瀏覽:153
丹青文件管理系統激活 瀏覽:604
迷你編程第一關卡住怎麼辦 瀏覽:658
少兒編程編程貓和猿編程哪個好 瀏覽:318
windows更新完後哪些文件可以刪 瀏覽:107
華為鎖屏密碼忘記 瀏覽:845
抖音平台數據從哪裡看 瀏覽:820
打開文件夾出錯 瀏覽:825
如何清除蘋果app的文件和數據 瀏覽:515
如何打開crv文件 瀏覽:41
md文件夾在win7不能打開 瀏覽:668
頌拓手錶運動數據如何導入微信 瀏覽:654
什麼網站信訪最有效 瀏覽:396
魅藍e2升級安卓70 瀏覽:438
黃石響應式網站建設多少錢 瀏覽:410
怎麼把excel工作簿放到一個文件夾 瀏覽:949
wifi網路延時大怎麼處理 瀏覽:345
雲辦公的原理是把傳統文件放哪裡 瀏覽:113
不屬於群防群治隊伍數據項有哪些 瀏覽:404

友情鏈接