-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBoard.java
More file actions
227 lines (168 loc) · 6.37 KB
/
Board.java
File metadata and controls
227 lines (168 loc) · 6.37 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
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
/**
* This class sets up the countries and continents on the Risk game board.
*
* @author Ted Mader
* @version Alpha
* @date 5/02/14
**/
public class Board {
private int i;
private int j;
private boolean isLoaded;
private boolean isAdjacent;
private String name;
private String[] continentsArray;
private String[] adjacenciesArray;
private ArrayList<Country> countriesList;
private ArrayList<Country> adjacenciesList;
private ArrayList<Country> memberCountries;
private ArrayList<Country> unoccupied;
private ArrayList<Continent> continentsList;
private HashMap<String, Country> countriesMap;
private HashMap<String, Continent> continentsMap;
public Board() {
}
/**
* Loads the information needed to construct the board and constructs the country and continent objects
* needed for the board from three files. The first file lists all the countries. The second file lists
* all of the continents and which countries are on a given continent. The third file lists the adjacencies
* for each country.
**/
public boolean loadBoard(String[] countriesArray, String[] adjacenciesFileArray, String[] continentsFileArray) {
isLoaded = false;
countriesMap = new HashMap<String, Country>();
continentsMap = new HashMap<String, Continent>();
// Populates countriesMap
for (i = 0; i < countriesArray.length; i++) {
countriesMap.put(countriesArray[i], new Country(countriesArray[i]));
}
countriesList = new ArrayList<Country>(countriesMap.values());
// Populates country adjacencies
for (i = 0; i < adjacenciesFileArray.length; i++) {
System.out.println("Building new adjacenciesArray with " + adjacenciesFileArray[i] + "...");
// Splits each line into the individual country names
adjacenciesArray = adjacenciesFileArray[i].split(",");
System.out.println("Building new adjacenciesList...");
adjacenciesList = new ArrayList<Country>();
// Creates a list of countries adjacent to the country in index 0
for (j = 1; j < adjacenciesArray.length; j++) {
System.out.println("Adding to adjacenciesList: " + adjacenciesArray[j]);
adjacenciesList.add(countriesMap.get(adjacenciesArray[j]));
}
// Adds the adjacencies to the country
countriesMap.get(adjacenciesArray[0]).addAdjacencies(adjacenciesList);
}
// Populates continents
for (i = 0; i < continentsFileArray.length; i++) {
// Splits the data of each line
continentsArray = continentsFileArray[i].split(",");
memberCountries = new ArrayList<Country>();
// Populates memberCountries, starting at index 2
for (j = 2; j < continentsArray.length; j++) {
System.out.println("Adding memberCountry to " + continentsArray[0] + ": " + continentsArray[j]);
memberCountries.add(countriesMap.get(continentsArray[j]));
}
// Populates continents hash map
continentsMap.put(continentsArray[0], new Continent(continentsArray[0], Integer.parseInt(continentsArray[1]), memberCountries));
}
// Sets isLoaded to true if successful
isLoaded = true;
return isLoaded;
}
/**
* Returns a list containing the continent objects the board has
* */
public ArrayList<Continent> getContinents() {
return new ArrayList<Continent>(continentsMap.values());
}
/**
* Returns the continent object whose name is the string continentName
**/
public Continent getContinentByName(String continentName) {
return continentsMap.get(continentName);
}
/**
* Returns the number of bonus armies awarded to a player for controlling all the countries in
* the continent whose name is the string continentName
**/
public int getBonusAmrines(String continentName) {
return continentsMap.get(continentName).getBonusArmies();
}
/**
* Returns a list of the country objects that are in the continent specified
* by the string continentName
**/
public ArrayList<Country> getMemberCountries(String continentName) {
return continentsMap.get(continentName).getMemberCountries();
}
/**
* Returns a list of all of the country objects in the board
**/
public ArrayList<Country> getCountries() {
return countriesList;
}
/**
* Returns the country object for the country specified by the string
* countryName
**/
public Country getCountryByName(String countryName) {
return countriesMap.get(countryName);
}
/**
* Sets the occupant of the country object specified by the string countryName
* to be the player object supplied as an argument.
**/
public void setOccupant(String countryName, Player occupant) {
countriesMap.get(countryName).setOccupant(occupant);
}
/**
* Returns the player object that currently occupies the country specufied by
* string countryName
**/
public Player getOccupant(String countryName) {
return countriesMap.get(countryName).getOccupant();
}
/**
* Sets the number of armies currently in the country specified by the string
* countryName to the integer supplied as an argument
**/
public void setNumberArmies(String countryName, int numberArmies) {
countriesMap.get(countryName).setNumArmies(numberArmies);
}
/**
* Returns the number of armies currently in the country specified by the string
* countryName
**/
public int getNumberArmies(String countryName) {
return countriesMap.get(countryName).getArmies();
}
/**
* Returns a list of the country objects that are the countries adjacent to the country
* specified by the string countryName on the board
**/
public ArrayList<Country> getAdjacencies(String countryName) {
return countriesMap.get(countryName).getAdjacencies();
}
public ArrayList<Country> getUnoccupied() {
unoccupied = new ArrayList<Country>();
for (i = 0; i < countriesList.size(); i++) {
if (countriesList.get(i).hasPlayer() == false) {
unoccupied.add(countriesList.get(i));
}
}
return unoccupied;
}
public boolean checkAdjacency(String countryA, String countryB) {
if (countriesMap.get(countryA).getAdjacencies().contains(countriesMap.get(countryB))) {
isAdjacent = true;
} else {
isAdjacent = false;
}
return isAdjacent;
}
}