回答編集履歴
5
説明文追加
answer
CHANGED
@@ -15,6 +15,8 @@
|
|
15
15
|
あとは、順番に関係なく石を置けてしまう問題があります。
|
16
16
|
パスする処理も変です。
|
17
17
|
|
18
|
+
こちらで正常動作確認したソースコードです。
|
19
|
+
|
18
20
|
```java
|
19
21
|
import java.awt.*;
|
20
22
|
import javax.swing.*;
|
4
インデントをタブ文字に変更
answer
CHANGED
@@ -22,329 +22,329 @@
|
|
22
22
|
import java.util.*;
|
23
23
|
|
24
24
|
class Stone {
|
25
|
-
|
25
|
+
public final static int black = 1;
|
26
|
-
|
26
|
+
public final static int white = 2;
|
27
|
-
|
27
|
+
public int obverse = 0;
|
28
28
|
|
29
|
-
|
29
|
+
Stone() {
|
30
|
-
|
30
|
+
obverse = 0;
|
31
|
-
|
31
|
+
}
|
32
32
|
|
33
|
-
|
33
|
+
void setObverse(int color) {
|
34
|
-
|
34
|
+
if (color == black || color == white)
|
35
|
-
|
35
|
+
obverse = color;
|
36
|
-
|
36
|
+
else
|
37
|
-
|
37
|
+
System.out.println("黒か白でなければいけません");
|
38
|
-
|
38
|
+
}
|
39
39
|
|
40
|
-
|
40
|
+
void doReverse(int color) {
|
41
|
-
|
41
|
+
if (color == black)
|
42
|
-
|
42
|
+
obverse = white;
|
43
|
-
|
43
|
+
else if (color == white)
|
44
|
-
|
44
|
+
obverse = black;
|
45
|
-
|
45
|
+
else
|
46
|
-
|
46
|
+
System.out.println("黒か白でなければいけません");
|
47
|
-
|
47
|
+
}
|
48
48
|
|
49
|
-
|
49
|
+
void paint(Graphics g, Point p, int rad) {
|
50
|
-
|
50
|
+
if (obverse == black) {
|
51
|
-
|
51
|
+
g.setColor(Color.black);
|
52
|
-
|
52
|
+
g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
|
53
|
-
|
53
|
+
} else if (obverse == white) {
|
54
|
-
|
54
|
+
g.setColor(Color.white);
|
55
|
-
|
55
|
+
g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
|
56
|
-
|
56
|
+
}
|
57
|
-
|
57
|
+
}
|
58
58
|
}
|
59
59
|
|
60
60
|
class Board {
|
61
|
-
|
61
|
+
Stone[][] ste = new Stone[8][8];
|
62
|
-
|
62
|
+
public int num_grid_black;
|
63
|
-
|
63
|
+
public int num_grid_white;
|
64
|
-
|
64
|
+
private Point[] direction = new Point[8];
|
65
|
-
|
65
|
+
public int[][] eval_black = new int[8][8];
|
66
|
-
|
66
|
+
public int[][] eval_white = new int[8][8];
|
67
67
|
|
68
|
-
|
68
|
+
Board() {
|
69
69
|
|
70
|
-
|
70
|
+
for (int x = 0; x < 8; x++) {
|
71
|
-
|
71
|
+
for (int y = 0; y < 8; y++) {
|
72
|
-
|
72
|
+
ste[x][y] = new Stone();
|
73
|
-
|
73
|
+
}
|
74
|
-
|
74
|
+
}
|
75
|
-
|
75
|
+
ste[3][3].setObverse(1);
|
76
|
-
|
76
|
+
ste[3][4].setObverse(2);
|
77
|
-
|
77
|
+
ste[4][3].setObverse(2);
|
78
|
-
|
78
|
+
ste[4][4].setObverse(1);
|
79
79
|
|
80
|
-
|
80
|
+
direction[0] = new Point(1, 0);
|
81
|
-
|
81
|
+
direction[1] = new Point(1, 1);
|
82
|
-
|
82
|
+
direction[2] = new Point(0, 1);
|
83
|
-
|
83
|
+
direction[3] = new Point(-1, 1);
|
84
|
-
|
84
|
+
direction[4] = new Point(-1, 0);
|
85
|
-
|
85
|
+
direction[5] = new Point(-1, -1);
|
86
|
-
|
86
|
+
direction[6] = new Point(0, -1);
|
87
|
-
|
87
|
+
direction[7] = new Point(1, -1);
|
88
|
-
|
88
|
+
}
|
89
89
|
|
90
|
-
|
90
|
+
boolean isOnBoard(int x, int y) {
|
91
|
-
|
91
|
+
if (x < 0 || 7 < x || y < 0 || 7 < y)
|
92
|
-
|
92
|
+
return false;
|
93
|
-
|
93
|
+
else
|
94
|
-
|
94
|
+
return true;
|
95
|
-
|
95
|
+
}
|
96
96
|
|
97
|
-
|
97
|
+
ArrayList<Integer> getLine(int x, int y, Point d) {
|
98
|
-
|
98
|
+
ArrayList<Integer> line = new ArrayList<Integer>();
|
99
|
-
|
99
|
+
int cx = x + d.x;
|
100
|
-
|
100
|
+
int cy = y + d.y;
|
101
|
-
|
101
|
+
while (isOnBoard(cx, cy) && ste[cx][cy].obverse != 0) {
|
102
|
-
|
102
|
+
line.add(ste[cx][cy].obverse);
|
103
|
-
|
103
|
+
cx += d.x;
|
104
|
-
|
104
|
+
cy += d.y;
|
105
|
-
|
105
|
+
}
|
106
|
-
|
106
|
+
return line;
|
107
|
-
|
107
|
+
}
|
108
108
|
|
109
|
-
|
109
|
+
int countReverseStone(int x, int y, int s) {
|
110
|
-
|
110
|
+
if (ste[x][y].obverse != 0)
|
111
|
-
|
111
|
+
return -1;
|
112
|
-
|
112
|
+
int cnt = 0;
|
113
|
-
|
113
|
+
for (int d = 0; d < 8; d++) {
|
114
|
-
|
114
|
+
ArrayList<Integer> line = new ArrayList<Integer>();
|
115
|
-
|
115
|
+
line = getLine(x, y, direction[d]);
|
116
|
-
|
116
|
+
int n = 0;
|
117
|
-
|
117
|
+
while (n < line.size() && line.get(n) != s)
|
118
|
-
|
118
|
+
n++;
|
119
|
-
|
119
|
+
if (1 <= n && n < line.size())
|
120
|
-
|
120
|
+
cnt += n;
|
121
|
-
|
121
|
+
}
|
122
|
-
|
122
|
+
return cnt;
|
123
|
-
|
123
|
+
}
|
124
124
|
|
125
|
-
|
125
|
+
void setStoneAndReverse(int x, int y, int s) {
|
126
|
-
|
126
|
+
for (int d = 0; d < 8; d++) {
|
127
|
-
|
127
|
+
ArrayList<Integer> p = new ArrayList<Integer>();
|
128
|
-
|
128
|
+
p = getLine(x, y, direction[d]);
|
129
|
-
|
129
|
+
int cx = x;
|
130
|
-
|
130
|
+
int cy = y;
|
131
|
-
|
131
|
+
int n = 0;
|
132
132
|
|
133
|
-
|
133
|
+
while (n < p.size() - 1 && p.get(n) != s && ste[cx][cy].obverse == s) {
|
134
|
-
|
134
|
+
cx += direction[d].x;
|
135
|
-
|
135
|
+
cy += direction[d].y;
|
136
|
-
|
136
|
+
System.out.println(d);
|
137
|
-
|
137
|
+
System.out.println("cx=" + cx);
|
138
|
-
|
138
|
+
System.out.println("cy=" + cy);
|
139
|
-
|
139
|
+
ste[cx][cy].doReverse(ste[cx][cy].obverse);
|
140
|
-
|
140
|
+
n++;
|
141
|
-
|
141
|
+
}
|
142
|
-
|
142
|
+
}
|
143
|
-
|
143
|
+
}
|
144
144
|
|
145
|
-
|
145
|
+
void evaluateBoard() {
|
146
|
-
|
146
|
+
num_grid_black = 0;
|
147
|
-
|
147
|
+
num_grid_white = 0;
|
148
|
-
|
148
|
+
for (int i = 0; i < 8; i++) {
|
149
|
-
|
149
|
+
for (int j = 0; j < 8; j++) {
|
150
|
-
|
150
|
+
eval_black[i][j] = countReverseStone(i, j, 1);
|
151
|
-
|
151
|
+
if (eval_black[i][j] > 0)
|
152
|
-
|
152
|
+
num_grid_black++;
|
153
|
-
|
153
|
+
eval_white[i][j] = countReverseStone(i, j, 2);
|
154
|
-
|
154
|
+
if (eval_white[i][j] > 0)
|
155
|
-
|
155
|
+
num_grid_white++;
|
156
|
-
|
156
|
+
}
|
157
|
-
|
157
|
+
}
|
158
|
-
|
158
|
+
}
|
159
159
|
|
160
|
-
|
160
|
+
void printBoard() {
|
161
|
-
|
161
|
+
for (int y = 0; y < 8; y++) {
|
162
|
-
|
162
|
+
for (int x = 0; x < 8; x++) {
|
163
|
-
|
163
|
+
System.out.printf("%2d ", ste[x][y].obverse);
|
164
|
-
|
164
|
+
}
|
165
|
-
|
165
|
+
System.out.println("");
|
166
|
-
|
166
|
+
}
|
167
|
-
|
167
|
+
}
|
168
168
|
|
169
|
-
|
169
|
+
void printEval() {
|
170
|
-
|
170
|
+
System.out.println("Black(1):");
|
171
|
-
|
171
|
+
for (int i = 0; i < 8; i++) {
|
172
|
-
|
172
|
+
for (int j = 0; j < 8; j++) {
|
173
|
-
|
173
|
+
System.out.printf("%2d ", eval_black[j][i]);
|
174
|
-
|
174
|
+
}
|
175
|
-
|
175
|
+
System.out.println("");
|
176
|
-
|
176
|
+
}
|
177
|
-
|
177
|
+
System.out.println("White(2):");
|
178
|
-
|
178
|
+
for (int i = 0; i < 8; i++) {
|
179
|
-
|
179
|
+
for (int j = 0; j < 8; j++) {
|
180
|
-
|
180
|
+
System.out.printf("%2d ", eval_white[j][i]);
|
181
|
-
|
181
|
+
}
|
182
|
-
|
182
|
+
System.out.println("");
|
183
|
-
|
183
|
+
}
|
184
|
-
|
184
|
+
}
|
185
185
|
|
186
|
-
|
186
|
+
void paint(Graphics g, int unit_size) {
|
187
187
|
|
188
|
-
|
188
|
+
g.setColor(Color.black);
|
189
|
-
|
189
|
+
g.fillRect(0, 0, unit_size * 10, unit_size * 10);
|
190
190
|
|
191
|
-
|
191
|
+
g.setColor(new Color(0, 85, 0));
|
192
|
-
|
192
|
+
g.fillRect(unit_size, unit_size, unit_size * 8, unit_size * 8);
|
193
193
|
|
194
|
-
|
194
|
+
g.setColor(Color.black);
|
195
|
-
|
195
|
+
for (int i = 0; i < 9; i++) {
|
196
|
-
|
196
|
+
g.drawLine(unit_size * (i + 1), unit_size, unit_size * (i + 1), unit_size * 9);
|
197
|
-
|
197
|
+
}
|
198
198
|
|
199
|
-
|
199
|
+
g.setColor(Color.black);
|
200
|
-
|
200
|
+
for (int i = 0; i < 9; i++) {
|
201
|
-
|
201
|
+
g.drawLine(unit_size, unit_size * (i + 1), unit_size * 9, unit_size * (i + 1));
|
202
|
-
|
202
|
+
}
|
203
203
|
|
204
|
-
|
204
|
+
for (int i = 0; i < 2; i++) {
|
205
|
-
|
205
|
+
for (int j = 0; j < 2; j++) {
|
206
|
-
|
206
|
+
g.fillRect(unit_size * (3 + 4 * i) - (unit_size / 16), unit_size * (3 + 4 * j) - (unit_size / 16),
|
207
|
-
|
207
|
+
(unit_size / 8), (unit_size / 8));
|
208
|
-
|
208
|
+
}
|
209
|
-
|
209
|
+
}
|
210
210
|
|
211
|
-
|
211
|
+
Point p = new Point();
|
212
|
-
|
212
|
+
int rad = ((unit_size / 2) * 4) / 5;
|
213
|
-
|
213
|
+
for (int x = 0; x < 8; x++) {
|
214
|
-
|
214
|
+
for (int y = 0; y < 8; y++) {
|
215
|
-
|
215
|
+
p.x = (unit_size * (x + 1) + unit_size * (x + 2)) / 2;
|
216
|
-
|
216
|
+
p.y = (unit_size * (y + 1) + unit_size * (y + 2)) / 2;
|
217
|
-
|
217
|
+
ste[x][y].paint(g, p, rad);
|
218
|
-
|
218
|
+
}
|
219
|
-
|
219
|
+
}
|
220
|
-
|
220
|
+
}
|
221
221
|
|
222
|
-
|
222
|
+
void setStone(int x, int y, int s) {
|
223
|
-
|
223
|
+
ste[x][y].setObverse(s);
|
224
|
-
|
224
|
+
}
|
225
225
|
}
|
226
226
|
|
227
227
|
public class Reversi extends JPanel {
|
228
|
-
|
228
|
+
private final static int UNIT_SIZE = 80;
|
229
|
-
|
229
|
+
private Board board = new Board();
|
230
230
|
|
231
|
-
|
231
|
+
private int turn = 1;
|
232
232
|
|
233
|
-
|
233
|
+
public Reversi() {
|
234
|
-
|
234
|
+
setPreferredSize(new Dimension(800, 800));
|
235
|
-
|
235
|
+
addMouseListener(new MouseProc());
|
236
|
-
|
236
|
+
}
|
237
237
|
|
238
|
-
|
238
|
+
public void paintComponent(Graphics g) {
|
239
|
-
|
239
|
+
board.paint(g, UNIT_SIZE);
|
240
|
-
|
240
|
+
}
|
241
241
|
|
242
|
-
|
242
|
+
void changeTurn() {
|
243
|
-
|
243
|
+
if (turn == 1)
|
244
|
-
|
244
|
+
turn = 2;
|
245
|
-
|
245
|
+
else if (turn == 2)
|
246
|
-
|
246
|
+
turn = 1;
|
247
|
-
|
247
|
+
}
|
248
248
|
|
249
|
-
|
249
|
+
void MessageDialog(String str) {
|
250
|
-
|
250
|
+
JOptionPane.showMessageDialog(this, str, "情報", JOptionPane.INFORMATION_MESSAGE);
|
251
|
-
|
251
|
+
}
|
252
252
|
|
253
|
-
|
253
|
+
void EndMessageDialog(int num_black, int num_white) {
|
254
|
-
|
254
|
+
if (num_black > num_white) {
|
255
|
-
|
255
|
+
String str = "[黒:" + num_black + ",白:" + num_white + "]で黒の勝ち";
|
256
|
-
|
256
|
+
JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
|
257
|
-
|
257
|
+
} else if (num_black == num_white) {
|
258
|
-
|
258
|
+
String str = "[黒:" + num_black + ",白:" + num_white + "]で引き分け";
|
259
|
-
|
259
|
+
JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
|
260
|
-
|
260
|
+
} else {
|
261
|
-
|
261
|
+
String str = "[黒:" + num_black + ",白:" + num_white + "]で白の勝ち";
|
262
|
-
|
262
|
+
JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
|
263
|
-
|
263
|
+
}
|
264
264
|
|
265
|
-
|
265
|
+
System.exit(0);
|
266
|
-
|
266
|
+
}
|
267
267
|
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
268
|
+
class MouseProc extends MouseAdapter {
|
269
|
+
public void mouseClicked(MouseEvent me) {
|
270
|
+
Point point = me.getPoint();
|
271
|
+
int btn = me.getButton();
|
272
|
+
System.out.println("(" + point.x + "," + point.y + ")");
|
273
|
+
board.evaluateBoard();
|
274
|
+
if (turn == 1 && btn == MouseEvent.BUTTON1) {
|
275
|
+
int x = point.x / UNIT_SIZE - 1;
|
276
|
+
int y = point.y / UNIT_SIZE - 1;
|
277
|
+
System.out.println("[" + x + "]" + "[" + y + "]");
|
278
|
+
if (0 <= x && x < 8 && 0 <= y && y < 8) {
|
279
|
+
if (board.eval_black[x][y] < 1) {
|
280
|
+
System.out.println("そこに石を置けません");
|
281
|
+
} else {
|
282
|
+
board.setStone(x, y, 1);
|
283
|
+
board.setStoneAndReverse(x, y, 1);
|
284
|
+
repaint();
|
285
|
+
board.printBoard();
|
286
|
+
board.evaluateBoard();
|
287
|
+
board.printEval();
|
288
|
+
changeTurn();
|
289
|
+
if (board.num_grid_black >= 1) {
|
290
|
+
MessageDialog("白の番です");
|
291
|
+
} else {
|
292
|
+
changeTurn();
|
293
|
+
MessageDialog("あなたはパスです");
|
294
|
+
}
|
295
|
+
}
|
296
|
+
}
|
297
|
+
} else if (turn == 2 && btn == MouseEvent.BUTTON3) {
|
298
|
+
int x = point.x / UNIT_SIZE - 1;
|
299
|
+
int y = point.y / UNIT_SIZE - 1;
|
300
|
+
System.out.println("[" + x + "]" + "[" + y + "]");
|
301
|
+
if (0 <= x && x < 8 && 0 <= y && y < 8) {
|
302
|
+
if (board.eval_white[x][y] < 1) {
|
303
|
+
System.out.println("そこに石を置けません");
|
304
|
+
} else {
|
305
|
+
board.setStone(x, y, 2);
|
306
|
+
board.setStoneAndReverse(x, y, 2);
|
307
|
+
repaint();
|
308
|
+
board.printBoard();
|
309
|
+
board.evaluateBoard();
|
310
|
+
board.printEval();
|
311
|
+
changeTurn();
|
312
|
+
if (board.num_grid_black >= 1) {
|
313
|
+
MessageDialog("黒の番です");
|
314
|
+
} else {
|
315
|
+
changeTurn();
|
316
|
+
MessageDialog("あなたはパスです");
|
317
|
+
}
|
318
|
+
}
|
319
|
+
}
|
320
|
+
}
|
321
321
|
|
322
|
-
|
322
|
+
int num_black = 0;
|
323
|
-
|
323
|
+
int num_white = 0;
|
324
|
-
|
324
|
+
for (int x = 0; x < 8; x++) {
|
325
|
-
|
325
|
+
for (int y = 0; y < 8; y++) {
|
326
|
-
|
326
|
+
if (board.ste[x][y].obverse == 1)
|
327
|
-
|
327
|
+
num_black++;
|
328
|
-
|
328
|
+
if (board.ste[x][y].obverse == 2)
|
329
|
-
|
329
|
+
num_white++;
|
330
|
-
|
330
|
+
}
|
331
|
-
|
331
|
+
}
|
332
332
|
|
333
|
-
|
333
|
+
if (board.num_grid_black < 1 && board.num_grid_white < 1) {
|
334
|
-
|
334
|
+
EndMessageDialog(num_black, num_white);
|
335
|
-
|
335
|
+
}
|
336
|
-
|
336
|
+
}
|
337
|
-
|
337
|
+
}
|
338
338
|
|
339
|
-
|
339
|
+
public static void main(String[] args) {
|
340
|
-
|
340
|
+
JFrame f = new JFrame();
|
341
341
|
|
342
|
-
|
342
|
+
f.getContentPane().setLayout(new FlowLayout());
|
343
|
-
|
343
|
+
f.getContentPane().add(new Reversi());
|
344
|
-
|
344
|
+
f.pack();
|
345
|
-
|
345
|
+
f.setResizable(false);
|
346
|
-
|
346
|
+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
347
|
-
|
347
|
+
f.setVisible(true);
|
348
|
-
|
348
|
+
}
|
349
349
|
}
|
350
350
|
```
|
3
正常動作するコードを追加
answer
CHANGED
@@ -13,4 +13,338 @@
|
|
13
13
|
ですね。
|
14
14
|
|
15
15
|
あとは、順番に関係なく石を置けてしまう問題があります。
|
16
|
-
パスする処理も変です。
|
16
|
+
パスする処理も変です。
|
17
|
+
|
18
|
+
```java
|
19
|
+
import java.awt.*;
|
20
|
+
import javax.swing.*;
|
21
|
+
import java.awt.event.*;
|
22
|
+
import java.util.*;
|
23
|
+
|
24
|
+
class Stone {
|
25
|
+
public final static int black = 1;
|
26
|
+
public final static int white = 2;
|
27
|
+
public int obverse = 0;
|
28
|
+
|
29
|
+
Stone() {
|
30
|
+
obverse = 0;
|
31
|
+
}
|
32
|
+
|
33
|
+
void setObverse(int color) {
|
34
|
+
if (color == black || color == white)
|
35
|
+
obverse = color;
|
36
|
+
else
|
37
|
+
System.out.println("黒か白でなければいけません");
|
38
|
+
}
|
39
|
+
|
40
|
+
void doReverse(int color) {
|
41
|
+
if (color == black)
|
42
|
+
obverse = white;
|
43
|
+
else if (color == white)
|
44
|
+
obverse = black;
|
45
|
+
else
|
46
|
+
System.out.println("黒か白でなければいけません");
|
47
|
+
}
|
48
|
+
|
49
|
+
void paint(Graphics g, Point p, int rad) {
|
50
|
+
if (obverse == black) {
|
51
|
+
g.setColor(Color.black);
|
52
|
+
g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
|
53
|
+
} else if (obverse == white) {
|
54
|
+
g.setColor(Color.white);
|
55
|
+
g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
class Board {
|
61
|
+
Stone[][] ste = new Stone[8][8];
|
62
|
+
public int num_grid_black;
|
63
|
+
public int num_grid_white;
|
64
|
+
private Point[] direction = new Point[8];
|
65
|
+
public int[][] eval_black = new int[8][8];
|
66
|
+
public int[][] eval_white = new int[8][8];
|
67
|
+
|
68
|
+
Board() {
|
69
|
+
|
70
|
+
for (int x = 0; x < 8; x++) {
|
71
|
+
for (int y = 0; y < 8; y++) {
|
72
|
+
ste[x][y] = new Stone();
|
73
|
+
}
|
74
|
+
}
|
75
|
+
ste[3][3].setObverse(1);
|
76
|
+
ste[3][4].setObverse(2);
|
77
|
+
ste[4][3].setObverse(2);
|
78
|
+
ste[4][4].setObverse(1);
|
79
|
+
|
80
|
+
direction[0] = new Point(1, 0);
|
81
|
+
direction[1] = new Point(1, 1);
|
82
|
+
direction[2] = new Point(0, 1);
|
83
|
+
direction[3] = new Point(-1, 1);
|
84
|
+
direction[4] = new Point(-1, 0);
|
85
|
+
direction[5] = new Point(-1, -1);
|
86
|
+
direction[6] = new Point(0, -1);
|
87
|
+
direction[7] = new Point(1, -1);
|
88
|
+
}
|
89
|
+
|
90
|
+
boolean isOnBoard(int x, int y) {
|
91
|
+
if (x < 0 || 7 < x || y < 0 || 7 < y)
|
92
|
+
return false;
|
93
|
+
else
|
94
|
+
return true;
|
95
|
+
}
|
96
|
+
|
97
|
+
ArrayList<Integer> getLine(int x, int y, Point d) {
|
98
|
+
ArrayList<Integer> line = new ArrayList<Integer>();
|
99
|
+
int cx = x + d.x;
|
100
|
+
int cy = y + d.y;
|
101
|
+
while (isOnBoard(cx, cy) && ste[cx][cy].obverse != 0) {
|
102
|
+
line.add(ste[cx][cy].obverse);
|
103
|
+
cx += d.x;
|
104
|
+
cy += d.y;
|
105
|
+
}
|
106
|
+
return line;
|
107
|
+
}
|
108
|
+
|
109
|
+
int countReverseStone(int x, int y, int s) {
|
110
|
+
if (ste[x][y].obverse != 0)
|
111
|
+
return -1;
|
112
|
+
int cnt = 0;
|
113
|
+
for (int d = 0; d < 8; d++) {
|
114
|
+
ArrayList<Integer> line = new ArrayList<Integer>();
|
115
|
+
line = getLine(x, y, direction[d]);
|
116
|
+
int n = 0;
|
117
|
+
while (n < line.size() && line.get(n) != s)
|
118
|
+
n++;
|
119
|
+
if (1 <= n && n < line.size())
|
120
|
+
cnt += n;
|
121
|
+
}
|
122
|
+
return cnt;
|
123
|
+
}
|
124
|
+
|
125
|
+
void setStoneAndReverse(int x, int y, int s) {
|
126
|
+
for (int d = 0; d < 8; d++) {
|
127
|
+
ArrayList<Integer> p = new ArrayList<Integer>();
|
128
|
+
p = getLine(x, y, direction[d]);
|
129
|
+
int cx = x;
|
130
|
+
int cy = y;
|
131
|
+
int n = 0;
|
132
|
+
|
133
|
+
while (n < p.size() - 1 && p.get(n) != s && ste[cx][cy].obverse == s) {
|
134
|
+
cx += direction[d].x;
|
135
|
+
cy += direction[d].y;
|
136
|
+
System.out.println(d);
|
137
|
+
System.out.println("cx=" + cx);
|
138
|
+
System.out.println("cy=" + cy);
|
139
|
+
ste[cx][cy].doReverse(ste[cx][cy].obverse);
|
140
|
+
n++;
|
141
|
+
}
|
142
|
+
}
|
143
|
+
}
|
144
|
+
|
145
|
+
void evaluateBoard() {
|
146
|
+
num_grid_black = 0;
|
147
|
+
num_grid_white = 0;
|
148
|
+
for (int i = 0; i < 8; i++) {
|
149
|
+
for (int j = 0; j < 8; j++) {
|
150
|
+
eval_black[i][j] = countReverseStone(i, j, 1);
|
151
|
+
if (eval_black[i][j] > 0)
|
152
|
+
num_grid_black++;
|
153
|
+
eval_white[i][j] = countReverseStone(i, j, 2);
|
154
|
+
if (eval_white[i][j] > 0)
|
155
|
+
num_grid_white++;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
void printBoard() {
|
161
|
+
for (int y = 0; y < 8; y++) {
|
162
|
+
for (int x = 0; x < 8; x++) {
|
163
|
+
System.out.printf("%2d ", ste[x][y].obverse);
|
164
|
+
}
|
165
|
+
System.out.println("");
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
void printEval() {
|
170
|
+
System.out.println("Black(1):");
|
171
|
+
for (int i = 0; i < 8; i++) {
|
172
|
+
for (int j = 0; j < 8; j++) {
|
173
|
+
System.out.printf("%2d ", eval_black[j][i]);
|
174
|
+
}
|
175
|
+
System.out.println("");
|
176
|
+
}
|
177
|
+
System.out.println("White(2):");
|
178
|
+
for (int i = 0; i < 8; i++) {
|
179
|
+
for (int j = 0; j < 8; j++) {
|
180
|
+
System.out.printf("%2d ", eval_white[j][i]);
|
181
|
+
}
|
182
|
+
System.out.println("");
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
void paint(Graphics g, int unit_size) {
|
187
|
+
|
188
|
+
g.setColor(Color.black);
|
189
|
+
g.fillRect(0, 0, unit_size * 10, unit_size * 10);
|
190
|
+
|
191
|
+
g.setColor(new Color(0, 85, 0));
|
192
|
+
g.fillRect(unit_size, unit_size, unit_size * 8, unit_size * 8);
|
193
|
+
|
194
|
+
g.setColor(Color.black);
|
195
|
+
for (int i = 0; i < 9; i++) {
|
196
|
+
g.drawLine(unit_size * (i + 1), unit_size, unit_size * (i + 1), unit_size * 9);
|
197
|
+
}
|
198
|
+
|
199
|
+
g.setColor(Color.black);
|
200
|
+
for (int i = 0; i < 9; i++) {
|
201
|
+
g.drawLine(unit_size, unit_size * (i + 1), unit_size * 9, unit_size * (i + 1));
|
202
|
+
}
|
203
|
+
|
204
|
+
for (int i = 0; i < 2; i++) {
|
205
|
+
for (int j = 0; j < 2; j++) {
|
206
|
+
g.fillRect(unit_size * (3 + 4 * i) - (unit_size / 16), unit_size * (3 + 4 * j) - (unit_size / 16),
|
207
|
+
(unit_size / 8), (unit_size / 8));
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
Point p = new Point();
|
212
|
+
int rad = ((unit_size / 2) * 4) / 5;
|
213
|
+
for (int x = 0; x < 8; x++) {
|
214
|
+
for (int y = 0; y < 8; y++) {
|
215
|
+
p.x = (unit_size * (x + 1) + unit_size * (x + 2)) / 2;
|
216
|
+
p.y = (unit_size * (y + 1) + unit_size * (y + 2)) / 2;
|
217
|
+
ste[x][y].paint(g, p, rad);
|
218
|
+
}
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
void setStone(int x, int y, int s) {
|
223
|
+
ste[x][y].setObverse(s);
|
224
|
+
}
|
225
|
+
}
|
226
|
+
|
227
|
+
public class Reversi extends JPanel {
|
228
|
+
private final static int UNIT_SIZE = 80;
|
229
|
+
private Board board = new Board();
|
230
|
+
|
231
|
+
private int turn = 1;
|
232
|
+
|
233
|
+
public Reversi() {
|
234
|
+
setPreferredSize(new Dimension(800, 800));
|
235
|
+
addMouseListener(new MouseProc());
|
236
|
+
}
|
237
|
+
|
238
|
+
public void paintComponent(Graphics g) {
|
239
|
+
board.paint(g, UNIT_SIZE);
|
240
|
+
}
|
241
|
+
|
242
|
+
void changeTurn() {
|
243
|
+
if (turn == 1)
|
244
|
+
turn = 2;
|
245
|
+
else if (turn == 2)
|
246
|
+
turn = 1;
|
247
|
+
}
|
248
|
+
|
249
|
+
void MessageDialog(String str) {
|
250
|
+
JOptionPane.showMessageDialog(this, str, "情報", JOptionPane.INFORMATION_MESSAGE);
|
251
|
+
}
|
252
|
+
|
253
|
+
void EndMessageDialog(int num_black, int num_white) {
|
254
|
+
if (num_black > num_white) {
|
255
|
+
String str = "[黒:" + num_black + ",白:" + num_white + "]で黒の勝ち";
|
256
|
+
JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
|
257
|
+
} else if (num_black == num_white) {
|
258
|
+
String str = "[黒:" + num_black + ",白:" + num_white + "]で引き分け";
|
259
|
+
JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
|
260
|
+
} else {
|
261
|
+
String str = "[黒:" + num_black + ",白:" + num_white + "]で白の勝ち";
|
262
|
+
JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
|
263
|
+
}
|
264
|
+
|
265
|
+
System.exit(0);
|
266
|
+
}
|
267
|
+
|
268
|
+
class MouseProc extends MouseAdapter {
|
269
|
+
public void mouseClicked(MouseEvent me) {
|
270
|
+
Point point = me.getPoint();
|
271
|
+
int btn = me.getButton();
|
272
|
+
System.out.println("(" + point.x + "," + point.y + ")");
|
273
|
+
board.evaluateBoard();
|
274
|
+
if (turn == 1 && btn == MouseEvent.BUTTON1) {
|
275
|
+
int x = point.x / UNIT_SIZE - 1;
|
276
|
+
int y = point.y / UNIT_SIZE - 1;
|
277
|
+
System.out.println("[" + x + "]" + "[" + y + "]");
|
278
|
+
if (0 <= x && x < 8 && 0 <= y && y < 8) {
|
279
|
+
if (board.eval_black[x][y] < 1) {
|
280
|
+
System.out.println("そこに石を置けません");
|
281
|
+
} else {
|
282
|
+
board.setStone(x, y, 1);
|
283
|
+
board.setStoneAndReverse(x, y, 1);
|
284
|
+
repaint();
|
285
|
+
board.printBoard();
|
286
|
+
board.evaluateBoard();
|
287
|
+
board.printEval();
|
288
|
+
changeTurn();
|
289
|
+
if (board.num_grid_black >= 1) {
|
290
|
+
MessageDialog("白の番です");
|
291
|
+
} else {
|
292
|
+
changeTurn();
|
293
|
+
MessageDialog("あなたはパスです");
|
294
|
+
}
|
295
|
+
}
|
296
|
+
}
|
297
|
+
} else if (turn == 2 && btn == MouseEvent.BUTTON3) {
|
298
|
+
int x = point.x / UNIT_SIZE - 1;
|
299
|
+
int y = point.y / UNIT_SIZE - 1;
|
300
|
+
System.out.println("[" + x + "]" + "[" + y + "]");
|
301
|
+
if (0 <= x && x < 8 && 0 <= y && y < 8) {
|
302
|
+
if (board.eval_white[x][y] < 1) {
|
303
|
+
System.out.println("そこに石を置けません");
|
304
|
+
} else {
|
305
|
+
board.setStone(x, y, 2);
|
306
|
+
board.setStoneAndReverse(x, y, 2);
|
307
|
+
repaint();
|
308
|
+
board.printBoard();
|
309
|
+
board.evaluateBoard();
|
310
|
+
board.printEval();
|
311
|
+
changeTurn();
|
312
|
+
if (board.num_grid_black >= 1) {
|
313
|
+
MessageDialog("黒の番です");
|
314
|
+
} else {
|
315
|
+
changeTurn();
|
316
|
+
MessageDialog("あなたはパスです");
|
317
|
+
}
|
318
|
+
}
|
319
|
+
}
|
320
|
+
}
|
321
|
+
|
322
|
+
int num_black = 0;
|
323
|
+
int num_white = 0;
|
324
|
+
for (int x = 0; x < 8; x++) {
|
325
|
+
for (int y = 0; y < 8; y++) {
|
326
|
+
if (board.ste[x][y].obverse == 1)
|
327
|
+
num_black++;
|
328
|
+
if (board.ste[x][y].obverse == 2)
|
329
|
+
num_white++;
|
330
|
+
}
|
331
|
+
}
|
332
|
+
|
333
|
+
if (board.num_grid_black < 1 && board.num_grid_white < 1) {
|
334
|
+
EndMessageDialog(num_black, num_white);
|
335
|
+
}
|
336
|
+
}
|
337
|
+
}
|
338
|
+
|
339
|
+
public static void main(String[] args) {
|
340
|
+
JFrame f = new JFrame();
|
341
|
+
|
342
|
+
f.getContentPane().setLayout(new FlowLayout());
|
343
|
+
f.getContentPane().add(new Reversi());
|
344
|
+
f.pack();
|
345
|
+
f.setResizable(false);
|
346
|
+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
347
|
+
f.setVisible(true);
|
348
|
+
}
|
349
|
+
}
|
350
|
+
```
|
2
問題点追加
answer
CHANGED
@@ -12,4 +12,5 @@
|
|
12
12
|
|
13
13
|
ですね。
|
14
14
|
|
15
|
-
あとは、順番に関係なく石を置けてしまう問題があります。
|
15
|
+
あとは、順番に関係なく石を置けてしまう問題があります。
|
16
|
+
パスする処理も変です。
|
1
問題点指摘追加
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
まずは、ここ
|
2
2
|
|
3
3
|
```java
|
4
4
|
line.add(ste[cy][cx].obverse);
|
@@ -10,4 +10,6 @@
|
|
10
10
|
line.add(ste[cx][cy].obverse);
|
11
11
|
```
|
12
12
|
|
13
|
-
ですね。
|
13
|
+
ですね。
|
14
|
+
|
15
|
+
あとは、順番に関係なく石を置けてしまう問題があります。
|