-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
173 lines (138 loc) · 4.81 KB
/
Copy pathGame.java
File metadata and controls
173 lines (138 loc) · 4.81 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
import java.util.*;
import java.io.*;
public class Game {
public String gameName;
public ArrayList<Team> teams = new ArrayList<>(); //type inference
public Game(String gameName){
this.gameName = gameName;
}
//Method to delete a specific team
public void deleteTeam(String nameOfTheTeam){
int index = -1;
for(Team t : teams){
index++;
if(t.teamName.equals(nameOfTheTeam)){
this.teams.remove(index);
break;
}
}
}
public void listNamesAccordingToTeam(){
try{
for(Team t : this.teams){
String fileName = String.format("namelist-%s.txt",t.teamName);
File myFile = new File(fileName);
//true if the file did not already exist
if(myFile.createNewFile()){
FileWriter myWriter = new FileWriter(fileName);
for(GameCharacter c : t.teamMembers){
myWriter.write(String.format("%s\n",c.getUsername()));
}
myWriter.close();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
//Method to determine whether there are more swordsmen or archers
public String determineMajorityType(){
int numberOfSwordsmen = 0;
int numberOfArchers = 0;
for(Team t : this.teams){
for(GameCharacter c : t.teamMembers){
if(c instanceof Swordsman){
numberOfSwordsmen++;
continue;
}
if(c instanceof Archer){
numberOfArchers++;
}
}
}
if(numberOfArchers == numberOfSwordsmen){
return String.format("The number of swordsmen is the same as the number of archers.");
}
if(numberOfArchers > numberOfSwordsmen){
return String.format("There are more archers!");
}
else{
return String.format("There are more swordsmen!");
}
}
public GameCharacter bestCharacter(){
GameCharacter best = null;
int maximum = -1111;
for(Team t : this.teams){
for(GameCharacter c : t.teamMembers){
if(c.getPlayerLevel() >= maximum){
maximum = c.getPlayerLevel();
best = c;
}
}
}
return best;
}
//Method that returns an array filled with the XP values of each member of the specified team
public ArrayList<Long> experienceOfTeamMembers(String nameOfTheTeam){
ArrayList<Long> myArr = new ArrayList<>();
for(Team t : this.teams){
if(t.teamName.equals(nameOfTheTeam)){
for(GameCharacter c : t.teamMembers){
myArr.add(c.getExperience());
}
}
}
return myArr;//.stream().mapToLong(i -> i).toArray();
}
//Method to add a new character to a selected team
public void addCharacterToSpecifiedTeam(String nameOfTheTeam, GameCharacter characterToBeAdded){
Boolean teamExists = false;
int index = -1;
for(Team t : this.teams){
index++;
if(t.teamName.equals(nameOfTheTeam)){
teamExists = true;
break;
}
}
if(teamExists == true){
teams.get(index).insertMember(characterToBeAdded);
return;
}
else{
teams.add(new Team(nameOfTheTeam, characterToBeAdded));
}
}
public class Team {
public String teamName;
public ArrayList<GameCharacter> teamMembers = new ArrayList<>();
public Team(String teamName, GameCharacter characterToBeAdded){
this.teamName = teamName;
this.teamMembers.add(characterToBeAdded);
Game.this.teams.add(this);
}
//Method to insert new team members in increasing order, based on the player IDs.
public void insertMember(GameCharacter c){
if(this.teamMembers.isEmpty()){
this.teamMembers.add(c);
return;
}
int index = -1;
Boolean atEnd = true;
for(GameCharacter player : this.teamMembers){
index++;
if(player.getPlayerId() >= c.getPlayerId()){
atEnd = false;
break;
}
}
if(atEnd == false){
this.teamMembers.add(index, c);
return;
} else {
this.teamMembers.add(c);
}
}
}
}