質問するログイン新規登録

回答編集履歴

5

説明文追加

2019/11/27 17:24

投稿

shiracamus
shiracamus

スコア5406

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

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

2019/11/27 17:24

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -22,329 +22,329 @@
22
22
  import java.util.*;
23
23
 
24
24
  class Stone {
25
- public final static int black = 1;
25
+ public final static int black = 1;
26
- public final static int white = 2;
26
+ public final static int white = 2;
27
- public int obverse = 0;
27
+ public int obverse = 0;
28
28
 
29
- Stone() {
29
+ Stone() {
30
- obverse = 0;
30
+ obverse = 0;
31
- }
31
+ }
32
32
 
33
- void setObverse(int color) {
33
+ void setObverse(int color) {
34
- if (color == black || color == white)
34
+ if (color == black || color == white)
35
- obverse = color;
35
+ obverse = color;
36
- else
36
+ else
37
- System.out.println("黒か白でなければいけません");
37
+ System.out.println("黒か白でなければいけません");
38
- }
38
+ }
39
39
 
40
- void doReverse(int color) {
40
+ void doReverse(int color) {
41
- if (color == black)
41
+ if (color == black)
42
- obverse = white;
42
+ obverse = white;
43
- else if (color == white)
43
+ else if (color == white)
44
- obverse = black;
44
+ obverse = black;
45
- else
45
+ else
46
- System.out.println("黒か白でなければいけません");
46
+ System.out.println("黒か白でなければいけません");
47
- }
47
+ }
48
48
 
49
- void paint(Graphics g, Point p, int rad) {
49
+ void paint(Graphics g, Point p, int rad) {
50
- if (obverse == black) {
50
+ if (obverse == black) {
51
- g.setColor(Color.black);
51
+ g.setColor(Color.black);
52
- g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
52
+ g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
53
- } else if (obverse == white) {
53
+ } else if (obverse == white) {
54
- g.setColor(Color.white);
54
+ g.setColor(Color.white);
55
- g.fillOval(p.x - 32, p.y - 32, 2 * rad, 2 * rad);
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
- Stone[][] ste = new Stone[8][8];
61
+ Stone[][] ste = new Stone[8][8];
62
- public int num_grid_black;
62
+ public int num_grid_black;
63
- public int num_grid_white;
63
+ public int num_grid_white;
64
- private Point[] direction = new Point[8];
64
+ private Point[] direction = new Point[8];
65
- public int[][] eval_black = new int[8][8];
65
+ public int[][] eval_black = new int[8][8];
66
- public int[][] eval_white = new int[8][8];
66
+ public int[][] eval_white = new int[8][8];
67
67
 
68
- Board() {
68
+ Board() {
69
69
 
70
- for (int x = 0; x < 8; x++) {
70
+ for (int x = 0; x < 8; x++) {
71
- for (int y = 0; y < 8; y++) {
71
+ for (int y = 0; y < 8; y++) {
72
- ste[x][y] = new Stone();
72
+ ste[x][y] = new Stone();
73
- }
73
+ }
74
- }
74
+ }
75
- ste[3][3].setObverse(1);
75
+ ste[3][3].setObverse(1);
76
- ste[3][4].setObverse(2);
76
+ ste[3][4].setObverse(2);
77
- ste[4][3].setObverse(2);
77
+ ste[4][3].setObverse(2);
78
- ste[4][4].setObverse(1);
78
+ ste[4][4].setObverse(1);
79
79
 
80
- direction[0] = new Point(1, 0);
80
+ direction[0] = new Point(1, 0);
81
- direction[1] = new Point(1, 1);
81
+ direction[1] = new Point(1, 1);
82
- direction[2] = new Point(0, 1);
82
+ direction[2] = new Point(0, 1);
83
- direction[3] = new Point(-1, 1);
83
+ direction[3] = new Point(-1, 1);
84
- direction[4] = new Point(-1, 0);
84
+ direction[4] = new Point(-1, 0);
85
- direction[5] = new Point(-1, -1);
85
+ direction[5] = new Point(-1, -1);
86
- direction[6] = new Point(0, -1);
86
+ direction[6] = new Point(0, -1);
87
- direction[7] = new Point(1, -1);
87
+ direction[7] = new Point(1, -1);
88
- }
88
+ }
89
89
 
90
- boolean isOnBoard(int x, int y) {
90
+ boolean isOnBoard(int x, int y) {
91
- if (x < 0 || 7 < x || y < 0 || 7 < y)
91
+ if (x < 0 || 7 < x || y < 0 || 7 < y)
92
- return false;
92
+ return false;
93
- else
93
+ else
94
- return true;
94
+ return true;
95
- }
95
+ }
96
96
 
97
- ArrayList<Integer> getLine(int x, int y, Point d) {
97
+ ArrayList<Integer> getLine(int x, int y, Point d) {
98
- ArrayList<Integer> line = new ArrayList<Integer>();
98
+ ArrayList<Integer> line = new ArrayList<Integer>();
99
- int cx = x + d.x;
99
+ int cx = x + d.x;
100
- int cy = y + d.y;
100
+ int cy = y + d.y;
101
- while (isOnBoard(cx, cy) && ste[cx][cy].obverse != 0) {
101
+ while (isOnBoard(cx, cy) && ste[cx][cy].obverse != 0) {
102
- line.add(ste[cx][cy].obverse);
102
+ line.add(ste[cx][cy].obverse);
103
- cx += d.x;
103
+ cx += d.x;
104
- cy += d.y;
104
+ cy += d.y;
105
- }
105
+ }
106
- return line;
106
+ return line;
107
- }
107
+ }
108
108
 
109
- int countReverseStone(int x, int y, int s) {
109
+ int countReverseStone(int x, int y, int s) {
110
- if (ste[x][y].obverse != 0)
110
+ if (ste[x][y].obverse != 0)
111
- return -1;
111
+ return -1;
112
- int cnt = 0;
112
+ int cnt = 0;
113
- for (int d = 0; d < 8; d++) {
113
+ for (int d = 0; d < 8; d++) {
114
- ArrayList<Integer> line = new ArrayList<Integer>();
114
+ ArrayList<Integer> line = new ArrayList<Integer>();
115
- line = getLine(x, y, direction[d]);
115
+ line = getLine(x, y, direction[d]);
116
- int n = 0;
116
+ int n = 0;
117
- while (n < line.size() && line.get(n) != s)
117
+ while (n < line.size() && line.get(n) != s)
118
- n++;
118
+ n++;
119
- if (1 <= n && n < line.size())
119
+ if (1 <= n && n < line.size())
120
- cnt += n;
120
+ cnt += n;
121
- }
121
+ }
122
- return cnt;
122
+ return cnt;
123
- }
123
+ }
124
124
 
125
- void setStoneAndReverse(int x, int y, int s) {
125
+ void setStoneAndReverse(int x, int y, int s) {
126
- for (int d = 0; d < 8; d++) {
126
+ for (int d = 0; d < 8; d++) {
127
- ArrayList<Integer> p = new ArrayList<Integer>();
127
+ ArrayList<Integer> p = new ArrayList<Integer>();
128
- p = getLine(x, y, direction[d]);
128
+ p = getLine(x, y, direction[d]);
129
- int cx = x;
129
+ int cx = x;
130
- int cy = y;
130
+ int cy = y;
131
- int n = 0;
131
+ int n = 0;
132
132
 
133
- while (n < p.size() - 1 && p.get(n) != s && ste[cx][cy].obverse == s) {
133
+ while (n < p.size() - 1 && p.get(n) != s && ste[cx][cy].obverse == s) {
134
- cx += direction[d].x;
134
+ cx += direction[d].x;
135
- cy += direction[d].y;
135
+ cy += direction[d].y;
136
- System.out.println(d);
136
+ System.out.println(d);
137
- System.out.println("cx=" + cx);
137
+ System.out.println("cx=" + cx);
138
- System.out.println("cy=" + cy);
138
+ System.out.println("cy=" + cy);
139
- ste[cx][cy].doReverse(ste[cx][cy].obverse);
139
+ ste[cx][cy].doReverse(ste[cx][cy].obverse);
140
- n++;
140
+ n++;
141
- }
141
+ }
142
- }
142
+ }
143
- }
143
+ }
144
144
 
145
- void evaluateBoard() {
145
+ void evaluateBoard() {
146
- num_grid_black = 0;
146
+ num_grid_black = 0;
147
- num_grid_white = 0;
147
+ num_grid_white = 0;
148
- for (int i = 0; i < 8; i++) {
148
+ for (int i = 0; i < 8; i++) {
149
- for (int j = 0; j < 8; j++) {
149
+ for (int j = 0; j < 8; j++) {
150
- eval_black[i][j] = countReverseStone(i, j, 1);
150
+ eval_black[i][j] = countReverseStone(i, j, 1);
151
- if (eval_black[i][j] > 0)
151
+ if (eval_black[i][j] > 0)
152
- num_grid_black++;
152
+ num_grid_black++;
153
- eval_white[i][j] = countReverseStone(i, j, 2);
153
+ eval_white[i][j] = countReverseStone(i, j, 2);
154
- if (eval_white[i][j] > 0)
154
+ if (eval_white[i][j] > 0)
155
- num_grid_white++;
155
+ num_grid_white++;
156
- }
156
+ }
157
- }
157
+ }
158
- }
158
+ }
159
159
 
160
- void printBoard() {
160
+ void printBoard() {
161
- for (int y = 0; y < 8; y++) {
161
+ for (int y = 0; y < 8; y++) {
162
- for (int x = 0; x < 8; x++) {
162
+ for (int x = 0; x < 8; x++) {
163
- System.out.printf("%2d ", ste[x][y].obverse);
163
+ System.out.printf("%2d ", ste[x][y].obverse);
164
- }
164
+ }
165
- System.out.println("");
165
+ System.out.println("");
166
- }
166
+ }
167
- }
167
+ }
168
168
 
169
- void printEval() {
169
+ void printEval() {
170
- System.out.println("Black(1):");
170
+ System.out.println("Black(1):");
171
- for (int i = 0; i < 8; i++) {
171
+ for (int i = 0; i < 8; i++) {
172
- for (int j = 0; j < 8; j++) {
172
+ for (int j = 0; j < 8; j++) {
173
- System.out.printf("%2d ", eval_black[j][i]);
173
+ System.out.printf("%2d ", eval_black[j][i]);
174
- }
174
+ }
175
- System.out.println("");
175
+ System.out.println("");
176
- }
176
+ }
177
- System.out.println("White(2):");
177
+ System.out.println("White(2):");
178
- for (int i = 0; i < 8; i++) {
178
+ for (int i = 0; i < 8; i++) {
179
- for (int j = 0; j < 8; j++) {
179
+ for (int j = 0; j < 8; j++) {
180
- System.out.printf("%2d ", eval_white[j][i]);
180
+ System.out.printf("%2d ", eval_white[j][i]);
181
- }
181
+ }
182
- System.out.println("");
182
+ System.out.println("");
183
- }
183
+ }
184
- }
184
+ }
185
185
 
186
- void paint(Graphics g, int unit_size) {
186
+ void paint(Graphics g, int unit_size) {
187
187
 
188
- g.setColor(Color.black);
188
+ g.setColor(Color.black);
189
- g.fillRect(0, 0, unit_size * 10, unit_size * 10);
189
+ g.fillRect(0, 0, unit_size * 10, unit_size * 10);
190
190
 
191
- g.setColor(new Color(0, 85, 0));
191
+ g.setColor(new Color(0, 85, 0));
192
- g.fillRect(unit_size, unit_size, unit_size * 8, unit_size * 8);
192
+ g.fillRect(unit_size, unit_size, unit_size * 8, unit_size * 8);
193
193
 
194
- g.setColor(Color.black);
194
+ g.setColor(Color.black);
195
- for (int i = 0; i < 9; i++) {
195
+ for (int i = 0; i < 9; i++) {
196
- g.drawLine(unit_size * (i + 1), unit_size, unit_size * (i + 1), unit_size * 9);
196
+ g.drawLine(unit_size * (i + 1), unit_size, unit_size * (i + 1), unit_size * 9);
197
- }
197
+ }
198
198
 
199
- g.setColor(Color.black);
199
+ g.setColor(Color.black);
200
- for (int i = 0; i < 9; i++) {
200
+ for (int i = 0; i < 9; i++) {
201
- g.drawLine(unit_size, unit_size * (i + 1), unit_size * 9, unit_size * (i + 1));
201
+ g.drawLine(unit_size, unit_size * (i + 1), unit_size * 9, unit_size * (i + 1));
202
- }
202
+ }
203
203
 
204
- for (int i = 0; i < 2; i++) {
204
+ for (int i = 0; i < 2; i++) {
205
- for (int j = 0; j < 2; j++) {
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),
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));
207
+ (unit_size / 8), (unit_size / 8));
208
- }
208
+ }
209
- }
209
+ }
210
210
 
211
- Point p = new Point();
211
+ Point p = new Point();
212
- int rad = ((unit_size / 2) * 4) / 5;
212
+ int rad = ((unit_size / 2) * 4) / 5;
213
- for (int x = 0; x < 8; x++) {
213
+ for (int x = 0; x < 8; x++) {
214
- for (int y = 0; y < 8; y++) {
214
+ for (int y = 0; y < 8; y++) {
215
- p.x = (unit_size * (x + 1) + unit_size * (x + 2)) / 2;
215
+ p.x = (unit_size * (x + 1) + unit_size * (x + 2)) / 2;
216
- p.y = (unit_size * (y + 1) + unit_size * (y + 2)) / 2;
216
+ p.y = (unit_size * (y + 1) + unit_size * (y + 2)) / 2;
217
- ste[x][y].paint(g, p, rad);
217
+ ste[x][y].paint(g, p, rad);
218
- }
218
+ }
219
- }
219
+ }
220
- }
220
+ }
221
221
 
