📄 boardhelper.java
字号:
package com.sillysoft.lux.util;import com.sillysoft.lux.*;//// BoardHelper.java// Lux//// Copyright (c) 2002-2007 Sillysoft Games. All rights reserved.//import java.util.List;import java.util.ArrayList;import java.util.Vector;/** This class is a collection of static methods for getting information out of a set of countries. <p>The majority of the methods in this class require a final parameter of Country[] countries. If the method calls for countries, do not attempt to pass in anything less than the entire board. Failure to do so will resultin undefined behavior.<p>There are three groups of methods in this class: Those that return information about the state of the game, those that return information aboutthe state of the board layout, and those that return paths between countries and/or continents. <p>Have fun.*/public class BoardHelper {/** Returns the country owned by <i>player</i> with the most armies on it.<p>This method first searches through all the <i>countries</i> looking for those which are owned by <i>player</i>. Once it has that list, it inspects eachto find out how many armies are on, and makes a note of whichever has the most.<p>To find the countries owned by <i>player</i>, it uses a PlayerIterator.<p>In the event that <i>player</i> is invalid, or is no longer in the game, the value returned is null.<p>* @param player the player interested in* @param countries the board* @return A country object* @see CountryIterator* @see PlayerIterator*/public static Country getPlayersBiggestArmy( int player, Country[] countries ) { CountryIterator armies = new PlayerIterator(player,countries); int biggestArmies = -1; Country root = null; while (armies.hasNext()) { Country a = armies.next(); if (a.getArmies() > biggestArmies) { biggestArmies = a.getArmies(); root = a; } } return root; }/** Same as getPlayersBiggestArmy() except it will return the biggest army that has at least one enemy neighbor. Will return null if the player has no countries. */public static Country getPlayersBiggestArmyWithEnemyNeighbor( int player, Country[] countries ) { CountryIterator armies = new PlayerIterator(player,countries); int biggestArmies = -1; Country root = null; while (armies.hasNext()) { Country a = armies.next(); if (a.getArmies() > biggestArmies && a.getNumberEnemyNeighbors() > 0) { biggestArmies = a.getArmies(); root = a; } } return root; } /** This method calculates the total number of armies owned by <i>player</i>. <p>This method first searches through all the <i>countries</i> looking for those which are owned by <i>player</i>. Once it has that list, it inspects eachto find out how many armies are on, and adds them together for a grandtotal.<p>To find the countries owned by <i>player</i>, it simply iterates over theentire board (ie, each Country in <i>countries</i>). It does not use a PlayerIterator.<p>In the event that <i>player</i> is invalid, or is no longer in the game, the value returned is zero.<p>* @param player the player interested in* @param countries the board* @return An integer* @see Country*/public static int getPlayerArmies( int player, Country[] countries ) { int enemies = 0; for (int i = 0; i< countries.length; i++) { if (countries[i].getOwner() == player) enemies += countries[i].getArmies(); } return enemies; } /** This method calculates the number of countries owned by <i>player</i>.<p>This method first searches through all the <i>countries</i> looking for those which are owned by <i>player</i>. As it finds them, it increments a value,which, when finished, is returned to the caller.<p>To find the countries owned by <i>player</i>, it simply iterates over theentire board (ie, each Country in <i>countries</i>). It does not use a PlayerIterator.<p>In the event that <i>player</i> is invalid, or is no longer in the game, the value returned is zero.<p>* @param player the player interested in* @param countries the board* @return An integer* @see Country* @see CountryIterator* @see PlayerIterator*/public static int getPlayerCountries( int player, Country[] countries ) { int number = 0; for (int i = 0; i< countries.length; i++) { if (countries[i].getOwner() == player) number++; } return number; } /** This method calculates the number of armies owned by <i>player</i> in <i>continent</i>.<p>This method first searches through all the <i>countries</i> looking for those which are part of <i>continent</i>. Once it has that list, it inspects eachto find out first if it is owned by <i>player</i>, and then adds the numberof armies on it to a counter if so. The final value in the counter iswhat is returned.<p>To find the countries in the <i>continent</i>, it uses a ContinentIterator.<p>In the event that <i>player</i> is invalid, or is no longer in the game, the value returned is zero.<p>In the event that <i>continent</i> is invalid, the value returned is zero. <p>* @param player the player interested in* @param continent the continent interested in* @param countries the board* @return An integer* @see Country* @see CountryIterator* @see ContinentIterator*/public static int getPlayerArmiesInContinent( int player, int continent, Country[] countries ) { int enemies = 0; CountryIterator continentE = new ContinentIterator(continent, countries); while (continentE.hasNext()) { Country c = continentE.next(); if (c.getOwner() == player) enemies += c.getArmies(); } return enemies; }/** this method calculates the number of armies owned by anyone who is NOT <i>player</i> in <i>continent</i>.<p>This method first searches through all the <i>countries</i> looking for those which are part of <i>continent</i>. Once it has that list, it inspects eachto find out first if it is owned by <i>player</i>, and then adds the numberof armies on it to a counter if <i><b>not</b></i>. The final value in the counter is what is returned.<p>To find the countries in the <i>continent</i>, it uses a ContinentIterator.<p>In the event that <i>player</i> is invalid, or is no longer in the game, the value returned is the total number of armies on the <i>continent</i> regardless of which player they belong to.<p>In the event that <i>continent</i> is invalid, the value returned is zero. <p>* @param player the player interested in* @param continent the continent interested in* @param countries the board* @return An integer* @see Country* @see CountryIterator* @see ContinentIterator*/public static int getEnemyArmiesInContinent( int player, int continent, Country[] countries ) { int enemies = 0; CountryIterator continentE = new ContinentIterator(continent, countries); while (continentE.hasNext()) { Country c = continentE.next(); if (c.getOwner() != player) enemies += c.getArmies(); } return enemies; } /** Get the number of armies owned by the player that are in countries that directly adjoin the given continent. */public static int getPlayerArmiesAdjoiningContinent(int ID, int cont, Country[] countries) { int[] borders = BoardHelper.getContinentBorders(cont, countries); // Make a list of the countries that must be counted - to avoid duplicate counting List adjoining = new ArrayList(); for (int b = 0; b < borders.length; b++) { Country[] neighbors = countries[ borders[b] ].getAdjoiningList(); for (int j = 0; j < neighbors.length; j++) if (neighbors[j].getOwner() == ID && neighbors[j].getContinent() != cont && ! adjoining.contains(neighbors[j])) adjoining.add(neighbors[j]); } int result = 0; for (int b = 0; b < adjoining.size(); b++) result += ((Country)adjoining.get(b)).getArmies(); return result; } /** This method checks if <i>player</i> still owns any countries.<p>This method first searches through all the <i>countries</i> looking for those which are owned by <i>player</i>. If it finds one, it immediatly stopslooking and returns true. If no country is found with <i>player</i> asthe owner, then it returns false.<p>To find a country owned by <i>player</i>, it simply iterates over theentire board (ie, each Country in <i>countries</i>). It does not useany type of Iterator.<p>In the event that <i>player</i> is invalid, or is no longer in the game, the value returned is the total number of armies on the <i>continent</i> regardless of which player they belong to.<p>In the event that <i>player</i> is invalid, or is no longer in the game, the value returned is false.<p>This method could be used to determine if any countries are unchosen duringthe initial selection process by passing in a -1 as the <i>player</i>.The use of the resulting information is dubious, at best, and other routines exist that are better suited to this use. <p>* @param player the player interested in* @param countries the board* @return A boolean (true or false)* @see Country* @see BoardHelper#playerOwnsContinentCountry* @see BoardHelper#getSmallestOpenCont* @see BoardHelper#getSmallestEmptyCont*/public static boolean playerIsStillInTheGame( int player, Country[] countries ) { // We cycle through the countries: for (int i = 0; i < countries.length; i++) { // If we find a country that player owns, we return true: if (countries[i].getOwner() == player) return true; } // If it got to here then player doesn't own any countries (and is // therefore out of the game). return false; }/** Returns the number of continents.<p>This method first increments a counter, looking for a null return valuefrom ContinentIterator. The value returned is the counter, less one.<p>* @param countries the board* @return An integer* @see Country* @see CountryIterator* @see ContinentIterator*/public static int numberOfContinents( Country[] countries ) { // there are a variable number of continents, so keep looking until // one doesn't exist boolean lastContExisted = true; int i; for (i = 0; lastContExisted; i++) { lastContExisted = false; ContinentIterator iter = new ContinentIterator( i, countries ); if (iter.hasNext()) { lastContExisted = true; } } return i-1; }/** Returns the number of countries in <i>continent</i>.<p>This method first searches through all the <i>countries</i> looking for those which are part of <i>continent</i>. Once it has that list, it simply iterates over the list and increments a counter as it does so.<p>To find the countries in the <i>continent</i>, it uses a ContinentIterator.<p>In the event that <i>continent</i> is invalid, the value returned is zero. <p>* @param continent the continent interested in* @param countries the board* @return An integer* @see Country* @see CountryIterator* @see ContinentIterator*/public static int getContinentSize(int continent, Country[] countries) { int count = 0; ContinentIterator iter = new ContinentIterator( continent, countries ); while (iter.hasNext()) { iter.next(); count++; } return count; }/** Returns the code of a country inside <i>continent</i>.<p>This method first searches through all the <i>countries</i> looking for those which are part of <i>continent</i>. Once it has that list, it simply iterates over the list and increments a counter as it does so.<p>To find the countries in the <i>continent</i>, it uses a ContinentIterator.<p>In the event that <i>continent</i> is invalid, the value returned is zero. <p>* @param continent the continent interested in* @param countries the board* @return An integer* @see Country* @see CountryIterator* @see ContinentIterator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -