質問編集履歴

1

冷静になれば自分でもわかるような点がありましたので変更しましが以前windowは真っ白のままでした。加えてcombinationsクラスのソースコードと概要を加えておきました。

2017/09/27 15:10

投稿

tarutarupop
tarutarupop

スコア20

test CHANGED
@@ -1 +1 @@
1
- ムプログラムの実行につ
1
+ JFrameにコンポネントが追加されず真っ白状態となる
test CHANGED
@@ -1,9 +1,11 @@
1
- 四目並べを行うプログラムを作ったのですがうまく起動してくれません。箇所やそ他改善点のご指摘お願いします。
1
+ 四目並べを行うプログラムを作ったのですがうまく起動してくれません。具体的にはしくコンポーネントが追加されていないようで真っ白になってしまします。コンポーネントを追加する順序にミスがあるでしょうか...?自力では解決しなかったためご指摘のほどお願いします。また、ほかの点で改善点があれば容赦なくお願いします。
2
2
 
3
3
  ```Java
4
4
 
5
5
  import java.awt.Color;
6
6
 
7
+ import java.awt.Dimension;
8
+
7
9
  import java.awt.GridLayout;
8
10
 
9
11
  import java.awt.event.ActionEvent;
@@ -32,7 +34,7 @@
32
34
 
33
35
  private JPanel backpane;
34
36
 
35
- private JLabel tellturn;
37
+ private JLabel telllimit;
36
38
 
37
39
  private JLabel tellcolor;
38
40
 
@@ -60,12 +62,12 @@
60
62
 
61
63
  jf.setVisible(true);
62
64
 
65
+
66
+
63
67
  }
64
68
 
65
69
  public GameScreen(int n){
66
70
 
67
-
68
-
69
71
  /*
70
72
 
71
73
  * 画面生成
@@ -76,7 +78,7 @@
76
78
 
77
79
  super();
78
80
 
79
- //backpane.setPreferredSize(new Dimension(930,700));
81
+ this.setPreferredSize(new Dimension(930,700));
80
82
 
81
83
  this.setLayout(null);
82
84
 
@@ -88,7 +90,7 @@
88
90
 
89
91
  tellcolor = new JLabel();
90
92
 
91
- tellcolor.setBackground(Color.red);
93
+ tellcolor.setBackground(getPlayer_c());
92
94
 
93
95
  tellcolor.setBounds(0, 0, 200, 200);
94
96
 
@@ -98,19 +100,17 @@
98
100
 
99
101
  //残り時間表示のためのパネル
100
102
 
101
- tellturn = new JLabel();
103
+ telllimit = new JLabel();
102
-
104
+
103
- tellturn.setBorder(new BevelBorder(BevelBorder.RAISED, Color.white, Color.black));
105
+ telllimit.setBorder(new BevelBorder(BevelBorder.RAISED, Color.white, Color.black));
104
-
106
+
105
- tellturn.setBounds(200, 0, 200, 430);
107
+ telllimit.setBounds(200, 0, 200, 430);
106
-
108
+
107
- tellturn.setForeground(Color.WHITE);
109
+ telllimit.setForeground(Color.WHITE);
108
-
109
- String text = getLabeltext();
110
+
110
-
111
- tellturn.setText(text);
111
+
112
-
112
+
113
- this.add(tellturn);
113
+ this.add(telllimit);
114
114
 
115
115
 
116
116
 
@@ -148,9 +148,9 @@
148
148
 
149
149
 
150
150
 
151
- //ゲーム開始
151
+ //ゲーム開始
152
-
152
+
153
- GameSystem gs = new GameSystem(15);
153
+ s = new GameSystem(15);
154
154
 
155
155
  }
156
156
 
@@ -168,25 +168,285 @@
168
168
 
169
169
  }
170
170
 
171
+ public void setTelllimittext(int second){
172
+
173
+ String s= "残り時間 "+ second+"秒";
174
+
175
+ telllimit.setText(s);
176
+
177
+ }
178
+
179
+
180
+
181
+ @Override
182
+
183
+ public void actionPerformed(ActionEvent e) {
184
+
185
+ String coordinate = e.getActionCommand();
186
+
187
+ for(int k=0;k<cn;k++){
188
+
189
+ for(int j = 0;j<cn;j++){
190
+
191
+ String number = k+"-"+j;
192
+
193
+ if(coordinate.equals(number)){
194
+
195
+ int order = k*20 + j+1; //入力の番号を作成
196
+
197
+ if(play_turn == true){
198
+
199
+ player_c = Color.RED;
200
+
201
+ s.addA(order);
202
+
203
+ }else{
204
+
205
+ player_c = Color.BLUE;
206
+
207
+ s.addB(order);
208
+
209
+ }
210
+
211
+ changeTurn();
212
+
213
+
214
+
215
+ if(k == 19){ //入力が底の時
216
+
217
+ processEnter(order,k,j,player_c);
218
+
219
+ }else{ //入力が上部の時
220
+
221
+ if(enter[order+20] == false){
222
+
223
+ System.out.println("無効な入力です");
224
+
225
+ }else{
226
+
227
+ processEnter(order,k,j,player_c);
228
+
229
+ }
230
+
231
+ }
232
+
233
+ s.createCombinations();
234
+
235
+ }
236
+
237
+ }
238
+
239
+ }
240
+
241
+ return;
242
+
243
+ }
244
+
245
+
246
+
171
- public String getLabeltext(){
247
+ public Color getPlayer_c(){
172
-
173
- //残り時間を表示するためのラベル
174
-
175
- String text;
176
248
 
177
249
  if(play_turn == true){
178
250
 
179
- text = "赤";
251
+ player_c = Color.RED;
180
252
 
181
253
  }else{
182
254
 
183
- text = "青";
255
+ player_c = Color.BLUE;
184
256
 
185
257
  }
186
258
 
259
+ return player_c;
260
+
261
+ }
262
+
263
+ public void changeTurn(){
264
+
265
+ if(play_turn == true){
266
+
267
+ player_c = Color.RED;
268
+
269
+ tellcolor.setBackground(Color.red);
270
+
271
+ play_turn = false;
272
+
273
+ }else{
274
+
275
+ player_c = Color.BLUE;
276
+
277
+ tellcolor.setBackground(Color.BLUE);
278
+
279
+ play_turn = true;
280
+
281
+ }
282
+
283
+ tellcolor.setBackground(getPlayer_c());
284
+
285
+ }
286
+
287
+ /* 色塗り処理 */
288
+
289
+ public void processEnter(int order,int k,int j,Color player_c){
290
+
291
+ /* 過去に入力があればtrueなければfalse */
292
+
293
+ if( enter[order] == true){
294
+
187
- String s= "残り時間 "+"秒";
295
+ System.out.println("既に入力されています");
188
-
296
+
189
- return s;
297
+ return;
298
+
299
+ }else{
300
+
301
+ y_coordinate.get(k).get(j).setBackground(player_c);
302
+
303
+ enter[order] = true;
304
+
305
+ }
306
+
307
+ }
308
+
309
+ }
310
+
311
+
312
+
313
+ ```
314
+
315
+ ```Java
316
+
317
+ import java.awt.event.ActionEvent;
318
+
319
+ import java.awt.event.ActionListener;
320
+
321
+ import java.util.ArrayList;
322
+
323
+ import java.util.Collections;
324
+
325
+ import java.util.Iterator;
326
+
327
+ import java.util.List;
328
+
329
+
330
+
331
+ import javax.swing.Timer;
332
+
333
+
334
+
335
+ public class GameSystem implements ActionListener{
336
+
337
+ private Timer timer;
338
+
339
+ private int sec;
340
+
341
+ private Combinations<Integer> c ;
342
+
343
+ private GameScreen gs ;
344
+
345
+ private Screen sc;
346
+
347
+ private ArrayList<Integer> number_a = new ArrayList<Integer>(); //player1の入力
348
+
349
+ private ArrayList<Integer> number_b = new ArrayList<Integer>(); //player2の入力
350
+
351
+ private boolean finish = false;
352
+
353
+
354
+
355
+
356
+
357
+ public GameSystem(int limit){
358
+
359
+ sec = 0;
360
+
361
+ timer = new Timer(limit*1000,this);
362
+
363
+ timer.start();
364
+
365
+ }
366
+
367
+
368
+
369
+ public void createCombinations(){
370
+
371
+ if(GameScreen.play_turn == !true){
372
+
373
+ if(number_a.size() > 3){
374
+
375
+ c = new Combinations<Integer>(number_a,4);
376
+
377
+ //con = c.count(number_a.size(),4);
378
+
379
+ judgeWinner();
380
+
381
+ }else{
382
+
383
+ return;
384
+
385
+ }
386
+
387
+ }else{
388
+
389
+ if(number_b.size() > 3){
390
+
391
+ c = new Combinations<Integer>(number_b,4);
392
+
393
+ //con = c.count(number_b.size(),4);
394
+
395
+ judgeWinner();
396
+
397
+ }else{
398
+
399
+ return;
400
+
401
+ }
402
+
403
+ }
404
+
405
+
406
+
407
+ }
408
+
409
+ /* 勝者判定 */
410
+
411
+ public void judgeWinner(){
412
+
413
+ Iterator<List<Integer>> i = c.iterator();
414
+
415
+ while(i.hasNext()){
416
+
417
+ List<Integer> t = i.next();
418
+
419
+ Collections.sort(t); //小さい順に並び替え
420
+
421
+ Collections.reverse(t); //大きい順へ
422
+
423
+ if(t.get(0)-t.get(3)==3 && t.get(0)-t.get(1)==1 ||
424
+
425
+ t.get(0)-t.get(1)==20 && t.get(1)-t.get(2)==20 && t.get(2)-t.get(3)==20 ||
426
+
427
+ t.get(0)-t.get(1)==21 && t.get(1)-t.get(2)==21 && t.get(2)-t.get(3)==21 ||
428
+
429
+ t.get(0)-t.get(1)==19 && t.get(1)-t.get(2)==19 && t.get(2)-t.get(3)==19 ){
430
+
431
+ finish = true;
432
+
433
+ System.out.println("ゲーム終了");
434
+
435
+ }
436
+
437
+ }
438
+
439
+ }
440
+
441
+ public void addA(int n){
442
+
443
+ number_a.add(n);
444
+
445
+ }
446
+
447
+ public void addB(int n){
448
+
449
+ number_b.add(n);
190
450
 
191
451
  }
192
452
 
@@ -196,290 +456,270 @@
196
456
 
197
457
  public void actionPerformed(ActionEvent e) {
198
458
 
199
- String coordinate = e.getActionCommand();
200
-
201
- for(int k=0;k<cn;k++){
202
-
203
- for(int j = 0;j<cn;j++){
204
-
205
- String number = k+"-"+j;
206
-
207
- if(coordinate.equals(number)){
208
-
209
- int order = k*20 + j+1; //入力の番号を作成
210
-
211
- if(play_turn == true){
459
+ if(finish == true){
212
-
213
- player_c = Color.RED;
460
+
214
-
215
- s.addA(order);
216
-
217
- }else{
218
-
219
- player_c = Color.BLUE;
220
-
221
- s.addB(order);
222
-
223
- }
224
-
225
- changeTurn();
461
+ timer.stop();
226
-
227
-
228
-
229
- if(k == 19){ //入力が底の時
230
-
231
- processEnter(order,k,j,player_c);
232
-
233
- }else{ //入力が上部の時
234
-
235
- if(enter[order+20] == false){
236
-
237
- System.out.println("無効な入力です");
238
-
239
- }else{
240
-
241
- processEnter(order,k,j,player_c);
242
-
243
- }
244
-
245
- }
246
-
247
- s.createCombinations();
248
-
249
- }
250
-
251
- }
252
462
 
253
463
  }
254
464
 
465
+ if (sec == 15){
466
+
467
+ sec = 0;
468
+
469
+ gs.changeTurn();
470
+
471
+ }else{
472
+
473
+ gs.setTelllimittext(sec);
474
+
255
- return;
475
+ sec++;
256
-
476
+
477
+
478
+
257
- }
479
+ }
258
-
259
- public void changeTurn(){
260
-
261
- if(play_turn == true){
262
-
263
- player_c = Color.RED;
264
-
265
- //s.addA(order);
266
-
267
- play_turn = false;
268
-
269
- }else{
270
-
271
- player_c = Color.BLUE;
272
-
273
- //s.addB(order);
274
-
275
- play_turn = true;
276
-
277
- }
278
-
279
- tellturn.setText(getLabeltext());
280
-
281
- }
282
-
283
- /* 色塗り処理 */
284
-
285
- public void processEnter(int order,int k,int j,Color player_c){
286
-
287
- /* 過去に入力があればtrueなければfalse */
288
-
289
- if( enter[order] == true){
290
-
291
- System.out.println("既に入力されています");
292
-
293
- return;
294
-
295
- }else{
296
-
297
- y_coordinate.get(k).get(j).setBackground(player_c);
298
-
299
- enter[order] = true;
300
-
301
- }
302
480
 
303
481
  }
304
482
 
305
483
  }
306
484
 
307
-
308
-
309
485
  ```
