質問編集履歴

3

解決策を思いついたので記入。現在、修正作業中。

2018/03/20 23:21

投稿

hilty8
hilty8

スコア15

test CHANGED
File without changes
test CHANGED
@@ -58,6 +58,24 @@
58
58
 
59
59
 
60
60
 
61
+ 追記2 問題箇所の予想(対処中) (18/03/21 - 08:10)
62
+
63
+ ---
64
+
65
+ MVCにおけるViewを担当している、マス目の処理を担うSquareクラス(extends StackPane)の処理に関して、
66
+
67
+ 前のターンにBoardLatticeインスタンスに追加したSquareインスタンスを更新するのではなく、
68
+
69
+ その上に新たなSquareインスタンスを追加していることが原因だと考えています。
70
+
71
+ そのため、BoardLatticeクラスの描画仕様を変更するつもりでいます。
72
+
73
+ 描画の方法に関して助言頂ければ幸いです。
74
+
75
+ 宜しくお願い致します。
76
+
77
+
78
+
61
79
  ```java
62
80
 
63
81
  public class Main extends Application {

2

主に、コードの補足、ModelであるLogManager、描画のメソッド、Controllerクラスの設定したBind、など追記しました。

2018/03/20 23:21

投稿

hilty8
hilty8

スコア15

test CHANGED
File without changes
test CHANGED
@@ -30,12 +30,34 @@
30
30
 
31
31
 
32
32
 
33
+
34
+
35
+
36
+
33
37
  わかりにくいところなどは指摘していただければ、なるべく早く追記します。
34
38
 
35
39
  宜しくお願い致します。
36
40
 
37
41
 
38
42
 
43
+ 追記(18/03/21 - 00:20)
44
+
45
+ ---
46
+
47
+ ・PieceTypeというのは、個人的に定義しているenumクラスです。コマなし、コマA~コマC、の計4つを持ちます。
48
+
49
+ ・盤面の描画の際、本当はint[][] ではなくPieceType[][] を用いていたので、実際のコードに沿った形に修正しました。
50
+
51
+ ・先ほどまではGameBoardクラスと表記していましたが、コードではBoardLatticeクラスという名前で使用していたので、実際のコードに沿って名前を修正しました。
52
+
53
+ ・MVCのModel部分であるLogManagerクラスの挙動は、標準出力を用いて1つずつ確認したので、このクラスにエラーは無いと判断しています。
54
+
55
+ 見直しをしたので、syntaxErrorは無いと思いますが、ミスがあれば即時対応します。
56
+
57
+ 宜しくお願い致します。
58
+
59
+
60
+
39
61
  ```java
40
62
 
41
63
  public class Main extends Application {
@@ -50,15 +72,11 @@
50
72
 
51
73
  root.getChildren().add(controller.gameBoard);
52
74
 
53
- (以下略)
75
+ (以下はStageやSceneに追加しているだけなので省略)
54
-
76
+
55
- }
77
+ }
56
-
78
+
57
- ```
79
+ ```
58
-
59
-
60
-
61
-
62
80
 
63
81
 
64
82
 
@@ -68,15 +86,27 @@
68
86
 
