回答編集履歴

4

コード修正

2022/06/04 18:26

投稿

jimbe
jimbe

スコア12760

test CHANGED
@@ -10,7 +10,7 @@
10
10
  import javafx.scene.text.Font;
11
11
  import javafx.stage.Stage;
12
12
 
13
- public class RollDie extends Application {
13
+ public class RollDice extends Application {
14
14
  static final int START_X = 300, START_Y = 150, ROLL_Y = 125;
15
15
  static final int[] XS = { START_X / 2, START_X + 150 };
16
16
 
@@ -42,14 +42,12 @@
42
42
  private static final int DOT_1 = DOT_SIZE / 2;
43
43
  private static final int DOT_2 = SIZE / 2 - DOT_SIZE / 2;
44
44
  private static final int DOT_3 = SIZE - DOT_SIZE - DOT_SIZE / 2;
45
-
45
+ //dx,dy
46
- private static final int[][] POINTS_OF_DOT = {
46
+ private static final int[][] POINTS = {
47
- { DOT_2, DOT_2 }, //x,y
47
+ { DOT_2, DOT_2 },
48
- { DOT_1, DOT_1 }, { DOT_1, DOT_2 }, { DOT_1, DOT_3 },
48
+ { DOT_1, DOT_1 }, { DOT_3, DOT_3 },
49
+ { DOT_1, DOT_3 }, { DOT_3, DOT_1 },
49
- { DOT_3, DOT_1 }, { DOT_3, DOT_2 }, { DOT_3, DOT_3 }
50
+ { DOT_1, DOT_2 }, { DOT_3, DOT_2 }
50
- };
51
- private static final int[] DOTS_OF_VALUE = {
52
- 0, 1, 6, 3, 4, 2, 5
53
51
  };
54
52
 
55
53
  static Dice[] rolls(int n, Random random) {
@@ -70,10 +68,7 @@
70
68
 
71
69
  gc.setFill(Color.BLACK);
72
70
  for(int i=1-value%2, end=i+value; i<end; i++) {
73
- int j = DOTS_OF_VALUE[i];
74
- int dx = POINTS_OF_DOT[j][0];
75
- int dy = POINTS_OF_DOT[j][1];
76
- gc.fillOval(x + dx, y + dy, DOT_SIZE, DOT_SIZE);
71
+ gc.fillOval(x + POINTS[i][0], y + POINTS[i][1], DOT_SIZE, DOT_SIZE);
77
72
  }
78
73
  }
79
74
  }

3

コード変数名変更等

2022/06/04 18:05

投稿

jimbe
jimbe

スコア12760

test CHANGED
@@ -43,12 +43,12 @@
43
43
  private static final int DOT_2 = SIZE / 2 - DOT_SIZE / 2;
44
44
  private static final int DOT_3 = SIZE - DOT_SIZE - DOT_SIZE / 2;
45
45
 
46
- private static final int[][] pointsOfDot = {
46
+ private static final int[][] POINTS_OF_DOT = {
47
47
  { DOT_2, DOT_2 }, //x,y
48
48
  { DOT_1, DOT_1 }, { DOT_1, DOT_2 }, { DOT_1, DOT_3 },
49
49
  { DOT_3, DOT_1 }, { DOT_3, DOT_2 }, { DOT_3, DOT_3 }
50
50
  };
51
- private static final int[] dotsOfValue = {
51
+ private static final int[] DOTS_OF_VALUE = {
52
52
  0, 1, 6, 3, 4, 2, 5
53
53
  };
54
54
 
@@ -69,10 +69,10 @@
69
69
  gc.fillRect(x, y, SIZE, SIZE);
70
70
 
71
71
  gc.setFill(Color.BLACK);
72
- for(int i=0, j=1-value%2; i<value; i++, j++) {
72
+ for(int i=1-value%2, end=i+value; i<end; i++) {
73
- int k = dotsOfValue[j];
73
+ int j = DOTS_OF_VALUE[i];
74
- int dx = pointsOfDot[k][0];
74
+ int dx = POINTS_OF_DOT[j][0];
75
- int dy = pointsOfDot[k][1];
75
+ int dy = POINTS_OF_DOT[j][1];
76
76
  gc.fillOval(x + dx, y + dy, DOT_SIZE, DOT_SIZE);
77
77
  }
78
78
  }

2

コード修正

2022/06/04 17:56

投稿

jimbe
jimbe

スコア12760

test CHANGED
@@ -11,23 +11,25 @@
11
11
  import javafx.stage.Stage;
12
12
 
13
13
  public class RollDie extends Application {
14
- final int START_X = 300, START_Y = 150, ROLL_Y = 125;
14
+ static final int START_X = 300, START_Y = 150, ROLL_Y = 125;
15
+ static final int[] XS = { START_X / 2, START_X + 150 };
15
16
 
16
17
  @Override
17
18
  public void start(Stage stage) {
18
- GraphicsContext gc = JIGraphicsUtility.setUpGraphics(stage, "Roll a Die", 700, 400);
19
+ GraphicsContext gc = JIGraphicsUtility.setUpGraphics(stage, "Roll a Dice", 700, 400);
19
20
 
20
21
  Random random = new Random();
21
- Dice dice1 = new Dice(random.nextInt(6) + 1);
22
+ Dice[] dices = Dice.rolls(XS.length, random);
22
- Dice dice2 = new Dice(random.nextInt(6) + 1);
23
23
 
24
+ int total = 0;
24
- dice1.draw(gc, START_X / 2, START_Y);
25
+ for(int i=0; i<XS.length; i++) {
25
- dice2.draw(gc, START_X + 150, START_Y);
26
+ dices[i].draw(gc, XS[i], START_Y);
27
+ total += dices[i].value;
28
+ }
26
29
 
27
- int t = dice1.roll + dice2.roll;
28
30
  Font largeFont = new Font(20);
29
31
  gc.setFont(largeFont);
30
- gc.fillText("The roll is " + t, START_X, ROLL_Y);
32
+ gc.fillText("The roll is " + total, START_X, ROLL_Y);
31
33
  }
32
34
 
33
35
  public static void main(String[] args) {
@@ -46,19 +48,20 @@
46
48
  { DOT_1, DOT_1 }, { DOT_1, DOT_2 }, { DOT_1, DOT_3 },
47
49
  { DOT_3, DOT_1 }, { DOT_3, DOT_2 }, { DOT_3, DOT_3 }
48
50
  };
49
- private static final int[][] dotsOfRoll = {
51
+ private static final int[] dotsOfValue = {
50
- { 0, -1 },
51
- { 1, 6, -1 },
52
- { 0, 1, 6, -1 },
53
- { 1, 3, 4, 6, -1 },
54
- { 0, 1, 3, 4, 6, -1 },
52
+ 0, 1, 6, 3, 4, 2, 5
55
- { 1, 2, 3, 4, 5, 6, -1 }
56
53
  };
57
54
 
55
+ static Dice[] rolls(int n, Random random) {
56
+ if(n <= 0) throw new IllegalArgumentException("n="+n);
57
+ Dice[] dices = new Dice[n];
58
+ for(int i=0; i<n; i++) dices[i] = new Dice(random.nextInt(6) + 1);
58
- final int roll;
59
+ return dices;
60
+ }
59
61
 
62
+ final int value;
60
- Dice(int roll) {
63
+ private Dice(int value) {
61
- this.roll = roll;
64
+ this.value = value;
62
65
  }
63
66
 
64
67
  void draw(GraphicsContext gc, int x, int y) {
@@ -66,10 +69,10 @@
66
69
  gc.fillRect(x, y, SIZE, SIZE);
67
70
 
68
71
  gc.setFill(Color.BLACK);
72
+ for(int i=0, j=1-value%2; i<value; i++, j++) {
69
- int[] pi = dotsOfRoll[roll - 1];
73
+ int k = dotsOfValue[j];
70
- for(int i=0; pi[i] != -1; i++) {
71
- int dx = pointsOfDot[pi[i]][0];
74
+ int dx = pointsOfDot[k][0];
72
- int dy = pointsOfDot[pi[i]][1];
75
+ int dy = pointsOfDot[k][1];
73
76
  gc.fillOval(x + dx, y + dy, DOT_SIZE, DOT_SIZE);
74
77
  }
75
78
  }

1

コード追加

2022/06/04 16:53

投稿

jimbe
jimbe

スコア12760

test CHANGED
@@ -1 +1,77 @@
1
1
  少なくとも(?) 二つの switch はメソッド化すれば 1 つになりますし、"サイコロ" という単位でクラス化することは(例え行数が多少増えても)オブジェクト指向としては意味があると思います。
2
+
3
+ **※以下のコードは実行確認していません。**
4
+ ```java
5
+ import java.util.Random;
6
+
7
+ import javafx.application.Application;
8
+ import javafx.scene.canvas.GraphicsContext;
9
+ import javafx.scene.paint.Color;
10
+ import javafx.scene.text.Font;
11
+ import javafx.stage.Stage;
12
+
13
+ public class RollDie extends Application {
14
+ final int START_X = 300, START_Y = 150, ROLL_Y = 125;
15
+
16
+ @Override
17
+ public void start(Stage stage) {
18
+ GraphicsContext gc = JIGraphicsUtility.setUpGraphics(stage, "Roll a Die", 700, 400);
19
+
20
+ Random random = new Random();
21
+ Dice dice1 = new Dice(random.nextInt(6) + 1);
22
+ Dice dice2 = new Dice(random.nextInt(6) + 1);
23
+
24
+ dice1.draw(gc, START_X / 2, START_Y);
25
+ dice2.draw(gc, START_X + 150, START_Y);
26
+
27
+ int t = dice1.roll + dice2.roll;
28
+ Font largeFont = new Font(20);
29
+ gc.setFont(largeFont);
30
+ gc.fillText("The roll is " + t, START_X, ROLL_Y);
31
+ }
32
+
33
+ public static void main(String[] args) {
34
+ launch(args);
35
+ }
36
+ }
37
+
38
+ class Dice {
39
+ private static final int SIZE = 120, DOT_SIZE = 20;
40
+ private static final int DOT_1 = DOT_SIZE / 2;
41
+ private static final int DOT_2 = SIZE / 2 - DOT_SIZE / 2;
42
+ private static final int DOT_3 = SIZE - DOT_SIZE - DOT_SIZE / 2;
43
+
44
+ private static final int[][] pointsOfDot = {
45
+ { DOT_2, DOT_2 }, //x,y
46
+ { DOT_1, DOT_1 }, { DOT_1, DOT_2 }, { DOT_1, DOT_3 },
47
+ { DOT_3, DOT_1 }, { DOT_3, DOT_2 }, { DOT_3, DOT_3 }
48
+ };
49
+ private static final int[][] dotsOfRoll = {
50
+ { 0, -1 },
51
+ { 1, 6, -1 },
52
+ { 0, 1, 6, -1 },
53
+ { 1, 3, 4, 6, -1 },
54
+ { 0, 1, 3, 4, 6, -1 },
55
+ { 1, 2, 3, 4, 5, 6, -1 }
56
+ };
57
+
58
+ final int roll;
59
+
60
+ Dice(int roll) {
61
+ this.roll = roll;
62
+ }
63
+
64
+ void draw(GraphicsContext gc, int x, int y) {
65
+ gc.setFill(Color.PINK);
66
+ gc.fillRect(x, y, SIZE, SIZE);
67
+
68
+ gc.setFill(Color.BLACK);
69
+ int[] pi = dotsOfRoll[roll - 1];
70
+ for(int i=0; pi[i] != -1; i++) {
71
+ int dx = pointsOfDot[pi[i]][0];
72
+ int dy = pointsOfDot[pi[i]][1];
73
+ gc.fillOval(x + dx, y + dy, DOT_SIZE, DOT_SIZE);
74
+ }
75
+ }
76
+ }
77
+ ```