310
486
 
311
487
  ```Java
312
488
 
313
- import java.awt.event.ActionEvent;
314
-
315
- import java.awt.event.ActionListener;
489
+ /*http://kuidaored.hatenablog.com/entry/20101120/1290268944様より引用
490
+
491
+ *組み合わせをすべて列挙するプログラム
492
+
493
+ */
316
494
 
317
495
  import java.util.ArrayList;
318
496
 
319
- import java.util.Collections;
497
+ import java.util.Arrays;
320
498
 
321
499
  import java.util.Iterator;
322
500
 
323
501
  import java.util.List;
324
502
 
325
-
326
-
327
- import javax.swing.Timer;
328
-
329
-
330
-
331
- public class GameSystem implements ActionListener{
332
-
333
- private Timer timer;
334
-
335
- private int sec;
336
-
337
- private Combinations<Integer> c ;
338
-
339
- private GameScreen gs ;
340
-
341
- private Screen sc;
342
-
343
- //private int counttime;
344
-
345
- private ArrayList<Integer> number_a = new ArrayList<Integer>(); //player1の入力
346
-
347
- private ArrayList<Integer> number_b = new ArrayList<Integer>(); //player2の入力
348
-
349
- private boolean finish = false;
350
-
351
-
352
-
353
-
354
-
355
- public GameSystem(int limit){
356
-
357
- sec = 0;
358
-
359
- timer = new Timer(limit*1000,this);
360
-
361
- timer.start();
362
-
363
- }
364
-
365
-
366
-
367
- public void createCombinations(){
368
-
369
- if(GameScreen.play_turn == !true){
370
-
371
- if(number_a.size() > 3){
372
-
373
- c = new Combinations<Integer>(number_a,4);
374
-
375
- //con = c.count(number_a.size(),4);
376
-
377
- judgeWinner();
378
-
379
- }else{
380
-
381
- return;
382
-
383
- }
384
-
385
- }else{
386
-
387
- if(number_b.size() > 3){
388
-
389
- c = new Combinations<Integer>(number_b,4);
390
-
391
- //con = c.count(number_b.size(),4);
392
-
393
- judgeWinner();
394
-
395
- }else{
396
-
397
- return;
398
-
399
- }
400
-
401
- }
402
-
403
-
404
-
405
- }
406
-
407
- /* 勝者判定 */
408
-
409
- public void judgeWinner(){
410
-
411
- Iterator<List<Integer>> i = c.iterator();
412
-
413
- while(i.hasNext()){
414
-
415
- List<Integer> t = i.next();
416
-
417
- Collections.sort(t); //小さい順に並び替え
418
-
419
- Collections.reverse(t); //大きい順へ
420
-
421
- if(t.get(0)-t.get(3)==3 && t.get(0)-t.get(1)==1 ||
422
-
423
- t.get(0)-t.get(1)==20 && t.get(1)-t.get(2)==20 && t.get(2)-t.get(3)==20 ||
424
-
425
- t.get(0)-t.get(1)==21 && t.get(1)-t.get(2)==21 && t.get(2)-t.get(3)==21 ||
426
-
427
- t.get(0)-t.get(1)==19 && t.get(1)-t.get(2)==19 && t.get(2)-t.get(3)==19 ){
428
-
429
-
430
-
431
- finish = true;
432
-
433
- System.out.println("ゲーム終了");
503
+ public class Combinations<T> implements Iterator{
504
+
505
+
506
+
507
+ private List<List<T>> combinations;
508
+
509
+ private List<T> list;
510
+
511
+ private int[] index;
512
+
513
+ private boolean[] visited;
514
+
515
+ private int r;
516
+
517
+ private boolean overHalf;
518
+
519
+ private Iterator<List<T>> iterator;
520
+
521
+
522
+
523
+ public Combinations(List<T> l,int r){
524
+
525
+ if(l.size() < 1 || r < 1 || l.size() < r){
526
+
527
+ throw new IllegalArgumentException();
528
+
529
+ }
530
+
531
+
532
+
533
+ this.combinations = new ArrayList<List<T>>();
534
+
535
+ this.list = l;
536
+
537
+ this.r = r;
538
+
539
+ if(this.r == l.size()){
540
+
541
+ this.combinations.add(list);
542
+
543
+ }
544
+
545
+ else{
546
+
547
+ if(this.r > list.size() / 2){
548
+
549
+ this.r = list.size() - this.r;
550
+
551
+ this.overHalf = true;
434
552
 
435
553
  }
436
554
 
437
- }
438
-
439
- }
440
-
441
- public void addA(int n){
442
-
443
- number_a.add(n);
444
-
445
- }
446
-
447
- public void addB(int n){
448
-
449
- number_b.add(n);
450
-
451
- }
452
-
453
-
454
-
455
- @Override
456
-
457
- public void actionPerformed(ActionEvent e) {
458
-
459
- if(finish == true){
460
-
461
- timer.stop();
462
-
463
- e.setSource(this);
464
-
465
- sc.actionPerformed(e);
466
-
467
- }
468
-
469
- if (sec == 15){
470
-
471
- sec = 0;
472
-
473
- gs.changeTurn();
474
-
475
- }else{
476
-
477
- sec++;
478
-
479
- }
480
-
481
- }
555
+
556
+
557
+ this.index = new int[this.r];
558
+
559
+ this.visited = new boolean[list.size()];
560
+
561
+ this.compute(0);
562
+
563
+ }
564
+
565
+
566
+
567
+ this.iterator = this.combinations.iterator();
568
+
569
+ }
570
+
571
+
572
+
573
+ public Combinations(T[] array, int r){
574
+
575
+ this(Arrays.asList(array),r);
576
+
577
+ }
578
+
579
+
580
+
581
+ private void compute(int n){
582
+
583
+ if(n == this.r){
584
+
585
+ List<T> combination = new ArrayList<T>();
586
+
587
+ if(overHalf){
588
+
589
+ for(int i = 0;i < this.list.size();i++){
590
+
591
+ boolean skip = false;
592
+
593
+ for(int j = 0;j < this.index.length;j++){
594
+
595
+ if(i == this.index[j]){
596
+
597
+ skip = true;
598
+
599
+ }
600
+
601
+ }
602
+
603
+ if(skip){
604
+
605
+ continue;
606
+
607
+ }
608
+
609
+ combination.add(list.get(i));
610
+
611
+ }
612
+
613
+ }
614
+
615
+ else{
616
+
617
+ for(int i = 0;i < this.index.length;i++){
618
+
619
+ combination.add(list.get(index[i]));
620
+
621
+ }
622
+
623
+ }
624
+
625
+ this.combinations.add(combination);
626
+
627
+ }
628
+
629
+ else{
630
+
631
+ for(int i = 0;i < this.list.size();i++){
632
+
633
+ if(n == 0 || !this.visited[i] && index[n - 1] < i){
634
+
635
+ this.visited[i] = true;
636
+
637
+ this.index[n] = i;
638
+
639
+ this.compute(n + 1);
640
+
641
+ this.visited[i] = false;
642
+
643
+ }
644
+
645
+ }
646
+
647
+ }
648
+
649
+ }
650
+
651
+
652
+
653
+ public List<List<T>> getCombinations(){
654
+
655
+ return this.combinations;
656
+
657
+ }
658
+
659
+
660
+
661
+ public static int count(int n, int r) throws IllegalArgumentException{
662
+
663
+ if(n < 1 || r < 1 || n < r){
664
+
665
+ throw new IllegalArgumentException();
666
+
667
+ }
668
+
669
+
670
+
671
+ int res = 1;
672
+
673
+ for(int i = n;i > r;i--){
674
+
675
+ res *= i;
676
+
677
+ }
678
+
679
+ for(int i = n - r;i > 1;i--){
680
+
681
+ res /= i;
682
+
683
+ }
684
+
685
+ return res;
686
+
687
+ }
688
+
689
+
690
+
691
+ public List<T> next(){
692
+
693
+
694
+
695
+ return this.iterator.next();
696
+
697
+ }
698
+
699
+
700
+
701
+ public boolean hasNext(){
702
+
703
+ return this.iterator.hasNext();
704
+
705
+ }
706
+
707
+
708
+
709
+ public Iterator<List<T>> iterator(){
710
+
711
+ return this.iterator;
712
+
713
+ }
714
+
715
+
716
+
717
+ public void remove(){
718
+
719
+ }
482
720
 
483
721
  }
484
722
 
723
+
724
+
485
725
  ```