69
87
  public class BoardLattice extends GridPane {
70
88
 
89
+ private Boolean turnPlayer1 = true;
90
+
91
+ public IntegerProperty propertyRow = new SimpleIntegerProperty(-1);
92
+
93
+ public IntegerProperty propertyCol = new SimpleIntegerProperty(-1);
94
+
71
95
  Square[][] squares;
72
96
 
97
+
98
+
99
+ public BoardLattice(PieceType[][] boardStatus, Boolean argsTurnPlayer1){
100
+
73
- public GameBoard(int[][] status){
101
+ rendering(boardStatus, argsTurnPlayer1);
74
-
75
- rendering();
102
+
76
-
77
- }
103
+ }
78
-
104
+
105
+
106
+
79
- public void rendering(int[][] status){
107
+ public void rendering(PieceType[][] boardStatus, Boolean argsTurnPlayer1){
108
+
109
+ turnPlayer1 = argsTurnPlayer1;
80
110
 
81
111
  //描画の処理
82
112
 
@@ -86,12 +116,42 @@
86
116
 
87
117
  for(int j=0; j<5; j++){
88
118
 
89
- // 前回のターンの結果を消去した後、新たに描画処理を実行
119
+ // 前回のターンの結果を消去した
90
120
 
91
121
  super.getChildren().remove(squares[i][j]);
92
122
 
123
+ // ImageViewクラスを継承したPieceクラスのインスタンスを生成
124
+
125
+ Piece piece = new Piece(boardStatus[i][j]);
126
+
127
+ squares[i][j].getChildren().add(piece);
128
+
93
129
  super.add(squares[i][j], i, j);
94
130
 
131
+
132
+
133
+ int row = i;
134
+
135
+ int col = j;
136
+
137
+ squares[i][j].setOnMouseClicked(event -> {
138
+
139
+ propertyRow.setValue(row);
140
+
141
+ propertyCol.setValue(col);
142
+
143
+ };
144
+
145
+ // 手番ではないPlayerの駒が載ったsquares[i][j]をクリックしてもEventが発生しないように設定
146
+
147
+ switch(boardStatus[i][j]){
148
+
149
+ // 条件分岐のみ省略
150
+
151
+ squares[i][j].setDisable(true);
152
+
153
+ }
154
+
95
155
  }
96
156
 
97
157
  }
@@ -148,15 +208,35 @@
148
208
 
149
209
  public class LogManager {
150
210
 
211
+ public Boolean turnPlayer1;
212
+
213
+ // 選択した駒の行、列の番号
214
+
215
+ public NumberBinding bindRow;
216
+
217
+ public NumberBinding bindCol;
218
+
151
219
  // 駒の移動方向を指すStringProperty
152
220
 
153
221
  public StringProperty direction = new SimpleStringProperty("");
154
222
 
223
+
224
+
155
- public int[][] tmpLog;
225
+ public int turn;
226
+
227
+ public PieceType[][][] mainLog = new PieceType[256][5][5]; // 256手番もあればゲームが終わる想定
228
+
229
+ public PieceType[][] tmpLog = new PieceType[5][5];
230
+
231
+
156
232
 
157
233
  public LogManager(){
158
234
 
235
+ turn = 1;
236
+
237
+ turnPlayer1 = true;
238
+
159
- tmpLog = new int[5][5];//細かい数値は省略します
239
+ initializeMainLog();
160
240
 
161
241
  }
162
242
 
@@ -166,11 +246,63 @@
166
246
 
167
247
  }
168
248
 
249
+ // 以下の移動の処理に関しては期待通りの結果が出ているため、各メソッドの詳細は省略しています。
250
+
169
251
  public void moveMethod(){
170
252
 
171
- // tmpLogを更新
253
+ // tmpLogを更新
254
+
172
-
255
+ setTmpLog();
256
+
257
+ //NumberBinding, StringPropertyの値を用いて移動処理を実行する
258
+
259
+ whichDirection(direction.getValue());
260
+
261
+ // 実際の移動処理
262
+
263
+ movePiece(bindRow.intValue(), bindCol.intValue());
264
+
265
+ // 処理結果を格納したtmpLogの値をMainLogに書き込み
266
+
267
+ updateMainLog();
268
+
173
- }
269
+ }
270
+
271
+ private void setTmpLog(){
272
+
273
+ // mainLog[turn]をtmpLogにコピー(省略)
274
+
275
+ }
276
+
277
+ private void whichDirection(String str){
278
+
279
+ // 移動処理に用いるint型の変数を設定(省略)
280
+
281
+ }
282
+
283
+ private void updateMainLog(){
284
+
285
+ // ターン数+1
286
+
287
+ turn += 1;
288
+
289
+ // 処理結果を格納したtmpLogの値をMainLogに書き込み(省略)
290
+
291
+ // 手番の入れ替え
292
+
293
+ turnPlayer1 = !turnPlayer1;
294
+
295
+ }
296
+
297
+
298
+
299
+ private void initializeMainLog(){
300
+
301
+ // MainLog[0], MainLog[1]の2つにおいて、初期状態のコマ配置を指定
302
+
303
+ }
304
+
305
+
174
306
 
175
307
  }
176
308
 
@@ -192,14 +324,20 @@
192
324
 
193
325
 
194
326
 
195
-
196
-
197
327
  public Controller() throws IOException {
198
328
 
329
+ logManager = new LogManager();
330
+
199
- boardLattice = new BoardLattice(int[][] status);
331
+ boardLattice = new BoardLattice(logMangager.getLog());
200
332
 
201
333
  directionButton = FXMLLoader.load(getClass().getResource("DirectionButton.fxml"));
202
334
 
335
+ // Binding設定1 - 選択した駒を示す情報のBinding
336
+
337
+ logManager.bindRow = Bindings.add(boardLattice.propertyRow, 0);
338
+
339
+ logManager.bindCol = Bindings.add(boardLattice.propertyCol, 0);
340
+
203
341
  // 移動ボタンのコントローラクラス、DirectionButtonControllerのStringPropertyと、logManagerのStringPropertyをBind
204
342
 
205
343
  logManager.direction.bind(DirectionButtonController.direction);
@@ -208,13 +346,13 @@
208
346
 
209
347
  logManager.direction.addListener( (ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
210
348
 
211
- // logManager内のtmpLog更新
349
+ // logManager内の移動処理メソッド実行
212
350
 
213
351
  logManager.moveMethod();
214
352
 
215
353
  // boardLatticeの再描画
216
354
 
217
- boardLattice.rendering(logManager.getLog());
355
+ boardLattice.rendering(logManager.getLog(), logManager.turnPlayer1);
218
356
 
219
357
  }
220
358
 
@@ -230,12 +368,14 @@
230
368
 
231
369
  public class Square extends StackPane {
232
370
 
233
- public Square(int num){
371
+ public Square(){
372
+
234
-
373
+ super.setPrefSize(70,70);
374
+
235
- // 引数に従ってStackPaneに駒画像追加
375
+ // MouseOver,MouseExitのEventに従って背景変化(省略)
236
-
376
+
237
- }
377
+ }
238
-
378
+
239
- }
379
+ }
240
-
380
+
241
- ```
381
+ ```

1

以下の説明を補足。1、MVCの役割を担当するクラス群 2、インスタンスに追加しているChangeListener 3、ChangeListenerから再描画までの処理

2018/03/20 15:34

投稿

hilty8
hilty8

スコア15

test CHANGED
File without changes
test CHANGED
@@ -46,9 +46,9 @@
46
46
 
47
47
  Controller controller = new Controller();// MVCモデルのコントローラ
48
48
 
49
- VBox vbox = new VBox();
49
+ VBox root = new VBox();
50
-
50
+
51
- vbox.getChildren().add(controller.gameBoard);
51
+ root.getChildren().add(controller.gameBoard);
52
52
 
53
53
  (以下略)
54
54
 
@@ -58,57 +58,165 @@
58
58
 
59
59
 
60
60
 
61
+
62
+
63
+
64
+
65
+ ```java
66
+
67
+ //MVCのView部分1 - ゲーム盤
68
+
69
+ public class BoardLattice extends GridPane {
70
+
71
+ Square[][] squares;
72
+
73
+ public GameBoard(int[][] status){
74
+
75
+ rendering();
76
+
77
+ }
78
+
79
+ public void rendering(int[][] status){
80
+
81
+ //描画の処理
82
+
83
+ squares = new Square[5][5];
84
+
85
+ for(int i=0; i<5; i++){
86
+
87
+ for(int j=0; j<5; j++){
88
+
89
+ // 前回のターンの結果を消去した後、新たに描画処理を実行
90
+
91
+ super.getChildren().remove(squares[i][j]);
92
+
93
+ super.add(squares[i][j], i, j);
94
+
95
+ }
96
+
97
+ }
98
+
99
+ }
100
+
101
+ }
102
+
103
+ ```
104
+
105
+
106
+
107
+
108
+
109
+ ```java
110
+
111
+ //MVCのView部分2 - 移動ボタンのコントローラクラス
112
+
113
+ public class DirectionButtonController implements initializable {
114
+
115
+ // 移動ボタンが押されたとき、このコントローラクラスのStringPropertyの値に格納される
116
+
117
+ public static StringProperty direction = new SimpleStringProperty("have not choosen!");
118
+
119
+ // FXML内に定義したボタン(Polygonクラス)
120
+
121
+ @FXML Polygon upButton;
122
+
123
+ // 移動ボタンにEventを実装
124
+
125
+ EventHandler<MouseEvent> upMethod = (event) -> {
126
+
127
+ direction.setValue("up");
128
+
129
+ };
130
+
131
+ @Override
132
+
133
+ public void initialize(URL url, ResourceBandle rb){
134
+
135
+ upButton.setOnMouseClicked(upMethod);
136
+
137
+ }
138
+
139
+ }
140
+
141
+ ```
142
+
143
+
144
+
145
+ ```java
146
+
147
+ //MVCのModel部分
148
+
149
+ public class LogManager {
150
+
151
+ // 駒の移動方向を指すStringProperty
152
+
153
+ public StringProperty direction = new SimpleStringProperty("");
154
+
155
+ public int[][] tmpLog;
156
+
157
+ public LogManager(){
158
+
159
+ tmpLog = new int[5][5];//細かい数値は省略します
160
+
161
+ }
162
+
163
+ public int[][] getLog(){
164
+
165
+ return tmpLog;
166
+
167
+ }
168
+
169
+ public void moveMethod(){
170
+
171
+ // tmpLogを更新
172
+
173
+ }
174
+
175
+ }
176
+
177
+ ```
178
+
179
+
180
+
61
181
  ```java
62
182
 
63
183
  public class Controller {
64
184
 
185
+ //MVCモデルにおける、View: BoardLattice、Model: LogManager
186
+
65
- public GameBoard gameBoard;
187
+ public BoardLattice boardLattice;
188
+
66
-
189
+ public LogManager logManager;
190
+
191
+ public GridPane directionButton;
192
+
193
+
194
+
195
+
196
+
67
- public Controller(){
197
+ public Controller() throws IOException {
68
-
198
+
69
- gameBoard = new GameBoard(int[][] status);
199
+ boardLattice = new BoardLattice(int[][] status);
200
+
70
-
201
+ directionButton = FXMLLoader.load(getClass().getResource("DirectionButton.fxml"));
202
+
203
+ // 移動ボタンのコントローラクラス、DirectionButtonControllerのStringPropertyと、logManagerのStringPropertyをBind
204
+
205
+ logManager.direction.bind(DirectionButtonController.direction);
206
+
207
+ // LogManagerのStringPropertyが更新されたとき、移動処理を実行(ChangeListenerをラムダ式で定義)
208
+
209
+ logManager.direction.addListener( (ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
210
+
71
- // int[][] status を更新するメソッドなどありますが、省略します。
211
+ // logManager内のtmpLogを更新
212
+
72
-
213
+ logManager.moveMethod();
214
+
215
+ // boardLatticeの再描画
216
+
217
+ boardLattice.rendering(logManager.getLog());
218
+
73
- }
219
+ }
74
-
75
- }
76
-
77
- ```
78
-
79
-
80
-
81
- ```java
82
-
83
- public class GameBoard extends GridPane {
84
-
85
- Square[][] squares;
86
-
87
- public GameBoard(int[][] status){
88
-
89
- rendering();
90
-
91
- }
92
-
93
- public void rendering(int[][] status){
94
-
95
- //描画の処理
96
-
97
- squares = new Square[5][5];
98
-
99
- for(int i=0; i<5; i++){
100
-
101
- for(int j=0; j<5; j++){
102
-
103
- // 前回のターンの結果を消去した後、新たに描画処理を実行
104
-
105
- super.getChildren().remove(squares[i][j]);
106
-
107
- super.add(squares[i][j], i, j);
108
-
109
- }
110
-
111
- }
112
220
 
113
221
  }
114
222