222
- void setStone(int x, int y, int s) {
222
+ void setStone(int x, int y, int s) {
223
- ste[x][y].setObverse(s);
223
+ ste[x][y].setObverse(s);
224
- }
224
+ }
225
225
  }
226
226
 
227
227
  public class Reversi extends JPanel {
228
- private final static int UNIT_SIZE = 80;
228
+ private final static int UNIT_SIZE = 80;
229
- private Board board = new Board();
229
+ private Board board = new Board();
230
230
 
231
- private int turn = 1;
231
+ private int turn = 1;
232
232
 
233
- public Reversi() {
233
+ public Reversi() {
234
- setPreferredSize(new Dimension(800, 800));
234
+ setPreferredSize(new Dimension(800, 800));
235
- addMouseListener(new MouseProc());
235
+ addMouseListener(new MouseProc());
236
- }
236
+ }
237
237
 
238
- public void paintComponent(Graphics g) {
238
+ public void paintComponent(Graphics g) {
239
- board.paint(g, UNIT_SIZE);
239
+ board.paint(g, UNIT_SIZE);
240
- }
240
+ }
241
241
 
242
- void changeTurn() {
242
+ void changeTurn() {
243
- if (turn == 1)
243
+ if (turn == 1)
244
- turn = 2;
244
+ turn = 2;
245
- else if (turn == 2)
245
+ else if (turn == 2)
246
- turn = 1;
246
+ turn = 1;
247
- }
247
+ }
248
248
 
249
- void MessageDialog(String str) {
249
+ void MessageDialog(String str) {
250
- JOptionPane.showMessageDialog(this, str, "情報", JOptionPane.INFORMATION_MESSAGE);
250
+ JOptionPane.showMessageDialog(this, str, "情報", JOptionPane.INFORMATION_MESSAGE);
251
- }
251
+ }
252
252
 
253
- void EndMessageDialog(int num_black, int num_white) {
253
+ void EndMessageDialog(int num_black, int num_white) {
254
- if (num_black > num_white) {
254
+ if (num_black > num_white) {
255
- String str = "[黒:" + num_black + ",白:" + num_white + "]で黒の勝ち";
255
+ String str = "[黒:" + num_black + ",白:" + num_white + "]で黒の勝ち";
256
- JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
256
+ JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
257
- } else if (num_black == num_white) {
257
+ } else if (num_black == num_white) {
258
- String str = "[黒:" + num_black + ",白:" + num_white + "]で引き分け";
258
+ String str = "[黒:" + num_black + ",白:" + num_white + "]で引き分け";
259
- JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
259
+ JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
260
- } else {
260
+ } else {
261
- String str = "[黒:" + num_black + ",白:" + num_white + "]で白の勝ち";
261
+ String str = "[黒:" + num_black + ",白:" + num_white + "]で白の勝ち";
262
- JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
262
+ JOptionPane.showMessageDialog(this, str, "ゲーム終了", JOptionPane.INFORMATION_MESSAGE);
263
- }
263
+ }
264
264
 
265
- System.exit(0);
265
+ System.exit(0);
266
- }
266
+ }
267
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
- }
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
- int num_black = 0;
322
+ int num_black = 0;
323
- int num_white = 0;
323
+ int num_white = 0;
324
- for (int x = 0; x < 8; x++) {
324
+ for (int x = 0; x < 8; x++) {
325
- for (int y = 0; y < 8; y++) {
325
+ for (int y = 0; y < 8; y++) {
326
- if (board.ste[x][y].obverse == 1)
326
+ if (board.ste[x][y].obverse == 1)
327
- num_black++;
327
+ num_black++;
328
- if (board.ste[x][y].obverse == 2)
328
+ if (board.ste[x][y].obverse == 2)
329
- num_white++;
329
+ num_white++;
330
- }
330
+ }
331
- }
331
+ }
332
332
 
