回答編集履歴

5

説明文追加

2019/11/27 17:24

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -32,6 +32,10 @@
32
32
 
33
33
 
34
34
 
35
+ こちらで正常動作確認したソースコードです。
36
+
37
+
38
+
35
39
  ```java
36
40
 
37
41
  import java.awt.*;

4

インデントをタブ文字に変更

2019/11/27 17:24

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -46,71 +46,71 @@
46
46
 
47
47
  class Stone {
48
48
 
49
- public final static int black = 1;
50
-
51
- public final static int white = 2;
52
-
53
- public int obverse = 0;
54
-
55
-
56
-
57
- Stone() {
58
-
59
- obverse = 0;
60
-
61
- }
62
-
63
-
64
-
65
- void setObverse(int color) {
66
-
67
- if (color == black || color == white)
68
-
69
- obverse = color;
70
-
71
- else
72
-
73
- System.out.println("黒か白でなければいけません");
74
-
75
- }
76
-
77
-
78
-
79
- void doReverse(int color) {
80
-
81
- if (color == black)
82
-
83
- obverse = white;
84
-
85
- else if (color == white)
86
-
87
- obverse = black;
88
-
89
- else
90
-
91
- System.out.println("黒か白でなければいけません");
92
-
93
- }
94
-
95
-
96
-
97
- void paint(Graphics g, Point p, int rad) {
98
-
99
- if (obverse == black) {
100
-
101
- g.setColor(Color.black);
102
-
103
- g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
104
-
105
- } else if (obverse == white) {
106
-
107
- g.setColor(Color.white);
108
-
109
- g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
110
-
111
- }
112
-
113
- }
49
+ public final static int black = 1;
50
+
51
+ public final static int white = 2;
52
+
53
+ public int obverse = 0;
54
+
55
+
56
+
57
+ Stone() {
58
+
59
+ obverse = 0;
60
+
61
+ }
62
+
63
+
64
+
65
+ void setObverse(int color) {
66
+
67
+ if (color == black || color == white)
68
+
69
+ obverse = color;
70
+
71
+ else
72
+
73
+ System.out.println("黒か白でなければいけません");
74
+
75
+ }
76
+
77
+
78
+
79
+ void doReverse(int color) {
80
+
81
+ if (color == black)
82
+
83
+ obverse = white;
84
+
85
+ else if (color == white)
86
+
87
+ obverse = black;
88
+
89
+ else
90
+
91
+ System.out.println("黒か白でなければいけません");
92
+
93
+ }
94
+
95
+
96
+
97
+ void paint(Graphics g, Point p, int rad) {
98
+
99
+ if (obverse == black) {
100
+
101
+ g.setColor(Color.black);
102
+
103
+ g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
104
+
105
+ } else if (obverse == white) {
106
+
107
+ g.setColor(Color.white);
108
+
109
+ g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
110
+
111
+ }
112
+
113
+ }
114
114
 
115
115
  }
116
116
 
@@ -118,333 +118,333 @@
118
118
 
119
119
  class Board {
120
120
 
121
- Stone[][] ste = new Stone[8][8];
122
-
123
- public int num_grid_black;
124
-
125
- public int num_grid_white;
126
-
127
- private Point[] direction = new Point[8];
128
-
129
- public int[][] eval_black = new int[8][8];
130
-
131
- public int[][] eval_white = new int[8][8];
132
-
133
-
134
-
135
- Board() {
136
-
137
-
138
-
139
- for (int x = 0; x < 8; x++) {
140
-
141
- for (int y = 0; y < 8; y++) {
142
-
143
- ste[x][y] = new Stone();
144
-
145
- }
146
-
147
- }
148
-
149
- ste[3][3].setObverse(1);
150
-
151
- ste[3][4].setObverse(2);
152
-
153
- ste[4][3].setObverse(2);
154
-
155
- ste[4][4].setObverse(1);
156
-
157
-
158
-
159
- direction[0] = new Point(1, 0);
160
-
161
- direction[1] = new Point(1, 1);
162
-
163
- direction[2] = new Point(0, 1);
164
-
165
- direction[3] = new Point(-1, 1);
166
-
167
- direction[4] = new Point(-1, 0);
168
-
169
- direction[5] = new Point(-1, -1);
170
-
171
- direction[6] = new Point(0, -1);
172
-
173
- direction[7] = new Point(1, -1);
174
-
175
- }
176
-
177
-
178
-
179
- boolean isOnBoard(int x, int y) {
180
-
181
- if (x < 0 || 7 < x || y < 0 || 7 < y)
182
-
183
- return false;
184
-
185
- else
186
-
187
- return true;
188
-
189
- }
190
-
191
-
192
-
193
- ArrayList<Integer> getLine(int x, int y, Point d) {
194
-
195
- ArrayList<Integer> line = new ArrayList<Integer>();
196
-
197
- int cx = x + d.x;
198
-
199
- int cy = y + d.y;
200
-
201
- while (isOnBoard(cx, cy) && ste[cx][cy].obverse != 0) {
202
-
203
- line.add(ste[cx][cy].obverse);
204
-
205
- cx += d.x;
206
-
207
- cy += d.y;
208
-
209
- }
210
-
211
- return line;
212
-
213
- }
214
-
215
-
216
-
217
- int countReverseStone(int x, int y, int s) {
218
-
219
- if (ste[x][y].obverse != 0)
220
-
221
- return -1;
222
-
223
- int cnt = 0;
224
-
225
- for (int d = 0; d < 8; d++) {
226
-
227
- ArrayList<Integer> line = new ArrayList<Integer>();
228
-
229
- line = getLine(x, y, direction[d]);
230
-
231
- int n = 0;
232
-
233
- while (n < line.size() && line.get(n) != s)
234
-
235
- n++;
236
-
237
- if (1 <= n && n < line.size())
238
-
239
- cnt += n;
240
-
241
- }
242
-
243
- return cnt;
244
-
245
- }
246
-
247
-
248
-
249
- void setStoneAndReverse(int x, int y, int s) {
250
-
251
- for (int d = 0; d < 8; d++) {
252
-
253
- ArrayList<Integer> p = new ArrayList<Integer>();
254
-
255
- p = getLine(x, y, direction[d]);
256
-
257
- int cx = x;
258
-
259
- int cy = y;
260
-
261
- int n = 0;
262
-
263
-
264
-
265
- while (n < p.size() - 1 && p.get(n) != s && ste[cx][cy].obverse == s) {
266
-
267
- cx += direction[d].x;
268
-
269
- cy += direction[d].y;
270
-
271
- System.out.println(d);
272
-
273
- System.out.println("cx=" + cx);
274
-
275
- System.out.println("cy=" + cy);
276
-
277
- ste[cx][cy].doReverse(ste[cx][cy].obverse);
278
-
279
- n++;
280
-
281
- }
282
-
283
- }
284
-
285
- }
286
-
287
-
288
-
289
- void evaluateBoard() {
290
-
291
- num_grid_black = 0;
292
-
293
- num_grid_white = 0;
294
-
295
- for (int i = 0; i < 8; i++) {
296
-
297
- for (int j = 0; j < 8; j++) {
298
-
299
- eval_black[i][j] = countReverseStone(i, j, 1);
300
-
301
- if (eval_black[i][j] > 0)
302
-
303
- num_grid_black++;
304
-
305
- eval_white[i][j] = countReverseStone(i, j, 2);
306
-
307
- if (eval_white[i][j] > 0)
308
-
309
- num_grid_white++;
310
-
311
- }
312
-
313
- }
314
-
315
- }
316
-
317
-
318
-
319
- void printBoard() {
320
-
321
- for (int y = 0; y < 8; y++) {
322
-
323
- for (int x = 0; x < 8; x++) {
324
-
325
- System.out.printf("%2d ", ste[x][y].obverse);
326
-
327
- }
328
-
329
- System.out.println("");
330
-
331
- }
332
-
333
- }
334
-
335
-
336
-
337
- void printEval() {
338
-
339
- System.out.println("Black(1):");
340
-
341
- for (int i = 0; i < 8; i++) {
342
-
343
- for (int j = 0; j < 8; j++) {
344
-
345
- System.out.printf("%2d ", eval_black[j][i]);
346
-
347
- }
348
-
349
- System.out.println("");
350
-
351
- }
352
-
353
- System.out.println("White(2):");
354
-
355
- for (int i = 0; i < 8; i++) {
356
-
357
- for (int j = 0; j < 8; j++) {
358
-
359
- System.out.printf("%2d ", eval_white[j][i]);
360
-
361
- }
362
-
363
- System.out.println("");
364
-
365
- }
366
-
367
- }
368
-
369
-
370
-
371
- void paint(Graphics g, int unit_size) {
372
-
373
-
374
-
375
- g.setColor(Color.black);
376
-
377
- g.fillRect(0, 0, unit_size * 10, unit_size * 10);
378
-
379
-
380
-
381
- g.setColor(new Color(0, 85, 0));
382
-
383
- g.fillRect(unit_size, unit_size, unit_size * 8, unit_size * 8);
384
-
385
-
386
-
387
- g.setColor(Color.black);
388
-
389
- for (int i = 0; i < 9; i++) {
390
-
391
- g.drawLine(unit_size * (i + 1), unit_size, unit_size * (i + 1), unit_size * 9);
392
-
393
- }
394
-
395
-
396
-
397
- g.setColor(Color.black);
398
-
399
- for (int i = 0; i < 9; i++) {
400
-
401
- g.drawLine(unit_size, unit_size * (i + 1), unit_size * 9, unit_size * (i + 1));
402
-
403
- }
404
-
405
-
406
-
407
- for (int i = 0; i < 2; i++) {
408
-
409
- for (int j = 0; j < 2; j++) {
410
-
411
- g.fillRect(unit_size * (3 + 4 * i) - (unit_size / 16), unit_size * (3 + 4 * j) - (unit_size / 16),
412
-
413
- (unit_size / 8), (unit_size / 8));
414
-
415
- }
416
-
417
- }
418
-
419
-
420
-
421
- Point p = new Point();
422
-
423
- int rad = ((unit_size / 2) * 4) / 5;
424
-
425
- for (int x = 0; x < 8; x++) {
426
-
427
- for (int y = 0; y < 8; y++) {
428
-
429
- p.x = (unit_size * (x + 1) + unit_size * (x + 2)) / 2;
430
-
431
- p.y = (unit_size * (y + 1) + unit_size * (y + 2)) / 2;
432
-
433
- ste[x][y].paint(g, p, rad);
434
-
435
- }
436
-
437
- }
438
-
439
- }
440
-
441
-
442
-
443
- void setStone(int x, int y, int s) {
444
-
445
- ste[x][y].setObverse(s);
446
-
447
- }
121
+ Stone[][] ste = new Stone[8][8];
122
+
123
+ public int num_grid_black;
124
+
125
+ public int num_grid_white;
126
+
127
+ private Point[] direction = new Point[8];
128
+
129
+ public int[][] eval_black = new int[8][8];
130
+
131
+ public int[][] eval_white = new int[8][8];
132
+
133
+
134
+
135
+ Board() {
136
+
137
+
138
+
139
+ for (int x = 0; x < 8; x++) {
140
+
141
+ for (int y = 0; y < 8; y++) {
142
+
143
+ ste[x][y] = new Stone();
144
+
145
+ }
146
+
147
+ }
148
+
149
+ ste[3][3].setObverse(1);
150
+
151
+ ste[3][4].setObverse(2);
152
+
153
+ ste[4][3].setObverse(2);
154
+
155
+ ste[4][4].setObverse(1);
156
+
157
+
158
+
159
+ direction[0] = new Point(1, 0);
160
+
161
+ direction[1] = new Point(1, 1);
162
+
163
+ direction[2] = new Point(0, 1);
164
+
165
+ direction[3] = new Point(-1, 1);
166
+
167
+ direction[4] = new Point(-1, 0);
168
+
169
+ direction[5] = new Point(-1, -1);
170
+
171
+ direction[6] = new Point(0, -1);
172
+
173
+ direction[7] = new Point(1, -1);
174
+
175
+ }
176
+
177
+
178
+
179
+ boolean isOnBoard(int x, int y) {
180
+
181
+ if (x < 0 || 7 < x || y < 0 || 7 < y)
182
+
183
+ return false;
184
+
185
+ else
186
+
187
+ return true;
188
+
189
+ }
190
+
191
+
192
+
193
+ ArrayList<Integer> getLine(int x, int y, Point d) {
194
+
195
+ ArrayList<Integer> line = new ArrayList<Integer>();
196
+
197
+ int cx = x + d.x;
198
+
199
+ int cy = y + d.y;
200
+
201
+ while (isOnBoard(cx, cy) && ste[cx][cy].obverse != 0) {
202
+
203
+ line.add(ste[cx][cy].obverse);
204
+
205
+ cx += d.x;
206
+
207
+ cy += d.y;
208
+
209
+ }
210
+
211
+ return line;
212
+
213
+ }
214
+
215
+
216
+
217
+ int countReverseStone(int x, int y, int s) {
218
+
219
+ if (ste[x][y].obverse != 0)
220
+
221
+ return -1;
222
+
223
+ int cnt = 0;
224
+
225
+ for (int d = 0; d < 8; d++) {
226
+
227
+ ArrayList<Integer> line = new ArrayList<Integer>();
228
+
229
+ line = getLine(x, y, direction[d]);
230
+
231
+ int n = 0;
232
+
233
+ while (n < line.size() && line.get(n) != s)
234
+
235
+ n++;
236
+
237
+ if (1 <= n && n < line.size())
238
+
239
+ cnt += n;
240
+
241
+ }
242
+
243
+ return cnt;
244
+
245
+ }
246
+
247
+
248
+
249
+ void setStoneAndReverse(int x, int y, int s) {
250
+
251
+ for (int d = 0; d < 8; d++) {
252
+
253
+ ArrayList<Integer> p = new ArrayList<Integer>();
254
+
255
+ p = getLine(x, y, direction[d]);
256
+
257
+ int cx = x;
258
+
259
+ int cy = y;
260
+
261
+ int n = 0;
262
+
263
+
264
+
265
+ while (n < p.size() - 1 && p.get(n) != s && ste[cx][cy].obverse == s) {
266
+
267
+ cx += direction[d].x;
268
+
269
+ cy += direction[d].y;
270
+
271
+ System.out.println(d);
272
+
273
+ System.out.println("cx=" + cx);
274
+
275
+ System.out.println("cy=" + cy);
276
+
277
+ ste[cx][cy].doReverse(ste[cx][cy].obverse);
278
+
279
+ n++;
280
+
281
+ }
282
+
283
+ }
284
+
285
+ }
286
+
287
+
288
+
289
+ void evaluateBoard() {
290
+
291
+ num_grid_black = 0;
292
+
293
+ num_grid_white = 0;
294
+
295
+ for (int i = 0; i < 8; i++) {
296
+
297
+ for (int j = 0; j < 8; j++) {
298
+
299
+ eval_black[i][j] = countReverseStone(i, j, 1);
300
+
301
+ if (eval_black[i][j] > 0)
302
+
303
+ num_grid_black++;
304
+
305
+ eval_white[i][j] = countReverseStone(i, j, 2);
306
+
307
+ if (eval_white[i][j] > 0)
308
+
309
+ num_grid_white++;
310
+
311
+ }
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
319
+ void printBoard() {
320
+
321
+ for (int y = 0; y < 8; y++) {
322
+
323
+ for (int x = 0; x < 8; x++) {
324
+
325
+ System.out.printf("%2d ", ste[x][y].obverse);
326
+
327
+ }
328
+
329
+ System.out.println("");
330
+
331
+ }
332
+
333
+ }
334
+
335
+
336
+
337
+ void printEval() {
338
+
339
+ System.out.println("Black(1):");
340
+
341
+ for (int i = 0; i < 8; i++) {
342
+
343
+ for (int j = 0; j < 8; j++) {
344
+
345
+ System.out.printf("%2d ", eval_black[j][i]);
346
+
347
+ }
348
+
349
+ System.out.println("");
350
+
351
+ }
352
+
353
+ System.out.println("White(2):");
354
+
355
+ for (int i = 0; i < 8; i++) {
356
+
357
+ for (int j = 0; j < 8; j++) {
358
+
359
+ System.out.printf("%2d ", eval_white[j][i]);
360
+
361
+ }
362
+
363
+ System.out.println("");
364
+
365
+ }
366
+
367
+ }
368
+
369
+
370
+
371
+ void paint(Graphics g, int unit_size) {
372
+
373
+
374
+
375
+ g.setColor(Color.black);
376
+
377
+ g.fillRect(0, 0, unit_size * 10, unit_size * 10);
378
+
379
+
380
+
381
+ g.setColor(new Color(0, 85, 0));
382
+
383
+ g.fillRect(unit_size, unit_size, unit_size * 8, unit_size * 8);
384
+
385
+
386
+
387
+ g.setColor(Color.black);
388
+
389
+ for (int i = 0; i < 9; i++) {
390
+
391
+ g.drawLine(unit_size * (i + 1), unit_size, unit_size * (i + 1), unit_size * 9);
392
+
393
+ }
394
+
395
+
396
+
397
+ g.setColor(Color.black);
398
+
399
+ for (int i = 0; i < 9; i++) {
400
+
401
+ g.drawLine(unit_size, unit_size * (i + 1), unit_size * 9, unit_size * (i + 1));
402
+
403
+ }
404
+
405
+
406
+
407
+ for (int i = 0; i < 2; i++) {
408
+
409
+ for (int j = 0; j < 2; j++) {
410
+
411
+ g.fillRect(unit_size * (3 + 4 * i) - (unit_size / 16), unit_size * (3 + 4 * j) - (unit_size / 16),
412
+
413
+ (unit_size / 8), (unit_size / 8));
414
+
415
+ }
416
+
417
+ }
418
+
419
+
420
+
421
+ Point p = new Point();
422
+
423
+ int rad = ((unit_size / 2) * 4) / 5;
424
+
425
+ for (int x = 0; x < 8; x++) {
426
+
427
+ for (int y = 0; y < 8; y++) {
428
+
429
+ p.x = (unit_size * (x + 1) + unit_size * (x + 2)) / 2;
430
+
431
+ p.y = (unit_size * (y + 1) + unit_size * (y + 2)) / 2;
432
+
433
+ ste[x][y].paint(g, p, rad);
434
+
435
+ }
436
+
437
+ }
438
+
439
+ }
440
+
441
+
442
+
443
+ void setStone(int x, int y, int s) {
444
+
445
+ ste[x][y].setObverse(s);
446
+
447
+ }
448
448
 
449
449
  }
450
450
 
@@ -452,247 +452,247 @@
452
452
 
453
453
  public class Reversi extends JPanel {
454
454
 
455
- private final static int UNIT_SIZE = 80;
456
-
457
- private Board board = new Board();
458
-
459
-
460
-
461
- private int turn = 1;
462
-
463
-
464
-
465
- public Reversi() {
466
-
467
- setPreferredSize(new Dimension(800, 800));
468
-
469
- addMouseListener(new MouseProc());
470
-
471
- }
472
-
473
-
474
-
475
- public void paintComponent(Graphics g) {
476
-
477
- board.paint(g, UNIT_SIZE);
478
-
479
- }
480
-
481
-
482
-
483
- void changeTurn() {
484
-
485
- if (turn == 1)
486
-
487
- turn = 2;
488
-
489
- else if (turn == 2)
490
-
491
- turn = 1;
492
-
493
- }
494
-
495
-
496
-
497
- void MessageDialog(String str) {
498
-
499
- JOptionPane.showMessageDialog(this, str, "情報", JOptionPane.INFORMATION_MESSAGE);
500
-
501
- }
502
-
503
-
504
-
505
- void EndMessageDialog(int num_black, int num_white) {
506
-
507
- if (num_black > num_white) {
508
-
509
- String str = "[黒:" + num_black + ",白:" + num_white + "]で黒の勝ち";
510
-
511
- JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
512
-
513
- } else if (num_black == num_white) {
514
-
515
- String str = "[黒:" + num_black + ",白:" + num_white + "]で引き分け";
516
-
517
- JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
518
-
519
- } else {
520
-
521
- String str = "[黒:" + num_black + ",白:" + num_white + "]で白の勝ち";
522
-
523
- JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
524
-
525
- }
526
-
527
-
528
-
529
- System.exit(0);
530
-
531
- }
532
-
533
-
534
-
535
- class MouseProc extends MouseAdapter {
536
-
537
- public void mouseClicked(MouseEvent me) {
538
-
539
- Point point = me.getPoint();
540
-
541
- int btn = me.getButton();
542
-
543
- System.out.println("(" + point.x + "," + point.y + ")");
544
-
545
- board.evaluateBoard();
546
-
547
- if (turn == 1 && btn == MouseEvent.BUTTON1) {
548
-
549
- int x = point.x / UNIT_SIZE - 1;
550
-
551
- int y = point.y / UNIT_SIZE - 1;
552
-
553
- System.out.println("[" + x + "]" + "[" + y + "]");
554
-
555
- if (0 <= x && x < 8 && 0 <= y && y < 8) {
556
-
557
- if (board.eval_black[x][y] < 1) {
558
-
559
- System.out.println("そこに石を置けません");
560
-
561
- } else {
562
-
563
- board.setStone(x, y, 1);
564
-
565
- board.setStoneAndReverse(x, y, 1);
566
-
567
- repaint();
568
-
569
- board.printBoard();
570
-
571
- board.evaluateBoard();
572
-
573
- board.printEval();
574
-
575
- changeTurn();
576
-
577
- if (board.num_grid_black >= 1) {
578
-
579
- MessageDialog("白の番です");
580
-
581
- } else {
582
-
583
- changeTurn();
584
-
585
- MessageDialog("あなたはパスです");
586
-
587
- }
588
-
589
- }
590
-
591
- }
592
-
593
- } else if (turn == 2 && btn == MouseEvent.BUTTON3) {
594
-
595
- int x = point.x / UNIT_SIZE - 1;
596
-
597
- int y = point.y / UNIT_SIZE - 1;
598
-
599
- System.out.println("[" + x + "]" + "[" + y + "]");
600
-
601
- if (0 <= x && x < 8 && 0 <= y && y < 8) {
602
-
603
- if (board.eval_white[x][y] < 1) {
604
-
605
- System.out.println("そこに石を置けません");
606
-
607
- } else {
608
-
609
- board.setStone(x, y, 2);
610
-
611
- board.setStoneAndReverse(x, y, 2);
612
-
613
- repaint();
614
-
615
- board.printBoard();
616
-
617
- board.evaluateBoard();
618
-
619
- board.printEval();
620
-
621
- changeTurn();
622
-
623
- if (board.num_grid_black >= 1) {
624
-
625
- MessageDialog("黒の番です");
626
-
627
- } else {
628
-
629
- changeTurn();
630
-
631
- MessageDialog("あなたはパスです");
632
-
633
- }
634
-
635
- }
636
-
637
- }
638
-
639
- }
640
-
641
-
642
-
643
- int num_black = 0;
644
-
645
- int num_white = 0;
646
-
647
- for (int x = 0; x < 8; x++) {
648
-
649
- for (int y = 0; y < 8; y++) {
650
-
651
- if (board.ste[x][y].obverse == 1)
652
-
653
- num_black++;
654
-
655
- if (board.ste[x][y].obverse == 2)
656
-
657
- num_white++;
658
-
659
- }
660
-
661
- }
662
-
663
-
664
-
665
- if (board.num_grid_black < 1 && board.num_grid_white < 1) {
666
-
667
- EndMessageDialog(num_black, num_white);
668
-
669
- }
670
-
671
- }
672
-
673
- }
674
-
675
-
676
-
677
- public static void main(String[] args) {
678
-
679
- JFrame f = new JFrame();
680
-
681
-
682
-
683
- f.getContentPane().setLayout(new FlowLayout());
684
-
685
- f.getContentPane().add(new Reversi());
686
-
687
- f.pack();
688
-
689
- f.setResizable(false);
690
-
691
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
692
-
693
- f.setVisible(true);
694
-
695
- }
455
+ private final static int UNIT_SIZE = 80;
456
+
457
+ private Board board = new Board();
458
+
459
+
460
+
461
+ private int turn = 1;
462
+
463
+
464
+
465
+ public Reversi() {
466
+
467
+ setPreferredSize(new Dimension(800, 800));
468
+
469
+ addMouseListener(new MouseProc());
470
+
471
+ }
472
+
473
+
474
+
475
+ public void paintComponent(Graphics g) {
476
+
477
+ board.paint(g, UNIT_SIZE);
478
+
479
+ }
480
+
481
+
482
+
483
+ void changeTurn() {
484
+
485
+ if (turn == 1)
486
+
487
+ turn = 2;
488
+
489
+ else if (turn == 2)
490
+
491
+ turn = 1;
492
+
493
+ }
494
+
495
+
496
+
497
+ void MessageDialog(String str) {
498
+
499
+ JOptionPane.showMessageDialog(this, str, "情報", JOptionPane.INFORMATION_MESSAGE);
500
+
501
+ }
502
+
503
+
504
+
505
+ void EndMessageDialog(int num_black, int num_white) {
506
+
507
+ if (num_black > num_white) {
508
+
509
+ String str = "[黒:" + num_black + ",白:" + num_white + "]で黒の勝ち";
510
+
511
+ JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
512
+
513
+ } else if (num_black == num_white) {
514
+
515
+ String str = "[黒:" + num_black + ",白:" + num_white + "]で引き分け";
516
+
517
+ JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
518
+
519
+ } else {
520
+
521
+ String str = "[黒:" + num_black + ",白:" + num_white + "]で白の勝ち";
522
+
523
+ JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
524
+
525
+ }
526
+
527
+
528
+
529
+ System.exit(0);
530
+
531
+ }
532
+
533
+
534
+
535
+ class MouseProc extends MouseAdapter {
536
+
537
+ public void mouseClicked(MouseEvent me) {
538
+
539
+ Point point = me.getPoint();
540
+
541
+ int btn = me.getButton();
542
+
543
+ System.out.println("(" + point.x + "," + point.y + ")");
544
+
545
+ board.evaluateBoard();
546
+
547
+ if (turn == 1 && btn == MouseEvent.BUTTON1) {
548
+
549
+ int x = point.x / UNIT_SIZE - 1;
550
+
551
+ int y = point.y / UNIT_SIZE - 1;
552
+
553
+ System.out.println("[" + x + "]" + "[" + y + "]");
554
+
555
+ if (0 <= x && x < 8 && 0 <= y && y < 8) {
556
+
557
+ if (board.eval_black[x][y] < 1) {
558
+
559
+ System.out.println("そこに石を置けません");
560
+
561
+ } else {
562
+
563
+ board.setStone(x, y, 1);
564
+
565
+ board.setStoneAndReverse(x, y, 1);
566
+
567
+ repaint();
568
+
569
+ board.printBoard();
570
+
571
+ board.evaluateBoard();
572
+
573
+ board.printEval();
574
+
575
+ changeTurn();
576
+
577
+ if (board.num_grid_black >= 1) {
578
+
579
+ MessageDialog("白の番です");
580
+
581
+ } else {
582
+
583
+ changeTurn();
584
+
585
+ MessageDialog("あなたはパスです");
586
+
587
+ }
588
+
589
+ }
590
+
591
+ }
592
+
593
+ } else if (turn == 2 && btn == MouseEvent.BUTTON3) {
594
+
595
+ int x = point.x / UNIT_SIZE - 1;
596
+
597
+ int y = point.y / UNIT_SIZE - 1;
598
+
599
+ System.out.println("[" + x + "]" + "[" + y + "]");
600
+
601
+ if (0 <= x && x < 8 && 0 <= y && y < 8) {
602
+
603
+ if (board.eval_white[x][y] < 1) {
604
+
605
+ System.out.println("そこに石を置けません");
606
+
607
+ } else {
608
+
609
+ board.setStone(x, y, 2);
610
+
611
+ board.setStoneAndReverse(x, y, 2);
612
+
613
+ repaint();
614
+
615
+ board.printBoard();
616
+
617
+ board.evaluateBoard();
618
+
619
+ board.printEval();
620
+
621
+ changeTurn();
622
+
623
+ if (board.num_grid_black >= 1) {
624
+
625
+ MessageDialog("黒の番です");
626
+
627
+ } else {
628
+
629
+ changeTurn();
630
+
631
+ MessageDialog("あなたはパスです");
632
+
633
+ }
634
+
635
+ }
636
+
637
+ }
638
+
639
+ }
640
+
641
+
642
+
643
+ int num_black = 0;
644
+
645
+ int num_white = 0;
646
+
647
+ for (int x = 0; x < 8; x++) {
648
+
649
+ for (int y = 0; y < 8; y++) {
650
+
651
+ if (board.ste[x][y].obverse == 1)
652
+
653
+ num_black++;
654
+
655
+ if (board.ste[x][y].obverse == 2)
656
+
657
+ num_white++;
658
+
659
+ }
660
+
661
+ }
662
+
663
+
664
+
665
+ if (board.num_grid_black < 1 && board.num_grid_white < 1) {
666
+
667
+ EndMessageDialog(num_black, num_white);
668
+
669
+ }
670
+
671
+ }
672
+
673
+ }
674
+
675
+
676
+
677
+ public static void main(String[] args) {
678
+
679
+ JFrame f = new JFrame();
680
+
681
+
682
+
683
+ f.getContentPane().setLayout(new FlowLayout());
684
+
685
+ f.getContentPane().add(new Reversi());
686
+
687
+ f.pack();
688
+
689
+ f.setResizable(false);
690
+
691
+ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
692
+
693
+ f.setVisible(true);
694
+
695
+ }
696
696
 
697
697
  }
698
698
 

3

正常動作するコードを追加

2019/11/27 17:21

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -29,3 +29,671 @@
29
29
  あとは、順番に関係なく石を置けてしまう問題があります。
30
30
 
31
31
  パスする処理も変です。
32
+
33
+
34
+
35
+ ```java
36
+
37
+ import java.awt.*;
38
+
39
+ import javax.swing.*;
40
+
41
+ import java.awt.event.*;
42
+
43
+ import java.util.*;
44
+
45
+
46
+
47
+ class Stone {
48
+
49
+ public final static int black = 1;
50
+
51
+ public final static int white = 2;
52
+
53
+ public int obverse = 0;
54
+
55
+
56
+
57
+ Stone() {
58
+
59
+ obverse = 0;
60
+
61
+ }
62
+
63
+
64
+
65
+ void setObverse(int color) {
66
+
67
+ if (color == black || color == white)
68
+
69
+ obverse = color;
70
+
71
+ else
72
+
73
+ System.out.println("黒か白でなければいけません");
74
+
75
+ }
76
+
77
+
78
+
79
+ void doReverse(int color) {
80
+
81
+ if (color == black)
82
+
83
+ obverse = white;
84
+
85
+ else if (color == white)
86
+
87
+ obverse = black;
88
+
89
+ else
90
+
91
+ System.out.println("黒か白でなければいけません");
92
+
93
+ }
94
+
95
+
96
+
97
+ void paint(Graphics g, Point p, int rad) {
98
+
99
+ if (obverse == black) {
100
+
101
+ g.setColor(Color.black);
102
+
103
+ g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
104
+
105
+ } else if (obverse == white) {
106
+
107
+ g.setColor(Color.white);
108
+
109
+ g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
110
+
111
+ }
112
+
113
+ }
114
+
115
+ }
116
+
117
+
118
+
119
+ class Board {
120
+
121
+ Stone[][] ste = new Stone[8][8];
122
+
123
+ public int num_grid_black;
124
+
125
+ public int num_grid_white;
126
+
127
+ private Point[] direction = new Point[8];
128
+
129
+ public int[][] eval_black = new int[8][8];
130
+
131
+ public int[][] eval_white = new int[8][8];
132
+
133
+
134
+
135
+ Board() {
136
+
137
+
138
+
139
+ for (int x = 0; x < 8; x++) {
140
+
141
+ for (int y = 0; y < 8; y++) {
142
+
143
+ ste[x][y] = new Stone();
144
+
145
+ }
146
+
147
+ }
148
+
149
+ ste[3][3].setObverse(1);
150
+
151
+ ste[3][4].setObverse(2);
152
+
153
+ ste[4][3].setObverse(2);
154
+
155
+ ste[4][4].setObverse(1);
156
+
157
+
158
+
159
+ direction[0] = new Point(1, 0);
160
+
161
+ direction[1] = new Point(1, 1);
162
+
163
+ direction[2] = new Point(0, 1);
164
+
165
+ direction[3] = new Point(-1, 1);
166
+
167
+ direction[4] = new Point(-1, 0);
168
+
169
+ direction[5] = new Point(-1, -1);
170
+
171
+ direction[6] = new Point(0, -1);
172
+
173
+ direction[7] = new Point(1, -1);
174
+
175
+ }
176
+
177
+
178
+
179
+ boolean isOnBoard(int x, int y) {
180
+
181
+ if (x < 0 || 7 < x || y < 0 || 7 < y)
182
+
183
+ return false;
184
+
185
+ else
186
+
187
+ return true;
188
+
189
+ }
190
+
191
+
192
+
193
+ ArrayList<Integer> getLine(int x, int y, Point d) {
194
+
195
+ ArrayList<Integer> line = new ArrayList<Integer>();
196
+
197
+ int cx = x + d.x;
198
+
199
+ int cy = y + d.y;
200
+
201
+ while (isOnBoard(cx, cy) && ste[cx][cy].obverse != 0) {
202
+
203
+ line.add(ste[cx][cy].obverse);
204
+
205
+ cx += d.x;
206
+
207
+ cy += d.y;
208
+
209
+ }
210
+
211
+ return line;
212
+
213
+ }
214
+
215
+
216
+
217
+ int countReverseStone(int x, int y, int s) {
218
+
219
+ if (ste[x][y].obverse != 0)
220
+
221
+ return -1;
222
+
223
+ int cnt = 0;
224
+
225
+ for (int d = 0; d < 8; d++) {
226
+
227
+ ArrayList<Integer> line = new ArrayList<Integer>();
228
+
229
+ line = getLine(x, y, direction[d]);
230
+
231
+ int n = 0;
232
+
233
+ while (n < line.size() && line.get(n) != s)
234
+
235
+ n++;
236
+
237
+ if (1 <= n && n < line.size())
238
+
239
+ cnt += n;
240
+
241
+ }
242
+
243
+ return cnt;
244
+
245
+ }
246
+
247
+
248
+
249
+ void setStoneAndReverse(int x, int y, int s) {
250
+
251
+ for (int d = 0; d < 8; d++) {
252
+
253
+ ArrayList<Integer> p = new ArrayList<Integer>();
254
+
255
+ p = getLine(x, y, direction[d]);
256
+
257
+ int cx = x;
258
+
259
+ int cy = y;
260
+
261
+ int n = 0;
262
+
263
+
264
+
265
+ while (n < p.size() - 1 && p.get(n) != s && ste[cx][cy].obverse == s) {
266
+
267
+ cx += direction[d].x;
268
+
269
+ cy += direction[d].y;
270
+
271
+ System.out.println(d);
272
+
273
+ System.out.println("cx=" + cx);
274
+
275
+ System.out.println("cy=" + cy);
276
+
277
+ ste[cx][cy].doReverse(ste[cx][cy].obverse);
278
+
279
+ n++;
280
+
281
+ }
282
+
283
+ }
284
+
285
+ }
286
+
287
+
288
+
289
+ void evaluateBoard() {
290
+
291
+ num_grid_black = 0;
292
+
293
+ num_grid_white = 0;
294
+
295
+ for (int i = 0; i < 8; i++) {
296
+
297
+ for (int j = 0; j < 8; j++) {
298
+
299
+ eval_black[i][j] = countReverseStone(i, j, 1);
300
+
301
+ if (eval_black[i][j] > 0)
302
+
303
+ num_grid_black++;
304
+
305
+ eval_white[i][j] = countReverseStone(i, j, 2);
306
+
307
+ if (eval_white[i][j] > 0)
308
+
309
+ num_grid_white++;
310
+
311
+ }
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
319
+ void printBoard() {
320
+
321
+ for (int y = 0; y < 8; y++) {
322
+
323
+ for (int x = 0; x < 8; x++) {
324
+
325
+ System.out.printf("%2d ", ste[x][y].obverse);
326
+
327
+ }
328
+
329
+ System.out.println("");
330
+
331
+ }
332
+
333
+ }
334
+
335
+
336
+
337
+ void printEval() {
338
+
339
+ System.out.println("Black(1):");
340
+
341
+ for (int i = 0; i < 8; i++) {
342
+
343
+ for (int j = 0; j < 8; j++) {
344
+
345
+ System.out.printf("%2d ", eval_black[j][i]);
346
+
347
+ }
348
+
349
+ System.out.println("");
350
+
351
+ }
352
+
353
+ System.out.println("White(2):");
354
+
355
+ for (int i = 0; i < 8; i++) {
356
+
357
+ for (int j = 0; j < 8; j++) {
358
+
359
+ System.out.printf("%2d ", eval_white[j][i]);
360
+
361
+ }
362
+
363
+ System.out.println("");
364
+
365
+ }
366
+
367
+ }
368
+
369
+
370
+
371
+ void paint(Graphics g, int unit_size) {
372
+
373
+
374
+
375
+ g.setColor(Color.black);
376
+
377
+ g.fillRect(0, 0, unit_size * 10, unit_size * 10);
378
+
379
+
380
+
381
+ g.setColor(new Color(0, 85, 0));
382
+
383
+ g.fillRect(unit_size, unit_size, unit_size * 8, unit_size * 8);
384
+
385
+
386
+
387
+ g.setColor(Color.black);
388
+
389
+ for (int i = 0; i < 9; i++) {
390
+
391
+ g.drawLine(unit_size * (i + 1), unit_size, unit_size * (i + 1), unit_size * 9);
392
+
393
+ }
394
+
395
+
396
+
397
+ g.setColor(Color.black);
398
+
399
+ for (int i = 0; i < 9; i++) {
400
+
401
+ g.drawLine(unit_size, unit_size * (i + 1), unit_size * 9, unit_size * (i + 1));
402
+
403
+ }
404
+
405
+
406
+
407
+ for (int i = 0; i < 2; i++) {
408
+
409
+ for (int j = 0; j < 2; j++) {
410
+
411
+ g.fillRect(unit_size * (3 + 4 * i) - (unit_size / 16), unit_size * (3 + 4 * j) - (unit_size / 16),
412
+
413
+ (unit_size / 8), (unit_size / 8));
414
+
415
+ }
416
+
417
+ }
418
+
419
+
420
+
421
+ Point p = new Point();
422
+
423
+ int rad = ((unit_size / 2) * 4) / 5;
424
+
425
+ for (int x = 0; x < 8; x++) {
426
+
427
+ for (int y = 0; y < 8; y++) {
428
+
429
+ p.x = (unit_size * (x + 1) + unit_size * (x + 2)) / 2;
430
+
431
+ p.y = (unit_size * (y + 1) + unit_size * (y + 2)) / 2;
432
+
433
+ ste[x][y].paint(g, p, rad);
434
+
435
+ }
436
+
437
+ }
438
+
439
+ }
440
+
441
+
442
+
443
+ void setStone(int x, int y, int s) {
444
+
445
+ ste[x][y].setObverse(s);
446
+
447
+ }
448
+
449
+ }
450
+
451
+
452
+
453
+ public class Reversi extends JPanel {
454
+
455
+ private final static int UNIT_SIZE = 80;
456
+
457
+ private Board board = new Board();
458
+
459
+
460
+
461
+ private int turn = 1;
462
+
463
+
464
+
465
+ public Reversi() {
466
+
467
+ setPreferredSize(new Dimension(800, 800));
468
+
469
+ addMouseListener(new MouseProc());
470
+
471
+ }
472
+
473
+
474
+
475
+ public void paintComponent(Graphics g) {
476
+
477
+ board.paint(g, UNIT_SIZE);
478
+
479
+ }
480
+
481
+
482
+
483
+ void changeTurn() {
484
+
485
+ if (turn == 1)
486
+
487
+ turn = 2;
488
+
489
+ else if (turn == 2)
490
+
491
+ turn = 1;
492
+
493
+ }
494
+
495
+
496
+
497
+ void MessageDialog(String str) {
498
+
499
+ JOptionPane.showMessageDialog(this, str, "情報", JOptionPane.INFORMATION_MESSAGE);
500
+
501
+ }
502
+
503
+
504
+
505
+ void EndMessageDialog(int num_black, int num_white) {
506
+
507
+ if (num_black > num_white) {
508
+
509
+ String str = "[黒:" + num_black + ",白:" + num_white + "]で黒の勝ち";
510
+
511
+ JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
512
+
513
+ } else if (num_black == num_white) {
514
+
515
+ String str = "[黒:" + num_black + ",白:" + num_white + "]で引き分け";
516
+
517
+ JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
518
+
519
+ } else {
520
+
521
+ String str = "[黒:" + num_black + ",白:" + num_white + "]で白の勝ち";
522
+
523
+ JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
524
+
525
+ }
526
+
527
+
528
+
529
+ System.exit(0);
530
+
531
+ }
532
+
533
+
534
+
535
+ class MouseProc extends MouseAdapter {
536
+
537
+ public void mouseClicked(MouseEvent me) {
538
+
539
+ Point point = me.getPoint();
540
+
541
+ int btn = me.getButton();
542
+
543
+ System.out.println("(" + point.x + "," + point.y + ")");
544
+
545
+ board.evaluateBoard();
546
+
547
+ if (turn == 1 && btn == MouseEvent.BUTTON1) {
548
+
549
+ int x = point.x / UNIT_SIZE - 1;
550
+
551
+ int y = point.y / UNIT_SIZE - 1;
552
+
553
+ System.out.println("[" + x + "]" + "[" + y + "]");
554
+
555
+ if (0 <= x && x < 8 && 0 <= y && y < 8) {
556
+
557
+ if (board.eval_black[x][y] < 1) {
558
+
559
+ System.out.println("そこに石を置けません");
560
+
561
+ } else {
562
+
563
+ board.setStone(x, y, 1);
564
+
565
+ board.setStoneAndReverse(x, y, 1);
566
+
567
+ repaint();
568
+
569
+ board.printBoard();
570
+
571
+ board.evaluateBoard();
572
+
573
+ board.printEval();
574
+
575
+ changeTurn();
576
+
577
+ if (board.num_grid_black >= 1) {
578
+
579
+ MessageDialog("白の番です");
580
+
581
+ } else {
582
+
583
+ changeTurn();
584
+
585
+ MessageDialog("あなたはパスです");
586
+
587
+ }
588
+
589
+ }
590
+
591
+ }
592
+
593
+ } else if (turn == 2 && btn == MouseEvent.BUTTON3) {
594
+
595
+ int x = point.x / UNIT_SIZE - 1;
596
+
597
+ int y = point.y / UNIT_SIZE - 1;
598
+
599
+ System.out.println("[" + x + "]" + "[" + y + "]");
600
+
601
+ if (0 <= x && x < 8 && 0 <= y && y < 8) {
602
+
603
+ if (board.eval_white[x][y] < 1) {
604
+
605
+ System.out.println("そこに石を置けません");
606
+
607
+ } else {
608
+
609
+ board.setStone(x, y, 2);
610
+
611
+ board.setStoneAndReverse(x, y, 2);
612
+
613
+ repaint();
614
+
615
+ board.printBoard();
616
+
617
+ board.evaluateBoard();
618
+
619
+ board.printEval();
620
+
621
+ changeTurn();
622
+
623
+ if (board.num_grid_black >= 1) {
624
+
625
+ MessageDialog("黒の番です");
626
+
627
+ } else {
628
+
629
+ changeTurn();
630
+
631
+ MessageDialog("あなたはパスです");
632
+
633
+ }
634
+
635
+ }
636
+
637
+ }
638
+
639
+ }
640
+
641
+
642
+
643
+ int num_black = 0;
644
+
645
+ int num_white = 0;
646
+
647
+ for (int x = 0; x < 8; x++) {
648
+
649
+ for (int y = 0; y < 8; y++) {
650
+
651
+ if (board.ste[x][y].obverse == 1)
652
+
653
+ num_black++;
654
+
655
+ if (board.ste[x][y].obverse == 2)
656
+
657
+ num_white++;
658
+
659
+ }
660
+
661
+ }
662
+
663
+
664
+
665
+ if (board.num_grid_black < 1 && board.num_grid_white < 1) {
666
+
667
+ EndMessageDialog(num_black, num_white);
668
+
669
+ }
670
+
671
+ }
672
+
673
+ }
674
+
675
+
676
+
677
+ public static void main(String[] args) {
678
+
679
+ JFrame f = new JFrame();
680
+
681
+
682
+
683
+ f.getContentPane().setLayout(new FlowLayout());
684
+
685
+ f.getContentPane().add(new Reversi());
686
+
687
+ f.pack();
688
+
689
+ f.setResizable(false);
690
+
691
+ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
692
+
693
+ f.setVisible(true);
694
+
695
+ }
696
+
697
+ }
698
+
699
+ ```

2

問題点追加

2019/11/27 17:17

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -27,3 +27,5 @@
27
27
 
28
28
 
29
29
  あとは、順番に関係なく石を置けてしまう問題があります。
30
+
31
+ パスする処理も変です。

1

問題点指摘追加

2019/11/27 02:21

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1,4 +1,4 @@
1
- ひとまず、ここ
1
+ まず、ここ
2
2
 
3
3
 
4
4
 
@@ -23,3 +23,7 @@
23
23
 
24
24
 
25
25
  ですね。
26
+
27
+
28
+
29
+ あとは、順番に関係なく石を置けてしまう問題があります。