/** * This method finds the adjacent states of a specific state * * @param myState The state which we want to find its neighbors * @param mode 0 for all adjacent states, 1 for all own adjacent states, 2 for all opponent * adjacent states * @return List of adjacent states of our state */ public ArrayList<State> getAdjacentStatesOfState(State myState, byte mode) { ArrayList<State> stateList = new ArrayList<State>(); for (Link tempLink : this.getMyLinkList()) { // Checking destination state of the links if (tempLink.getSourceState().getStateID() == myState.getStateID()) { for (State tempState : this.getMyStateList()) { if (tempState.getStateID() == tempLink.getDestintionState().getStateID()) { if (mode == 0) { // stateList.add(tempLink.getDestintionState()); stateList.add(tempState); break; } else if (mode == 1) { if (tempState.getCountryID() == tempLink.getSourceState().getCountryID()) { // stateList.add(tempLink.getDestintionState()); stateList.add(tempState); break; } } else if (mode == 2) { if (tempState.getCountryID() != tempLink.getSourceState().getCountryID()) { // stateList.add(tempLink.getDestintionState()); stateList.add(tempState); break; } } } } } // Checking source state of the links if (tempLink.getDestintionState().getStateID() == myState.getStateID()) { for (State tempState : this.getMyStateList()) { if (tempState.getStateID() == tempLink.getSourceState().getStateID()) { if (mode == 0) { // stateList.add(tempLink.getSourceState()); stateList.add(tempState); break; } else if (mode == 1) { if (tempState.getCountryID() == tempLink.getDestintionState().getCountryID()) { // stateList.add(tempLink.getSourceState()); stateList.add(tempState); break; } } else if (mode == 2) { if (tempState.getCountryID() != tempLink.getDestintionState().getCountryID()) { // stateList.add(tempLink.getSourceState()); stateList.add(tempState); break; } } } } } } return stateList; }
/** * This method finds the adjacent countries of a country * * @param opponentStates The states which we want to find their countries * @return Countries which are adjacent to a specific country */ public ArrayList<Country> getAdjacentCountries(ArrayList<State> opponentStates) { ArrayList<Country> opponentCountries = new ArrayList<Country>(); ArrayList<Integer> opponentCountriesID = new ArrayList<Integer>(); for (State state : opponentStates) { opponentCountriesID.add(state.getCountryID()); } // Creating a hashset to remove duplicate IDs HashSet countryHashSet = new HashSet(opponentCountriesID); opponentCountriesID.clear(); opponentCountriesID = new ArrayList(countryHashSet); // Finding countries which correspond to the IDs for (Integer countryID : opponentCountriesID) { for (Country country : this.myCountryList) { if (countryID == country.getCountryID()) { opponentCountries.add(country); break; } } } return opponentCountries; }
/** * This methods finds all states of a country * * @param countryID The ID of the country which we want to find its states * @return List of states of the country */ public ArrayList<State> getStatesByCountryID(int countryID) { ArrayList<State> stateList = new ArrayList<State>(); for (State tempState : this.myStateList) { if (tempState.getCountryID() == countryID) stateList.add(tempState); } return stateList; }
/** * This method will authenticate the map, whether its an acceptable map or not. * * @return A string which indicates the map problems (null string for no problem) */ public String validateMap() { // boolean verifyMap = true; String result = ""; // Check if a state is isolated for (State tempState : myStateList) { Boolean isolated = true; for (Link tempLink : myLinkList) { if (tempLink.getSourceState().getStateID() == tempState.getStateID() || tempLink.getDestintionState().getStateID() == tempState.getStateID()) { isolated = false; break; } } if (isolated) result += "<br>" + tempState.getStateName() + " is an isolated state."; } // Check if there are more than one country if (myCountryList.size() <= 1) result += "<br>" + "The map has less than two countries."; // Check if each country has exactly one capital for (Country tempCountry : myCountryList) { int capitalCount = 0; for (State tempState : myStateList) { if (tempState.getCountryID() == tempCountry.getCountryID() && tempState.getIsCapital()) capitalCount++; } if (capitalCount == 0) result += "<br>" + tempCountry.getCountryName() + " doesn't have any capital."; else if (capitalCount > 1) result += "<br>" + tempCountry.getCountryName() + " has more than one capital."; } return result; }
/** * This method finds the country to which state belongs * * @param selectedState The state which we want to find its country * @return Country of the state */ public Country getCountryByState(State selectedState) { Country returnCountry = null; for (Country tempCountry : this.getMyCountryList()) { if (tempCountry.getCountryID() == selectedState.getCountryID()) { returnCountry = tempCountry; break; } } return returnCountry; }
/** * This method find the percentage of continent possessions for a specific country * * @param countryID The ID of the country that we want to find its possessions * @return An array of percentage of continent possession */ public float[] getContinentPossession(int countryID) { ArrayList<State> statesOfContinent = null; int possessedStates; float percentage = 0; float[] percentageList = new float[this.myContinentList.size()]; // Check continents one by one for (int i = 0; i < this.myContinentList.size(); i++) { possessedStates = 0; statesOfContinent = new ArrayList<State>(); statesOfContinent = getStatesByContinentID(myContinentList.get(i).getContinentID()); for (State tempState : statesOfContinent) { if (tempState.getCountryID() == countryID) possessedStates++; } percentage = (float) possessedStates / statesOfContinent.size(); percentageList[i] = percentage; } return percentageList; }