回答編集履歴
6
クラス名を英語オセロルールの用語に変更
answer
CHANGED
@@ -2,22 +2,23 @@
|
|
2
2
|
まだリファクタリングできるところはあると思います。
|
3
3
|
|
4
4
|
```java
|
5
|
+
import java.util.*;
|
6
|
+
import java.util.stream.*;
|
5
7
|
import java.awt.*;
|
8
|
+
import java.awt.Point;
|
9
|
+
import java.awt.event.*;
|
6
10
|
import javax.swing.*;
|
7
|
-
import java.awt.event.*;
|
8
|
-
import java.util.*;
|
9
|
-
import java.util.stream.*;
|
10
11
|
|
11
|
-
interface
|
12
|
+
interface Disk {
|
12
13
|
public void paint(Graphics g, Point p, int size);
|
13
14
|
public String toString();
|
14
15
|
}
|
15
16
|
|
16
|
-
class
|
17
|
+
class Disks {
|
17
|
-
static final
|
18
|
+
static final Disk NONE, BLACK, WHITE;
|
18
19
|
|
19
20
|
static {
|
20
|
-
NONE = new
|
21
|
+
NONE = new Disk() {
|
21
22
|
public void paint(Graphics g, Point p, int size) {
|
22
23
|
}
|
23
24
|
|
@@ -26,7 +27,7 @@
|
|
26
27
|
}
|
27
28
|
};
|
28
29
|
|
29
|
-
BLACK = new
|
30
|
+
BLACK = new Disk() {
|
30
31
|
public void paint(Graphics g, Point p, int size) {
|
31
32
|
int r = (size / 2) * 4 / 5;
|
32
33
|
g.setColor(Color.black);
|
@@ -38,7 +39,7 @@
|
|
38
39
|
}
|
39
40
|
};
|
40
41
|
|
41
|
-
WHITE = new
|
42
|
+
WHITE = new Disk() {
|
42
43
|
public void paint(Graphics g, Point p, int size) {
|
43
44
|
int r = (size / 2) * 4 / 5;
|
44
45
|
g.setColor(Color.white);
|
@@ -51,7 +52,7 @@
|
|
51
52
|
};
|
52
53
|
}
|
53
54
|
|
54
|
-
private static final Map<
|
55
|
+
private static final Map<Disk, Disk> OPPOSITE = new HashMap<Disk, Disk>() {
|
55
56
|
{
|
56
57
|
put(NONE, NONE);
|
57
58
|
put(BLACK, WHITE);
|
@@ -59,80 +60,81 @@
|
|
59
60
|
}
|
60
61
|
};
|
61
62
|
|
62
|
-
static
|
63
|
+
static Disk opposite(Disk disk) {
|
63
|
-
return OPPOSITE.get(
|
64
|
+
return OPPOSITE.get(disk);
|
64
65
|
}
|
65
66
|
}
|
66
67
|
|
67
|
-
class
|
68
|
+
class Square {
|
68
|
-
private
|
69
|
+
private Disk disk;
|
69
70
|
|
70
|
-
|
71
|
+
Square() {
|
71
|
-
this.
|
72
|
+
this.disk = Disks.NONE;
|
72
73
|
}
|
73
74
|
|
74
|
-
boolean
|
75
|
+
boolean is_used() {
|
75
|
-
return this.
|
76
|
+
return this.disk != Disks.NONE;
|
76
77
|
}
|
77
78
|
|
78
|
-
boolean is(
|
79
|
+
boolean is(Disk disk) {
|
79
|
-
return this.
|
80
|
+
return this.disk == disk;
|
80
81
|
}
|
81
82
|
|
82
|
-
void put(
|
83
|
+
void put(Disk disk) {
|
83
|
-
this.
|
84
|
+
this.disk = disk;
|
84
85
|
}
|
85
86
|
|
86
|
-
Stone get() {
|
87
|
-
return this.stone;
|
88
|
-
}
|
89
|
-
|
90
87
|
void reverse() {
|
91
|
-
this.
|
88
|
+
this.disk = Disks.opposite(this.disk);
|
92
89
|
}
|
93
90
|
|
94
91
|
void paint(Graphics g, Point p, int size) {
|
95
|
-
|
92
|
+
disk.paint(g, p, size);
|
96
93
|
}
|
94
|
+
|
95
|
+
public String toString() {
|
96
|
+
return disk.toString();
|
97
|
+
}
|
97
98
|
}
|
98
99
|
|
99
100
|
class Board {
|
100
|
-
final
|
101
|
+
final Square[][] squares = new Square[8][8];
|
101
102
|
final int[][] eval_black = new int[8][8];
|
102
103
|
final int[][] eval_white = new int[8][8];
|
103
104
|
|
105
|
+
private static final Color COLOR = new Color(0, 85, 0);
|
104
106
|
private static final Point[] DIRECTION = {
|
105
|
-
new Point(1, 0),
|
106
|
-
new Point(1, 1),
|
107
|
-
new Point(0, 1),
|
108
|
-
new Point(-1, 1),
|
109
|
-
new Point(-1, 0),
|
110
107
|
new Point(-1, -1),
|
111
|
-
new Point(0, -1),
|
108
|
+
new Point( 0, -1),
|
112
|
-
new Point(1, -1),
|
109
|
+
new Point( 1, -1),
|
110
|
+
new Point(-1, 0),
|
111
|
+
new Point( 1, 0),
|
112
|
+
new Point(-1, 1),
|
113
|
+
new Point( 0, 1),
|
114
|
+
new Point( 1, 1),
|
113
115
|
};
|
114
116
|
|
115
117
|
Board() {
|
116
118
|
for (int x = 0; x < 8; x++) {
|
117
119
|
for (int y = 0; y < 8; y++) {
|
118
|
-
|
120
|
+
squares[x][y] = new Square();
|
119
121
|
}
|
120
122
|
}
|
123
|
+
squares[3][3].put(Disks.WHITE);
|
121
|
-
|
124
|
+
squares[4][3].put(Disks.BLACK);
|
125
|
+
squares[3][4].put(Disks.BLACK);
|
122
|
-
|
126
|
+
squares[4][4].put(Disks.WHITE);
|
123
|
-
mass[4][3].put(Stones.WHITE);
|
124
|
-
mass[4][4].put(Stones.BLACK);
|
125
127
|
}
|
126
128
|
|
127
129
|
boolean isOnBoard(int x, int y) {
|
128
130
|
return 0 <= x && x < 8 && 0 <= y && y < 8;
|
129
131
|
}
|
130
132
|
|
131
|
-
long count(
|
133
|
+
long count(Disk disk) {
|
132
|
-
return Arrays.stream(
|
134
|
+
return Arrays.stream(squares)
|
133
|
-
|
135
|
+
.flatMap(Stream::of)
|
134
|
-
|
136
|
+
.filter(m -> m.is(disk))
|
135
|
-
|
137
|
+
.count();
|
136
138
|
}
|
137
139
|
|
138
140
|
class Reversible {
|
@@ -152,76 +154,67 @@
|
|
152
154
|
|
153
155
|
void reverse() {
|
154
156
|
for (Point p: points) {
|
155
|
-
|
157
|
+
squares[p.x][p.y].reverse();
|
156
158
|
}
|
157
159
|
}
|
158
160
|
}
|
159
161
|
|
160
|
-
// 盤面(x,y)から方向dに向かって反転ラインを作る
|
161
|
-
Reversible getReversible(int x, int y, Point d,
|
162
|
+
Reversible getReversible(int x, int y, Point d, Disk s) {
|
163
|
+
Reversible reversible = new Reversible();
|
164
|
+
Disk opposite = Disks.opposite(s);
|
162
165
|
x += d.x;
|
163
166
|
y += d.y;
|
164
|
-
Reversible reversible = new Reversible();
|
165
|
-
Stone opposite = Stones.opposite(s);
|
166
|
-
while (isOnBoard(x, y) &&
|
167
|
+
while (isOnBoard(x, y) && squares[x][y].is(opposite)) {
|
167
168
|
reversible.add(new Point(x, y));
|
168
169
|
x += d.x;
|
169
170
|
y += d.y;
|
170
171
|
}
|
171
|
-
if (!(isOnBoard(x, y) &&
|
172
|
+
if (!(isOnBoard(x, y) && squares[x][y].is(s)))
|
172
173
|
reversible.removeAll();
|
173
174
|
return reversible;
|
174
175
|
}
|
175
176
|
|
176
|
-
// 盤面(x,y)に石sを置いた場合に反転できる石の数を数える
|
177
|
-
int
|
177
|
+
int countReversible(int x, int y, Disk s) {
|
178
|
-
// 既に石が置かれていたら、置けない
|
179
|
-
if (
|
178
|
+
if (squares[x][y].is_used())
|
180
179
|
return -1;
|
181
|
-
|
180
|
+
|
182
|
-
int count = 0;
|
183
|
-
|
181
|
+
return Arrays.stream(DIRECTION)
|
184
|
-
|
182
|
+
.mapToInt(d -> getReversible(x, y, d, s).count())
|
185
|
-
|
183
|
+
.sum();
|
186
184
|
}
|
187
185
|
|
188
|
-
// 石を置き他を反転させる
|
189
|
-
void putAndReverse(int x, int y,
|
186
|
+
void putAndReverse(int x, int y, Disk s) {
|
190
|
-
|
187
|
+
squares[x][y].put(s);
|
191
|
-
for (Point d: DIRECTION)
|
192
|
-
|
188
|
+
Arrays.stream(DIRECTION).forEach(d -> getReversible(x, y, d, s).reverse());
|
193
189
|
}
|
194
190
|
|
195
|
-
// 盤面を見る
|
196
|
-
void
|
191
|
+
void evaluate() {
|
197
192
|
for (int x = 0; x < 8; x++) {
|
198
193
|
for (int y = 0; y < 8; y++) {
|
199
|
-
eval_black[x][y] =
|
194
|
+
eval_black[x][y] = countReversible(x, y, Disks.BLACK);
|
200
|
-
eval_white[x][y] =
|
195
|
+
eval_white[x][y] = countReversible(x, y, Disks.WHITE);
|
201
196
|
}
|
202
197
|
}
|
203
198
|
}
|
204
199
|
|
205
|
-
// 盤面をコンソールに表示する
|
206
200
|
void printBoard() {
|
207
201
|
for (int y = 0; y < 8; y++) {
|
208
202
|
for (int x = 0; x < 8; x++) {
|
209
|
-
System.out.printf("%s ",
|
203
|
+
System.out.printf("%s ", squares[x][y]);
|
210
204
|
}
|
211
205
|
System.out.println();
|
212
206
|
}
|
213
207
|
}
|
214
208
|
|
215
|
-
// 盤面の評価結果をコンソールに表示する
|
216
|
-
void
|
209
|
+
void printEvaluation() {
|
217
|
-
System.out.println("Black(" +
|
210
|
+
System.out.println("Black(" + Disks.BLACK + "):");
|
218
211
|
for (int y = 0; y < 8; y++) {
|
219
212
|
for (int x = 0; x < 8; x++) {
|
220
213
|
System.out.printf("%2d ", eval_black[x][y]);
|
221
214
|
}
|
222
215
|
System.out.println();
|
223
216
|
}
|
224
|
-
System.out.println("White(" +
|
217
|
+
System.out.println("White(" + Disks.WHITE + "):");
|
225
218
|
for (int y = 0; y < 8; y++) {
|
226
219
|
for (int x = 0; x < 8; x++) {
|
227
220
|
System.out.printf("%2d ", eval_white[x][y]);
|
@@ -230,44 +223,45 @@
|
|
230
223
|
}
|
231
224
|
}
|
232
225
|
|
233
|
-
// 画面描画
|
234
|
-
void paint(Graphics g, int
|
226
|
+
void paint(Graphics g, int square_size) {
|
235
227
|
|
236
|
-
//
|
228
|
+
// Background
|
237
229
|
g.setColor(Color.black);
|
238
|
-
g.fillRect(0, 0,
|
230
|
+
g.fillRect(0, 0, square_size * 10, square_size * 10);
|
239
231
|
|
240
|
-
//
|
232
|
+
// Board
|
241
|
-
g.setColor(
|
233
|
+
g.setColor(Board.COLOR);
|
242
|
-
g.fillRect(
|
234
|
+
g.fillRect(square_size, square_size, square_size * 8, square_size * 8);
|
243
235
|
|
244
|
-
//
|
236
|
+
// Horizontal Line
|
245
237
|
g.setColor(Color.black);
|
246
238
|
for (int x = 0; x < 9; x++) {
|
247
|
-
g.drawLine(
|
239
|
+
g.drawLine(square_size * (x + 1), square_size, square_size * (x + 1), square_size * 9);
|
248
240
|
}
|
249
241
|
|
250
|
-
//
|
242
|
+
// Vertical Line
|
251
243
|
g.setColor(Color.black);
|
252
244
|
for (int y = 0; y < 9; y++) {
|
253
|
-
g.drawLine(
|
245
|
+
g.drawLine(square_size, square_size * (y + 1), square_size * 9, square_size * (y + 1));
|
254
246
|
}
|
255
247
|
|
256
|
-
//
|
248
|
+
// Dots
|
257
249
|
for (int x = 0; x < 2; x++) {
|
258
250
|
for (int y = 0; y < 2; y++) {
|
259
|
-
g.fillRect(
|
251
|
+
g.fillRect(square_size * (3 + 4 * x) - (square_size / 16),
|
260
|
-
|
252
|
+
square_size * (3 + 4 * y) - (square_size / 16),
|
253
|
+
square_size / 8,
|
254
|
+
square_size / 8);
|
261
255
|
}
|
262
256
|
}
|
263
257
|
|
264
|
-
//
|
258
|
+
// Disk
|
265
259
|
Point p = new Point();
|
266
260
|
for (int x = 0; x < 8; x++) {
|
267
261
|
for (int y = 0; y < 8; y++) {
|
268
|
-
p.x =
|
262
|
+
p.x = square_size * (x + 1) + square_size / 2;
|
269
|
-
p.y =
|
263
|
+
p.y = square_size * (y + 1) + square_size / 2;
|
270
|
-
|
264
|
+
squares[x][y].paint(g, p, square_size);
|
271
265
|
}
|
272
266
|
}
|
273
267
|
}
|
@@ -275,12 +269,12 @@
|
|
275
269
|
|
276
270
|
class Player {
|
277
271
|
private final String name;
|
278
|
-
private final
|
272
|
+
private final Disk disk;
|
279
273
|
private final int[][] reversible;
|
280
274
|
|
281
|
-
Player(String name,
|
275
|
+
Player(String name, Disk disk, int[][] reversible) {
|
282
276
|
this.name = name;
|
283
|
-
this.
|
277
|
+
this.disk = disk;
|
284
278
|
this.reversible = reversible;
|
285
279
|
}
|
286
280
|
|
@@ -293,11 +287,11 @@
|
|
293
287
|
}
|
294
288
|
|
295
289
|
void putAndReverse(Board board, int x, int y) {
|
296
|
-
board.putAndReverse(x, y,
|
290
|
+
board.putAndReverse(x, y, disk);
|
297
291
|
}
|
298
292
|
|
299
293
|
long count(Board board) {
|
300
|
-
return board.count(
|
294
|
+
return board.count(disk);
|
301
295
|
}
|
302
296
|
|
303
297
|
boolean pass() {
|
@@ -312,36 +306,39 @@
|
|
312
306
|
}
|
313
307
|
|
314
308
|
public class Reversi extends JPanel {
|
315
|
-
private
|
309
|
+
private static final int SQUARE_SIZE = 80;
|
316
310
|
private final Board board = new Board();
|
317
|
-
private final Player player1 = new Player("黒",
|
311
|
+
private final Player player1 = new Player("黒", Disks.BLACK, board.eval_black);
|
318
|
-
private final Player player2 = new Player("白",
|
312
|
+
private final Player player2 = new Player("白", Disks.WHITE, board.eval_white);
|
319
313
|
private Player player = player1;
|
320
314
|
|
321
315
|
public Reversi() {
|
322
316
|
setPreferredSize(new Dimension(800, 800));
|
323
|
-
addMouseListener(new
|
317
|
+
addMouseListener(new MouseHandler());
|
324
318
|
}
|
325
319
|
|
320
|
+
void turnPlayer() {
|
321
|
+
player = player == player1 ? player2 : player1;
|
322
|
+
}
|
323
|
+
|
326
324
|
public void paintComponent(Graphics g) {
|
327
|
-
board.paint(g,
|
325
|
+
board.paint(g, SQUARE_SIZE);
|
328
326
|
}
|
329
327
|
|
330
|
-
void turnPlayer() {
|
331
|
-
|
328
|
+
void showMessage(String message, String title) {
|
329
|
+
JOptionPane.showMessageDialog(this, message, title, JOptionPane.INFORMATION_MESSAGE);
|
332
330
|
}
|
333
331
|
|
334
332
|
void showMessage(String message) {
|
335
|
-
|
333
|
+
showMessage(message, "情報");
|
336
334
|
}
|
337
335
|
|
338
|
-
// 黒石と白石の数を比較し終了
|
339
|
-
void
|
336
|
+
void endGameAndExit() {
|
340
337
|
long num_player1 = player1.count(board);
|
341
338
|
long num_player2 = player2.count(board);
|
342
|
-
String message = "["
|
339
|
+
String message = "["
|
343
|
-
|
340
|
+
+ player1 + ":" + num_player1 + ","
|
344
|
-
|
341
|
+
+ player2 + ":" + num_player2 + "]で";
|
345
342
|
if (num_player1 > num_player2) {
|
346
343
|
message += player1 + "の勝ち";
|
347
344
|
} else if (num_player1 <= num_player2) {
|
@@ -349,50 +346,51 @@
|
|
349
346
|
} else {
|
350
347
|
message += "引き分け";
|
351
348
|
}
|
352
|
-
|
349
|
+
showMessage(message, "ゲーム終了");
|
353
350
|
|
354
351
|
System.exit(0);
|
355
352
|
}
|
356
353
|
|
357
|
-
// クリックされた時の処理用クラス
|
358
|
-
class
|
354
|
+
class MouseHandler extends MouseAdapter {
|
359
355
|
public void mouseClicked(MouseEvent me) {
|
360
356
|
board.printBoard();
|
361
357
|
Point point = me.getPoint();
|
362
358
|
System.out.println("(" + point.x + "," + point.y + ")");
|
363
|
-
int x = point.x /
|
359
|
+
int x = point.x / SQUARE_SIZE - 1;
|
364
|
-
int y = point.y /
|
360
|
+
int y = point.y / SQUARE_SIZE - 1;
|
365
361
|
if (!(0 <= x && x < 8 && 0 <= y && y < 8)) return;
|
366
362
|
System.out.println("[" + x + "]" + "[" + y + "]");
|
367
|
-
board.evaluateBoard();
|
368
363
|
int btn = me.getButton();
|
369
|
-
if (
|
364
|
+
if (player == player1 && btn == MouseEvent.BUTTON1 ||
|
370
|
-
|
365
|
+
player == player2 && btn == MouseEvent.BUTTON3) {
|
371
|
-
|
366
|
+
put(x, y);
|
372
367
|
}
|
368
|
+
}
|
369
|
+
}
|
370
|
+
|
371
|
+
void put(int x, int y) {
|
372
|
+
board.evaluate();
|
373
|
-
|
373
|
+
if (!player.putable(board, x, y)) {
|
374
|
-
|
374
|
+
System.out.println("そこに石を置けません");
|
375
|
-
|
375
|
+
return;
|
376
|
-
|
376
|
+
}
|
377
|
-
|
377
|
+
player.putAndReverse(board, x, y);
|
378
|
-
|
378
|
+
repaint();
|
379
|
-
|
379
|
+
board.printBoard();
|
380
|
-
|
380
|
+
board.evaluate();
|
381
|
-
|
381
|
+
board.printEvaluation();
|
382
382
|
|
383
|
-
|
383
|
+
if (player1.pass() && player2.pass()) {
|
384
|
-
|
384
|
+
endGameAndExit();
|
385
|
-
|
385
|
+
}
|
386
|
+
turnPlayer();
|
387
|
+
if (player.pass()) {
|
388
|
+
showMessage(player + "はパスです");
|
386
389
|
turnPlayer();
|
387
|
-
if (player.pass()) {
|
388
|
-
showMessage(player + "はパスです");
|
389
|
-
turnPlayer();
|
390
|
-
}
|
391
|
-
showMessage(player + "の番です");
|
392
390
|
}
|
391
|
+
showMessage(player + "の番です");
|
393
392
|
}
|
394
393
|
|
395
|
-
// 起動
|
396
394
|
public static void main(String[] args) {
|
397
395
|
JFrame f = new JFrame();
|
398
396
|
f.getContentPane().setLayout(new FlowLayout());
|
5
変数 i, j を x, y または y, x に変更
answer
CHANGED
@@ -194,10 +194,10 @@
|
|
194
194
|
|
195
195
|
// 盤面を見る
|
196
196
|
void evaluateBoard() {
|
197
|
-
for (int
|
197
|
+
for (int x = 0; x < 8; x++) {
|
198
|
-
for (int
|
198
|
+
for (int y = 0; y < 8; y++) {
|
199
|
-
eval_black[
|
199
|
+
eval_black[x][y] = countReverseStone(x, y, Stones.BLACK);
|
200
|
-
eval_white[
|
200
|
+
eval_white[x][y] = countReverseStone(x, y, Stones.WHITE);
|
201
201
|
}
|
202
202
|
}
|
203
203
|
}
|
@@ -215,16 +215,16 @@
|
|
215
215
|
// 盤面の評価結果をコンソールに表示する
|
216
216
|
void printEval() {
|
217
217
|
System.out.println("Black(" + Stones.BLACK + "):");
|
218
|
-
for (int
|
218
|
+
for (int y = 0; y < 8; y++) {
|
219
|
-
for (int
|
219
|
+
for (int x = 0; x < 8; x++) {
|
220
|
-
System.out.printf("%2d ", eval_black[
|
220
|
+
System.out.printf("%2d ", eval_black[x][y]);
|
221
221
|
}
|
222
222
|
System.out.println();
|
223
223
|
}
|
224
224
|
System.out.println("White(" + Stones.WHITE + "):");
|
225
|
-
for (int
|
225
|
+
for (int y = 0; y < 8; y++) {
|
226
|
-
for (int
|
226
|
+
for (int x = 0; x < 8; x++) {
|
227
|
-
System.out.printf("%2d ", eval_white[
|
227
|
+
System.out.printf("%2d ", eval_white[x][y]);
|
228
228
|
}
|
229
229
|
System.out.println();
|
230
230
|
}
|
@@ -243,20 +243,20 @@
|
|
243
243
|
|
244
244
|
// 横線
|
245
245
|
g.setColor(Color.black);
|
246
|
-
for (int
|
246
|
+
for (int x = 0; x < 9; x++) {
|
247
|
-
g.drawLine(unit_size * (
|
247
|
+
g.drawLine(unit_size * (x + 1), unit_size, unit_size * (x + 1), unit_size * 9);
|
248
248
|
}
|
249
249
|
|
250
250
|
// 縦線
|
251
251
|
g.setColor(Color.black);
|
252
|
-
for (int
|
252
|
+
for (int y = 0; y < 9; y++) {
|
253
|
-
g.drawLine(unit_size, unit_size * (
|
253
|
+
g.drawLine(unit_size, unit_size * (y + 1), unit_size * 9, unit_size * (y + 1));
|
254
254
|
}
|
255
255
|
|
256
256
|
// 目印
|
257
|
-
for (int
|
257
|
+
for (int x = 0; x < 2; x++) {
|
258
|
-
for (int
|
258
|
+
for (int y = 0; y < 2; y++) {
|
259
|
-
g.fillRect(unit_size * (3 + 4 *
|
259
|
+
g.fillRect(unit_size * (3 + 4 * x) - (unit_size / 16), unit_size * (3 + 4 * y) - (unit_size / 16),
|
260
260
|
(unit_size / 8), (unit_size / 8));
|
261
261
|
}
|
262
262
|
}
|
4
大文字開始メソッド名を小文字メソッド名に変更
answer
CHANGED
@@ -288,12 +288,12 @@
|
|
288
288
|
return name;
|
289
289
|
}
|
290
290
|
|
291
|
-
|
291
|
+
boolean putable(Board board, int x, int y) {
|
292
292
|
return reversible[x][y] >= 1;
|
293
|
-
|
293
|
+
}
|
294
294
|
|
295
295
|
void putAndReverse(Board board, int x, int y) {
|
296
|
-
|
296
|
+
board.putAndReverse(x, y, stone);
|
297
297
|
}
|
298
298
|
|
299
299
|
long count(Board board) {
|
@@ -331,12 +331,12 @@
|
|
331
331
|
player = player == player1 ? player2 : player1;
|
332
332
|
}
|
333
333
|
|
334
|
-
void
|
334
|
+
void showMessage(String message) {
|
335
|
-
JOptionPane.showMessageDialog(this,
|
335
|
+
JOptionPane.showMessageDialog(this, message, "情報", JOptionPane.INFORMATION_MESSAGE);
|
336
336
|
}
|
337
337
|
|
338
338
|
// 黒石と白石の数を比較し終了
|
339
|
-
void
|
339
|
+
void endGame() {
|
340
340
|
long num_player1 = player1.count(board);
|
341
341
|
long num_player2 = player2.count(board);
|
342
342
|
String message = "[";
|
@@ -368,7 +368,7 @@
|
|
368
368
|
int btn = me.getButton();
|
369
369
|
if (!(player == player1 && btn == MouseEvent.BUTTON1 ||
|
370
370
|
player == player2 && btn == MouseEvent.BUTTON3)) {
|
371
|
-
|
371
|
+
return;
|
372
372
|
}
|
373
373
|
if (!player.putable(board, x, y)) {
|
374
374
|
System.out.println("そこに石を置けません");
|
@@ -381,14 +381,14 @@
|
|
381
381
|
board.printEval();
|
382
382
|
|
383
383
|
if (player1.pass() && player2.pass()) {
|
384
|
-
|
384
|
+
endGame();
|
385
385
|
}
|
386
386
|
turnPlayer();
|
387
387
|
if (player.pass()) {
|
388
|
-
|
388
|
+
showMessage(player + "はパスです");
|
389
389
|
turnPlayer();
|
390
390
|
}
|
391
|
-
|
391
|
+
showMessage(player + "の番です");
|
392
392
|
}
|
393
393
|
}
|
394
394
|
|
3
player.putを処理分轄
answer
CHANGED
@@ -288,13 +288,12 @@
|
|
288
288
|
return name;
|
289
289
|
}
|
290
290
|
|
291
|
-
|
291
|
+
boolean putable(Board board, int x, int y) {
|
292
|
-
|
292
|
+
return reversible[x][y] >= 1;
|
293
|
+
}
|
294
|
+
|
295
|
+
void putAndReverse(Board board, int x, int y) {
|
293
|
-
|
296
|
+
board.putAndReverse(x, y, stone);
|
294
|
-
return true;
|
295
|
-
} else {
|
296
|
-
return false;
|
297
|
-
}
|
298
297
|
}
|
299
298
|
|
300
299
|
long count(Board board) {
|
@@ -367,17 +366,15 @@
|
|
367
366
|
System.out.println("[" + x + "]" + "[" + y + "]");
|
368
367
|
board.evaluateBoard();
|
369
368
|
int btn = me.getButton();
|
370
|
-
if (player == player1
|
369
|
+
if (!(player == player1 && btn == MouseEvent.BUTTON1 ||
|
371
|
-
|
370
|
+
player == player2 && btn == MouseEvent.BUTTON3)) {
|
372
|
-
|
371
|
+
return;
|
373
|
-
} else {
|
374
|
-
if (btn != MouseEvent.BUTTON3)
|
375
|
-
return;
|
376
372
|
}
|
377
|
-
if (!player.
|
373
|
+
if (!player.putable(board, x, y)) {
|
378
374
|
System.out.println("そこに石を置けません");
|
379
375
|
return;
|
380
376
|
}
|
377
|
+
player.putAndReverse(board, x, y);
|
381
378
|
repaint();
|
382
379
|
board.printBoard();
|
383
380
|
board.evaluateBoard();
|
2
盤面表示文字変更
answer
CHANGED
@@ -34,7 +34,7 @@
|
|
34
34
|
}
|
35
35
|
|
36
36
|
public String toString() {
|
37
|
-
return "
|
37
|
+
return "×";
|
38
38
|
}
|
39
39
|
};
|
40
40
|
|
@@ -46,7 +46,7 @@
|
|
46
46
|
}
|
47
47
|
|
48
48
|
public String toString() {
|
49
|
-
return "
|
49
|
+
return "●";
|
50
50
|
}
|
51
51
|
};
|
52
52
|
}
|
@@ -214,14 +214,14 @@
|
|
214
214
|
|
215
215
|
// 盤面の評価結果をコンソールに表示する
|
216
216
|
void printEval() {
|
217
|
-
System.out.println("Black(
|
217
|
+
System.out.println("Black(" + Stones.BLACK + "):");
|
218
218
|
for (int i = 0; i < 8; i++) {
|
219
219
|
for (int j = 0; j < 8; j++) {
|
220
220
|
System.out.printf("%2d ", eval_black[j][i]);
|
221
221
|
}
|
222
222
|
System.out.println();
|
223
223
|
}
|
224
|
-
System.out.println("White(
|
224
|
+
System.out.println("White(" + Stones.WHITE + "):");
|
225
225
|
for (int i = 0; i < 8; i++) {
|
226
226
|
for (int j = 0; j < 8; j++) {
|
227
227
|
System.out.printf("%2d ", eval_white[j][i]);
|
@@ -315,7 +315,6 @@
|
|
315
315
|
public class Reversi extends JPanel {
|
316
316
|
private final static int UNIT_SIZE = 80;
|
317
317
|
private final Board board = new Board();
|
318
|
-
|
319
318
|
private final Player player1 = new Player("黒", Stones.BLACK, board.eval_black);
|
320
319
|
private final Player player2 = new Player("白", Stones.WHITE, board.eval_white);
|
321
320
|
private Player player = player1;
|
@@ -399,7 +398,6 @@
|
|
399
398
|
// 起動
|
400
399
|
public static void main(String[] args) {
|
401
400
|
JFrame f = new JFrame();
|
402
|
-
|
403
401
|
f.getContentPane().setLayout(new FlowLayout());
|
404
402
|
f.getContentPane().add(new Reversi());
|
405
403
|
f.pack();
|
1
Reversi2 を Reversi に修正
answer
CHANGED
@@ -312,7 +312,7 @@
|
|
312
312
|
}
|
313
313
|
}
|
314
314
|
|
315
|
-
public class
|
315
|
+
public class Reversi extends JPanel {
|
316
316
|
private final static int UNIT_SIZE = 80;
|
317
317
|
private final Board board = new Board();
|
318
318
|
|
@@ -320,7 +320,7 @@
|
|
320
320
|
private final Player player2 = new Player("白", Stones.WHITE, board.eval_white);
|
321
321
|
private Player player = player1;
|
322
322
|
|
323
|
-
public
|
323
|
+
public Reversi() {
|
324
324
|
setPreferredSize(new Dimension(800, 800));
|
325
325
|
addMouseListener(new MouseProc());
|
326
326
|
}
|
@@ -401,7 +401,7 @@
|
|
401
401
|
JFrame f = new JFrame();
|
402
402
|
|
403
403
|
f.getContentPane().setLayout(new FlowLayout());
|
404
|
-
f.getContentPane().add(new
|
404
|
+
f.getContentPane().add(new Reversi());
|
405
405
|
f.pack();
|
406
406
|
f.setResizable(false);
|
407
407
|
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|