-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
299 lines (227 loc) · 10.7 KB
/
Solution.java
File metadata and controls
299 lines (227 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
//He who observes the day, observes it to the Lord; and he who does not observe the day, to the Lord he does not observe it.
// who eats, eats to the Lord, for he gives God thanks. He who doesn't eat, to the Lord he doesn't eat,
//d gives God thanks. (Romans 14:6)
------------------------------------------------Solution.java-----------------------------------------------------------------
package com.javarush.task.task13.task1328;
/*
Битва роботов
*/
public class Solution {
public static void main(String[] args) {
Robot amigo = new Robot("Амиго");
Robot enemy = new Robot("Сгибальщик-02");
doMove(amigo, enemy);
doMove(amigo, enemy);
doMove(enemy, amigo);
doMove(amigo, enemy);
doMove(enemy, amigo);
doMove(amigo, enemy);
doMove(enemy, amigo);
doMove(amigo, enemy);
}
public static void doMove(AbstractRobot robotFirst, AbstractRobot robotSecond) {
BodyPart attacked = robotFirst.attack();
BodyPart defenced = robotFirst.defense();
System.out.println(String.format("%s атаковал робота %s, атакована %s, защищена %s",
robotFirst.getName(), robotSecond.getName(), attacked, defenced));
}
}
------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------AbstractRobot.java------------------------------------------------------------
package com.javarush.task.task13.task1328;
public abstract class AbstractRobot implements Attackable, Defensable{
private static int hitCount;
public abstract String getName();
public BodyPart attack() {
BodyPart attackedBodyPart = null;
hitCount = hitCount + 1;
if (hitCount == 1) {
attackedBodyPart = BodyPart.ARM;
} else if (hitCount == 2) {
attackedBodyPart = BodyPart.HEAD;
} else if (hitCount == 3) {
attackedBodyPart = BodyPart.LEG;
} else if (hitCount > 3) {
hitCount = 0;
attackedBodyPart = BodyPart.CHEST;
}
return attackedBodyPart;
}
public BodyPart defense() {
BodyPart defencedBodyPart = null;
hitCount = hitCount + 2;
if (hitCount == 1) {
defencedBodyPart = BodyPart.HEAD;
} else if (hitCount == 2) {
defencedBodyPart = BodyPart.LEG;
} else if (hitCount == 3) {
defencedBodyPart = BodyPart.ARM;
} else if (hitCount > 3) {
hitCount = 0;
defencedBodyPart = BodyPart.CHEST;
}
return defencedBodyPart;
}
}
------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------BodyPart.java----------------------------------------------------------------
package com.javarush.task.task13.task1328;
public final class BodyPart {
final static BodyPart LEG = new BodyPart("нога");
final static BodyPart HEAD = new BodyPart("голова");
final static BodyPart ARM = new BodyPart("рука");
final static BodyPart CHEST = new BodyPart("грудь");
private String bodyPart;
private BodyPart(String bodyPart) {
this.bodyPart = bodyPart;
}
@Override
public String toString() {
return this.bodyPart;
}
}
------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------Defensable.java--------------------------------------------------------------
package com.javarush.task.task13.task1328;
public interface Defensable {
BodyPart defense();
}
------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------Attackable.java--------------------------------------------------------------
package com.javarush.task.task13.task1328;
public interface Attackable {
BodyPart attack();
}
------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------Robot.java-------------------------------------------------------------------
package com.javarush.task.task13.task1328;
public class Robot extends AbstractRobot {
private String name;
public Robot(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
------------------------------------------------------------------------------------------------------------------------------
/*
Битва роботов
1. Разобраться в том, что тут написано.
2. http://info.javarush.ru/uploads/images/00/00/07/2014/08/12/50f3e37f94.png
3. Смириться со своей участью и продолжить разбираться в коде.
4. …
5. Порадоваться, что мы все поняли.
6. Изменить код согласно новой архитектуре и добавить новую логику:
6.1. Сделать класс AbstractRobot абстрактным, вынести логику атаки и защиты из Robot в AbstractRobot.
6.2. Отредактировать класс Robot учитывая AbstractRobot.
6.3. Расширить класс BodyPart новой частью тела BodyPart.CHEST(«грудь«).
6.4. Добавить новую часть тела в реализацию интерфейсов Attackable и Defensable (в классе AbstractRobot).
7. http://info.javarush.ru/uploads/images/00/00/07/2014/08/12/3b9c65580b.png
Требования:
1. Класс AbstractRobot должен быть абстрактным.
2. Класс AbstractRobot должен реализовывать интерфейсы Attackable и Defensable.
3. Класс Robot должен наследоваться от класса AbstractRobot.
4. Логика поведения роботов должна быть вынесена в класс AbstractRobot.
5. В классе BodyPart должна содержаться и быть инициализирована final static переменная CHEST типа BodyPart.
6. Новая часть тела(BodyPart) должна быть добавлена в логику методов attack и defense в классе AbstractRobot.
------------------------------------------------Solution.java-----------------------------------------------------------------
package com.javarush.task.task13.task1328;
/*
Битва роботов
*/
public class Solution {
public static void main(String[] args) {
Robot amigo = new Robot("Амиго");
Robot enemy = new Robot("Сгибальщик-02");
doMove(amigo, enemy);
doMove(amigo, enemy);
doMove(enemy, amigo);
doMove(amigo, enemy);
doMove(enemy, amigo);
doMove(amigo, enemy);
doMove(enemy, amigo);
doMove(amigo, enemy);
}
public static void doMove(AbstractRobot robotFirst, AbstractRobot robotSecond) {
BodyPart attacked = robotFirst.attack();
BodyPart defenced = robotFirst.defense();
System.out.println(String.format("%s атаковал робота %s, атакована %s, защищена %s",
robotFirst.getName(), robotSecond.getName(), attacked, defenced));
}
}
------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------AbstractRobot.java------------------------------------------------------------
package com.javarush.task.task13.task1328;
public class AbstractRobot {
}
------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------BodyPart.java----------------------------------------------------------------
package com.javarush.task.task13.task1328;
public final class BodyPart {
final static BodyPart LEG = new BodyPart("нога");
final static BodyPart HEAD = new BodyPart("голова");
final static BodyPart ARM = new BodyPart("рука");
private String bodyPart;
private BodyPart(String bodyPart) {
this.bodyPart = bodyPart;
}
@Override
public String toString() {
return this.bodyPart;
}
}
------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------Defensable.java--------------------------------------------------------------
package com.javarush.task.task13.task1328;
public interface Defensable {
BodyPart defense();
}
------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------Attackable.java--------------------------------------------------------------
package com.javarush.task.task13.task1328;
public interface Attackable {
BodyPart attack();
}
------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------Robot.java-------------------------------------------------------------------
package com.javarush.task.task13.task1328;
public class Robot implements Attackable, Defensable {
private static int hitCount;
private String name;
public Robot(String name) {
this.name = name;
}
public String getName() {
return name;
}
public BodyPart attack() {
BodyPart attackedBodyPart = null;
hitCount = hitCount + 1;
if (hitCount == 1) {
attackedBodyPart = BodyPart.ARM;
} else if (hitCount == 2) {
attackedBodyPart = BodyPart.HEAD;
} else if (hitCount == 3) {
hitCount = 0;
attackedBodyPart = BodyPart.LEG;
}
return attackedBodyPart;
}
public BodyPart defense() {
BodyPart defencedBodyPart = null;
hitCount = hitCount + 2;
if (hitCount == 1) {
defencedBodyPart = BodyPart.HEAD;
} else if (hitCount == 2) {
defencedBodyPart = BodyPart.LEG;
} else if (hitCount == 3) {
hitCount = 0;
defencedBodyPart = BodyPart.ARM;
}
return defencedBodyPart;
}
}
------------------------------------------------------------------------------------------------------------------------------
*/