-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMap.java
More file actions
388 lines (348 loc) · 12.8 KB
/
Copy pathMap.java
File metadata and controls
388 lines (348 loc) · 12.8 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import java.awt.Font;
import java.awt.HeadlessException;
import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import apcslib.Format;
import chn.util.*;
/**
* The Map class for the Domination game contains the information regarding all aspects relating to the map (countries, districts and players).
*
* @author (Aishwarya, Anurag, Caroline, Serena)
* @version (June 5, 2018)
*/
public class Map
{
private List<Country> europe;
private int countryNum;
private int currentPlayer;
private List<Player> gamers = new ArrayList<Player>();
private final static int NUM_PLAYERS = 4;
/**
* The constructor for the Map class loads all of the required files
* for the game, and instantiates them as well. It reads and loads the
* information regarding the country, regarding the type of district
* and it intakes user input for each player to input their desired
* user name.
*/
//Aishwarya
public Map()
{
//ArrayList of Countries (for the whole map)
europe = new ArrayList<Country>();
//file readers
FileInput inFile1;
FileInput inFile2;
//files to read
String fileName1 = "stats.txt";
String fileName2 = "districts.txt";
//initialisations
inFile1 = new FileInput(fileName1);
inFile2 = new FileInput(fileName2);
//Caroline
int countryNum = inFile1.readInt();
//reads data from files and initializes all of the information
//regarding the country
//overall number of the district
int mNum = 0;
for (int i = 0; i < countryNum; i++)
{
//name and initial population of the country
String cName = inFile1.readToken();
int population = inFile1.readInt();
//initial color of the country’s districts
String cHex = inFile1.readToken();
//total number of districts
int dNum = inFile1.readInt();
//broken down into specific type of district
int oceanBorder = inFile1.readInt();
int landLocked = inFile1.readInt();
// initial amount of money the country has
int money = inFile1.readInt();
//ArrayList of LandDistricts to keep track of each district
//in the country
ArrayList<LandDistrict> prov = new ArrayList<LandDistrict>();
//calculations to determine the population, troop count,
//and money a district has
int dPop = population / dNum;
int dTroop = 100;
int dMoney = money / dNum;
int current = 0;
//continues for the oceanBorder districts to input which ways
//(directions) the district can attack (by reading the strings
//and converting them into ints)
for (int x = 0; x < oceanBorder; x++)
{
String input;
int count;
input = inFile2.readLine();
String strarray[] = input.split(" ");
int directions[] = new int[strarray.length];
for (count = 0; count < directions.length ; count++)
{
//adds the directions into the array (as an Integer)
directions[count] = Integer.parseInt(strarray[count]);
}
//creates a new OceanBorder object and adds it to the prov
//array
prov.add(new OceanBorder(dPop, dTroop, dMoney, directions, x, cName, mNum));
mNum++;
current = x;
}
//continues for the landLocked districts to input which ways
//(directions) the district can attack (by reading the strings
//and converting them into ints)
for (int x = 0; x < landLocked; x++)
{
String input;
int count;
input = inFile2.readLine();
String strarray[] = input.split(" ");
int directions[] = new int[strarray.length];
for (count = 0; count < directions.length ; count++)
{
////adds the directions into the array (as an Integer)
directions[count] = Integer.parseInt(strarray[count]);
}
//creates a new LandLocked objects and adds it to the
//prov array
prov.add(new LandLocked(dPop, dTroop, dMoney, directions,current + x, cName, mNum));
mNum++;
}
//creates a new Country object and adds it to the europe array,
//which is an array of Countries
europe.add(new Country(cName, population, cHex, dNum, money, prov, i));
}
//Serena
//takes user input (up to four players per game) for their
//chosen user name
ConsoleIO kb = new ConsoleIO();
String[] sarray = new String[europe.size()];
String[] names = new String[NUM_PLAYERS];
UIManager.put("OptionPane.messageFont", new Font("Arial", Font.PLAIN, 26));
UIManager.put("OptionPane.buttonFont", new Font("Arial", Font.PLAIN, 12));
for(int n = 0; n < NUM_PLAYERS; n++)
{
names[n] = JOptionPane.showInputDialog(null, "Player " + (n+1) + ", please enter your name: ");
}
for(int i = 0; i < europe.size(); i++)
{
sarray[i] = europe.get(i).getName();
}
sarray = MapGUI.displaySelector(sarray,names,NUM_PLAYERS);
for(int n = 0; n < NUM_PLAYERS; n++)
{
String name = names[n];
String cName = sarray[n];
int cIndex = findId(sarray[n]);
gamers.add(new Player(name,cName,cIndex));
}
}
/**
* The transferDistrict method is called when one districts attacks
* another. The method updates the information regarding both Countries
* involved in the attack.
*
* (All params are ints so we can directly locate in the ArrayList)
* @param wc - the winning Country of the attack
* @param lc - the losing Country of the attack
* @param wd - the winning District of the attack
* @param ld - the losing District of the attack
*
*/
//Aishwarya
public void transferDistrict(int wc, int wd, int lc, int ld)
{
//takes the losing country’s province and adds it to the winning
// country and removes it from the losing country
europe.get(lc).provinces.get(ld).setIdentifier(europe.get(wc).getName());
europe.get(wc).provinces.add(europe.get(lc).provinces.remove(ld));
//updating string identifier
//updates both countries information/stats
((Country)europe.get(wc)).updateAll();
((Country)europe.get(lc)).updateAll();
}
/**
* The getEurope method is a way to access the europe List that
* contains all of the countries
*
* @return europe - returns the List<Country> that holds all of the
* countries on the map.
*/
//Anurag
public List<Country> getEurope()
{
return europe;
}
/**
* The getGamers method is a way to access the gamer List that
* contains all of the players
*
* @return gamers - returns the List<Player> that holds all of the
* players on the map.
*/
//Anurag
public List<Player> getGamers()
{
return gamers;
}
/**
* isMine(Player, DistrictButton) tells the caller if the DistrictButton mentioned belongs to its player
* @param p - the Player
* @param b - the District Button which is used to
* @return rtn - tells you whether the Button is yours.
*/
//Anurag
public boolean isMine(Player p, DistrictButton b)
{
boolean rtn = false;
//returns true if the DistrictButton belongs to the player
if(europe.get(p.getCountryIndex()).getName().equals(b.getOwner().getName()))
{
rtn = true;
}
return rtn;
}
/**
* leaderBoard runs a StupidSort on the data inside the gamers ArrayList. After sorting the gamers based on their
* nation's dNum, the resulting ordered list of Strings with stats is returned
*
* @return leader - a leader board of Strings containing stats in order by value.
*/
//Anurag
public String[] leaderBoard()
{
String[] stray = new String[NUM_PLAYERS];
int[] sort = new int[NUM_PLAYERS];
for(int i = 0 ; i < NUM_PLAYERS ; i++)
{
stray[i] = Format.left(gamers.get(i).getCountryName(), 18) + Format.right(europe.get(gamers.get(i).getCountryIndex()).getDistrictNum(), 4);
sort[i] = europe.get(gamers.get(i).getCountryIndex()).getDistrictNum();
}
int min, temp;
String stemp;
for (int outer = 0; outer < sort.length - 1; outer++)
{
min = outer;
for (int inner = outer + 1; inner < sort.length; inner++)
{
if (sort[inner] > sort[min])
{
min = inner;
}
}
//swap(list[outer], list[flag]);
temp = sort[outer];
stemp = stray[outer];
sort[outer] = sort[min];
stray[outer] = stray[min];
sort[min] = temp;
stray[min] = stemp;
}
return stray;
}
/**
* @param userCIndex - index in the europe ArrayList<Country> for the country that starts the invasion
* @param userDIndex - index in the province ArrayList<LandDistrict> for the district that mounts the offense
* @param invCIndex - the invaded country's index
* @param invDIndex - the invading country's district's index
*
*/
//Aishwarya
public boolean invade(int userCIndex, int userDIndex, int invCIndex, int invDIndex)
{
if(europe.get(userCIndex).getProvince(userDIndex).getdTroopCount() >=
europe.get(invCIndex).getProvince(invDIndex).getdTroopCount())
{
try {
JOptionPane.showMessageDialog(null,new ImageIcon(ImageIO.read(new File("InvasionSuccess.jpg"))));
} catch (HeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
europe.get(invCIndex).getProvince(invDIndex).setTroopCount(
(europe.get(userCIndex).getProvince(userDIndex).getdTroopCount() -
europe.get(invCIndex).getProvince(invDIndex).getdTroopCount()));
europe.get(userCIndex).getProvince(userDIndex).setTroopCount(0);
transferDistrict( userCIndex, userDIndex, invCIndex, invDIndex);
return true;
}
else
{
try {
JOptionPane.showMessageDialog(null,new ImageIcon(ImageIO.read(new File("InvasionFail.jpg"))));
} catch (HeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
europe.get(userCIndex).getProvince(userDIndex).setTroopCount(
-1 * (europe.get(userCIndex).getProvince(userDIndex).getdTroopCount() -
europe.get(invCIndex).getProvince(invDIndex).getdTroopCount()));
europe.get(invCIndex).getProvince(invDIndex).setTroopCount(0);
transferDistrict(invCIndex, invDIndex, userCIndex, userDIndex);
return false;
}
}
/**
* findId(String) provides the user with the index in the europe ArrayList
* <Country> for the Country with parameter s as its name
* @param s - the Country Name
* @return index of the Country in question.
*/
//Anurag
public int findId(String s)
{
int rtn = 0;
for(int i = 0 ; i < europe.size() ; i++)
{
if(s.equals(europe.get(i).getName()))
{
rtn = i;
i = europe.size();
}
}
return rtn;
}
/**
* nextTurn is a void method that moves the game on to the next player and skips over "dead" countries
*/
//Anurag
public void nextTurn()
{
// makes sure that the numbers loop around
if(currentPlayer == gamers.size()-1)
{
currentPlayer = 0;
gamers.get(currentPlayer).startTurn();
}
// makes sure that dead countries are skipped
else
{
currentPlayer++;
while(europe.get(gamers.get(currentPlayer).getCountryIndex()).getDistrictNum() == 0)
{
currentPlayer++;
}
gamers.get(currentPlayer).startTurn();
}
}
/**
* currentPlayer is the getter method for the instance variable currentPlayer
* @return the index of the currentPlayer in the gamer ArrayList<Player>
*/
//Anurag
public int currentPlayer()
{
return currentPlayer;
}
}