回答編集履歴

1

取り下げます

2020/04/22 07:19

投稿

TN8001
TN8001

スコア9434

test CHANGED
@@ -1,177 +1,3 @@
1
- `buttons`でforを回のが、かえってわかりにくのかもしれません。
1
+ すいません、取り下げます
2
2
 
3
- 2次元配列のループのように、`listX` `llistY`で2重のforのほうがなじみがあるでしょうか?
4
-
5
-
6
-
7
- `button16`まで作るのがだるいので、生成もループにさせていただきました。
8
-
9
- 元に戻す必要がなければ`copyX`を作らずに、直接`listX`を`Collections.shuffle`すればいいです。
10
-
11
-
12
-
13
- ```Java
14
-
15
- import java.util.ArrayList;
16
-
17
- import java.util.Arrays;
3
+ これでは明らかにシャッフルとは言えませんね。。。
18
-
19
- import java.util.Collections;
20
-
21
- import java.util.List;
22
-
23
-
24
-
25
- import javafx.application.Application;
26
-
27
- import javafx.scene.Scene;
28
-
29
- import javafx.scene.control.Button;
30
-
31
- import javafx.scene.layout.AnchorPane;
32
-
33
- import javafx.stage.Stage;
34
-
35
-
36
-
37
-
38
-
39
- public class App extends Application {
40
-
41
- private final Button normalButton = new Button("normal");
42
-
43
- private final Button shuffleButton = new Button("shuffle");
44
-
45
- private final Button[] buttons = new Button[16];
46
-
47
-
48
-
49
- private final List<Integer> listX = Arrays.asList(20, 80, 140, 200);
50
-
51
- private final List<Integer> listY = Arrays.asList(270, 330, 390, 450);
52
-
53
-
54
-
55
- public static void main(String[] args) {
56
-
57
- launch(args);
58
-
59
- }
60
-
61
-
62
-
63
- @Override public void start(Stage stage) {
64
-
65
- AnchorPane pane = new AnchorPane();
66
-
67
-
68
-
69
- normalButton.setLayoutX(90);
70
-
71
- normalButton.setLayoutY(150);
72
-
73
- normalButton.setOnAction(e -> normal());
74
-
75
- pane.getChildren().add(normalButton);
76
-
77
-
78
-
79
- shuffleButton.setLayoutX(90);
80
-
81
- shuffleButton.setLayoutY(200);
82
-
83
- shuffleButton.setOnAction(e -> shuffle());
84
-
85
- pane.getChildren().add(shuffleButton);
86
-
87
-
88
-
89
- for (int i = 0; i < buttons.length; i++) {
90
-
91
- Button b = new Button("" + (i + 1));
92
-
93
- b.setOnAction(e -> System.out.println("button" + ((Button) e.getSource()).getText()));
94
-
95
- buttons[i] = b;
96
-
97
- }
98
-
99
- normal();
100
-
101
- pane.getChildren().addAll(buttons);
102
-
103
-
104
-
105
- Scene scene = new Scene(pane, 260, 500);
106
-
107
- stage.setScene(scene);
108
-
109
- stage.show();
110
-
111
- }
112
-
113
-
114
-
115
- private void normal() {
116
-
117
- int index = 0;
118
-
119
- for (int y : listY) {
120
-
121
- for (int x : listX) {
122
-
123
- buttons[index].setLayoutX(x);
124
-
125
- buttons[index].setLayoutY(y);
126
-
127
- index++;
128
-
129
- }
130
-
131
- }
132
-
133
- }
134
-
135
-
136
-
137
- private void shuffle() {
138
-
139
- List<Integer> copyX = new ArrayList<>(listX);
140
-
141
- List<Integer> copyY = new ArrayList<>(listY);
142
-
143
- Collections.shuffle(copyX);
144
-
145
- Collections.shuffle(copyY);
146
-
147
-
148
-
149
- int index = 0;
150
-
151
- for (int y : copyY) {
152
-
153
- for (int x : copyX) {
154
-
155
- buttons[index].setLayoutX(x);
156
-
157
- buttons[index].setLayoutY(y);
158
-
159
- index++;
160
-
161
- }
162
-
163
- }
164
-
165
- }
166
-
167
- }
168
-
169
- ```
170
-
171
-
172
-
173
- ---
174
-
175
-
176
-
177
- なにを`shuffle`するかでいろいろありますね^^;