333
- if (board.num_grid_black < 1 && board.num_grid_white < 1) {
333
+ if (board.num_grid_black < 1 && board.num_grid_white < 1) {
334
- EndMessageDialog(num_black, num_white);
334
+ EndMessageDialog(num_black, num_white);
335
- }
335
+ }
336
- }
336
+ }
337
- }
337
+ }
338
338
 
339
- public static void main(String[] args) {
339
+ public static void main(String[] args) {
340
- JFrame f = new JFrame();
340
+ JFrame f = new JFrame();
341
341
 
342
- f.getContentPane().setLayout(new FlowLayout());
342
+ f.getContentPane().setLayout(new FlowLayout());
343
- f.getContentPane().add(new Reversi());
343
+ f.getContentPane().add(new Reversi());
344
- f.pack();
344
+ f.pack();
345
- f.setResizable(false);
345
+ f.setResizable(false);
346
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
346
+ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
347
- f.setVisible(true);
347
+ f.setVisible(true);
348
- }
348
+ }
349
349
  }
350
350
  ```

3

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

2019/11/27 17:21

投稿

shiracamus
shiracamus

スコア5406

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

問題点追加

2019/11/27 17:17

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -12,4 +12,5 @@
12
12
 
13
13
  ですね。
14
14
 
15
- あとは、順番に関係なく石を置けてしまう問題があります。
15
+ あとは、順番に関係なく石を置けてしまう問題があります。
16
+ パスする処理も変です。

1

問題点指摘追加

2019/11/27 02:21

投稿

shiracamus
shiracamus

スコア5406

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
+ あとは、順番に関係なく石を置けてしまう問題があります。