导航:首页 > 编程语言 > 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绘制棋盘相关的资料

热点内容
什么app帮别人买感冒药 浏览:365
华为终端客服微信 浏览:33
文件后面加内容应该怎么取名 浏览:959
可以学会讲白话的app有哪些 浏览:332
mysql数据库navicat 浏览:246
word如何固定文本框 浏览:974
什么文件转换为pst 浏览:76
编程在哪里好找工作 浏览:177
电脑图标上有个文件 浏览:832
笔记本i5系统重装教程 浏览:472
文件如何变白 浏览:679
长城c30加装导航升级 浏览:648
压缩包会改变文件内容么 浏览:548
45度面槽怎么编程 浏览:742
苹果6原装充电器多大的 浏览:814
腾讯群文件 浏览:584
win10双击excel文件无法直接打开 浏览:152
dnf90版本剑魂右槽排名 浏览:375
mac给文件夹添加快捷方式到桌面 浏览:221
如何提高配套文件科学性 浏览:552

友情链接