Skip to content

Commit bbd607d

Browse files
authored
He who observes the day, observes it to the Lord
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. He who eats, eats to the Lord, for he gives God thanks. He who doesn't eat, to the Lord he doesn't eat, and gives God thanks. (Romans 14:6)
1 parent 53be4d8 commit bbd607d

1 file changed

Lines changed: 299 additions & 0 deletions

File tree

task13/task1328/Solution.java

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

0 commit comments

Comments
 (0)