質問編集履歴
2
コード修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -100,11 +100,13 @@
|
|
100
100
|
|
101
101
|
###試したこと
|
102
102
|
|
103
|
-
とりあえず
|
103
|
+
とりあえずコメントしていただいたように、目的地までの絶対値を100で割って
|
104
104
|
|
105
|
-
|
105
|
+
移動距離のアクション数を出してみました。(間違いがあれば教えてください)
|
106
106
|
|
107
|
-
|
107
|
+
しかし、次は旋回の回数をどう取得すればいいかで詰まってしまいました。
|
108
|
+
|
109
|
+
旋回の回数は1回か2回のどちらかということはわかっているのですが。
|
108
110
|
|
109
111
|
ド素人のコードですがレビューお願いします
|
110
112
|
|
@@ -118,52 +120,20 @@
|
|
118
120
|
|
119
121
|
//getPostion()は現在地を取得するメソッドです
|
120
122
|
|
121
|
-
public int countAction(int destinationX, int destinationY, Direction direction) {
|
123
|
+
public int countAction(int destinationX, int destinationY, Direction direction) {
|
122
124
|
|
123
125
|
int countAction = 0;
|
124
126
|
|
125
|
-
int distanceX = destinationX - getPostionX();
|
127
|
+
int distanceX = Math.abs(destinationX - getPostionX());
|
126
128
|
|
127
|
-
int distanceY = destinationY - getPostionY();
|
129
|
+
int distanceY = Math.abs(destinationY - getPostionY());
|
128
130
|
|
129
|
-
i
|
131
|
+
countAction = countAction + (distanceY + distanceX) / 100;
|
130
132
|
|
131
|
-
|
133
|
+
|
132
134
|
|
133
|
-
|
135
|
+
|
134
136
|
|
135
|
-
|
137
|
+
}
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
if (distanceY < 0) {
|
140
|
-
|
141
|
-
for (int j = -100; j == distanceY; j += -100) {
|
142
|
-
|
143
|
-
countAction++;
|
144
|
-
|
145
|
-
}
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
if (distanceX > 0) {
|
150
|
-
|
151
|
-
for(int i = 100; i == distanceX; i += 100){
|
152
|
-
|
153
|
-
countAction++;
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
}
|
158
|
-
|
159
|
-
if (distanceX < 0) {
|
160
|
-
|
161
|
-
for (int j = -100; j == distanceX; j += -100) {
|
162
|
-
|
163
|
-
countAction++;
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
}
|
168
138
|
|
169
139
|
```
|
1
試していること追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -95,3 +95,75 @@
|
|
95
95
|
|
96
96
|
|
97
97
|
質問するのも初めてなので追加で必要な情報があれば教えてください。よろしくお願いします。
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
###試したこと
|
102
|
+
|
103
|
+
とりあえずこんな感じで書き始めましたがif文がやたら多くなってしまい
|
104
|
+
|
105
|
+
前後左右の移動のカウントを上手く1つにまとめたいのですが目的地の座標が+の場合と-の場合があるので上手くまとめられません
|
106
|
+
|
107
|
+
else ifを使っていないのは使わない方がいいと書いてあったためで理由はまだ調べていません。
|
108
|
+
|
109
|
+
ド素人のコードですがレビューお願いします
|
110
|
+
|
111
|
+
```
|
112
|
+
|
113
|
+
//目的地までの最小アクション数を算出する
|
114
|
+
|
115
|
+
//destinationX,Yはそれぞれ目的地の座標
|
116
|
+
|
117
|
+
//directionは最終的に向いていたい方角です
|
118
|
+
|
119
|
+
//getPostion()は現在地を取得するメソッドです
|
120
|
+
|
121
|
+
public int countAction(int destinationX, int destinationY, Direction direction) {
|
122
|
+
|
123
|
+
int countAction = 0;
|
124
|
+
|
125
|
+
int distanceX = destinationX - getPostionX();
|
126
|
+
|
127
|
+
int distanceY = destinationY - getPostionY();
|
128
|
+
|
129
|
+
if (distanceY > 0) {
|
130
|
+
|
131
|
+
for (int i = 100; i == distanceY; i += 100) {
|
132
|
+
|
133
|
+
countAction++;
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
if (distanceY < 0) {
|
140
|
+
|
141
|
+
for (int j = -100; j == distanceY; j += -100) {
|
142
|
+
|
143
|
+
countAction++;
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
if (distanceX > 0) {
|
150
|
+
|
151
|
+
for(int i = 100; i == distanceX; i += 100){
|
152
|
+
|
153
|
+
countAction++;
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
if (distanceX < 0) {
|
160
|
+
|
161
|
+
for (int j = -100; j == distanceX; j += -100) {
|
162
|
+
|
163
|
+
countAction++;
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
```
|