private Pane populateGrid(){ Pane pane = new Pane(); pane.setPrefSize(1024, 500); Random random = new Random(); List<Point2D> usedPoints = new ArrayList<>(); for (int i = 0; i <= 9; i++) {
int randomX = random.nextInt(1024/80); int randomY = random.nextInt(500/80); Point2D p = new Point2D(randomX, randomY); //判断集合usedPoints中是否包含了p,如果包含了则重新创建,并添加到usedPoints集合中 while(usedPoints.contains(p)) { randomX = random.nextInt(1024/80); randomY = random.nextInt(500/80); p = new Point2D(randomX,randomY); } usedPoints.add(p); //此类是自建类,用于创建一个数字框 TileView tile = new TileView(Integer.toString(i)); tile.setTranslateX(randomX * 80); tile.setTranslateY(randomY * 80); tile.setOnMouseClicked(e->{ if(tileSequence.isEmpty()) { System.out.println("游戏已结束"); return; } TileView correctTile = tileSequence.remove(0); if(tile == correctTile) { tile.show(); } else { tileSequence.clear(); System.out.println("失败:游戏结束"); } });