-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattle.java
62 lines (45 loc) · 1.97 KB
/
Battle.java
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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.stream.Collectors;
public class Battle{
private Team team1;
private Team team2;
public Team fight(Team team1, Team team2) throws Exception {
int currentRound = 0;
while(currentRound <= 30) {
ArrayList<Character> team1Members =
(ArrayList<Character>)team1
.getAliveMembers()
.stream()
.sorted(Comparator.comparing(Character::getSpeed).reversed())
.collect(Collectors.toList());
ArrayList<Character> team2Members =
(ArrayList<Character>)team2
.getAliveMembers()
.stream()
.sorted(Comparator.comparing(Character::getSpeed).reversed())
.collect(Collectors.toList());
for(Character member : team1Members) {
team1.move(member, team2);
}
for(Character member : team2Members) {
team2.move(member, team1);
}
currentRound++;
if(team1.getAliveMembersCount()==0) {
return team2;
}
if(team2.getAliveMembersCount()==0) {
return team1;
}
System.out.println("Alive members in team 1: "+team1.getAliveMembersCount());
System.out.println("Alive members in team 2: "+team2.getAliveMembersCount());
}
return null;
}
/*Sorting the alive members of both teams in the descending order
according to their speeds. This will be the order for the member to move in the round.
When a character has the turn, if the character is still alive,
call the method on the character’s team for the character to attack the enemy
team, i.e., move(Character member, Team enemyTeam) of the Team class.*/
}