⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boardhelper.java

📁 good project for programmer,,
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
*/public static int getCountryInContinent(int continent, Country[] countries)	{	if (continent < 0)		System.out.println("BoardHelper.getCountryInContinent() called with a continent code of "+continent);			ContinentIterator ce = new ContinentIterator(continent, countries);	return ce.next().getCode();	}	/** This method returns an array of countryCodes of the border countries of <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 determine what countries are adjacent.  If any of the adjacent countriesis part of a different continent, then the root country is added to a listof 'border' countries.  Finally, it returns the list of border countries.<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 a null list... or it throws a wobbly which is caught by the game engine, causing you to forfeit the remainder of the current phase.<p>* @param continent  the continent interested in* @param countries  the board* @return   		An array of integers representing a series of countries* @see Country* @see CountryIterator* @see ContinentIterator*/public static int[] getContinentBorders( int continent, Country[] countries )	{	// We need a list for temporary results:	List tempResults = new ArrayList();		// Loop through the countries in the continent:	boolean border;	ContinentIterator iter = new ContinentIterator( continent, countries );	while (iter.hasNext()) {		Country c = iter.next();		border = false;		Country[] neighbors = c.getAdjoiningList();		// Now loop through the neighbors and see if any of them are in 		// another continent		for (int j = 0; j < neighbors.length; j++)			{			if ( neighbors[j].getContinent() != continent )				{				// If country[i] has a neighbor in another continent, then 				// country[i] is a border country:				border = true;				}			}				if (border)			tempResults.add(c);		}		// We are done. Copy the list into an array:	int[] result = new int[tempResults.size()];	for (int i = 0; i < result.length; i++)		result[i] = ((Country)tempResults.get(i)).getCode();		return result;	}	/** This method returns an array of countryCodes of the border countries <i>outside</i> the requested <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 determine what countries are adjacent.  If any of the adjacent countriesis part of a different continent, then the adjacent country is added to a listof 'border' countries.  Finally, it returns the list of border countries.<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 a null list... or it throws a wobbly which is caught by the game engine, causing you to forfeit the remainder of the current phase.<p>Warning:  This method has not been blessed.  It was snuck in by the documentor.<p> * @param continent  the continent interested in* @param countries  the board* @return   		An array of integers representing a series ofcountries* @see Country* @see CountryIterator* @see ContinentIterator*/public static int[] getContinentBordersBeyond(    									int continent, Country[] countries )	{	// We need a list for temporary results:	List tempResults = new ArrayList();		// Loop through the countries in the continent:	ContinentIterator iter = new ContinentIterator( continent, countries );	while (iter.hasNext()) {		Country c = iter.next();		Country[] neighbors = c.getAdjoiningList();		// Now loop through the neighbors and see if any of them are in 		// another continent		for (int j = 0; j < neighbors.length; j++)			{			if ( neighbors[j].getContinent() != continent )				{				// If c has a neighbor in another continent, then 				// the neighbor is a border-beyond country:				if (!tempResults.contains(neighbors[j]))					tempResults.add(neighbors[j]);				}			}		}		// We are done. Copy the list into an array:	int[] result = new int[tempResults.size()];	for (int i = 0; i < result.length; i++)		result[i] = ((Country)tempResults.get(i)).getCode();		return result;	}	/** This method simply determines if <i>player</i> owns all the countries that are part of <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 <i>not</i> owned by <i>player</i>.  If it findsany that are not, it returns false.  If all the countries are successfullychecked, then it returns true.<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>player</i> is invalid, or is no longer in the game, the value returned is false, which is sane.<p>Warning:  In the event that <i>continent</i> is invalid, the value returned is<i><b>true</b></i>, which is not sane.<p>* @param player 	the player interested in* @param continent  the continent interested in* @param countries  the board* @return   		A boolean (true or false)* @see Country* @see CountryIterator* @see ContinentIterator*/public static boolean playerOwnsContinent( 							int player, int continent, Country[] countries )	{	// Loop through all the countries in the desired continent, checking to 	// see if the desired player owns them. If we find a country that he 	// does not own, then we return false:	ContinentIterator iter = new ContinentIterator( continent, countries );	while (iter.hasNext()) {		if ( iter.next().getOwner() != player )			return false;		}		// Otherwise we return true:	return true;	}	// End of playerOwnsContinent()/** This method simply determines if <i>player</i> owns any full continents.<p>For each continent on the board, this method searches through all the <i>countries</i> in it, looking for any that are not owned by <i>player</i>.If it determines that a given continent is not fully owned by <i>player</i>,it moves on to the next.  If at any time it determines that <i>player</i>owns a full continent, it returns true.  If it runs through all the continentswithout finding one owned by <i>player</i>, it returns false.<p>To find the countries in the <i>continent</i>s, it uses a ContinentIterator.It also uses this ContinentIterator to determine when there are no morecontinents to be checked.<p>In the event that <i>player</i> is invalid, or is no longer in the game, the value returned is false.<p>* @param player 	the player interested in* @param countries  the board* @return   		A boolean (true or false)* @see Country* @see CountryIterator* @see ContinentIterator*/public static boolean playerOwnsAnyContinent( int player, Country[] countries )	{	// there are a variable number of continents, so keep looking until one 	// doesn't exist	boolean lastContExisted = true;			for (int i = 0; lastContExisted; i++) {		lastContExisted = false;		ContinentIterator iter = new ContinentIterator( i, countries );		if (iter.hasNext()) {			if (playerOwnsContinent( player, i, countries )) {				return true;				}			lastContExisted = true;			}		}	return false;	}/** The same as playerOwnsAnyContinent() except it only considers continents with positive bonuses. */public static boolean playerOwnsAnyPositiveContinent( int player, Country[] countries, Board board )	{	// there are a variable number of continents, so keep looking until one 	// doesn't exist	boolean lastContExisted = true;			for (int i = 0; lastContExisted; i++) {		lastContExisted = false;		ContinentIterator iter = new ContinentIterator( i, countries );		if (iter.hasNext()) {			if (board.getContinentBonus(i) > 0 && playerOwnsContinent( player, i, countries )) {				return true;				}			lastContExisted = true;			}		}	return false;	}/** Checks whether or not any player fully owns <i>continent</i>.<p>The first country in the <i>continent</i> is checked for its owner, and thenevery additional country within the <i>continent</i> is inspected to seeif the same player owns them.  If so, true is returned.  The moment it finds a country that is owned by a player other than the one found in thefirst country, false is returned.<p>To find the countries in the <i>continent</i>s, it uses a ContinentIterator.<p>Warning:  In the event that <i>continent</i> is invalid, the value returned is<i><b>true</b></i>, which is not sane.<p>* @param continent  the continent interested in* @param countries  the board* @return   		A boolean (true or false)* @see Country* @see CountryIterator* @see ContinentIterator*/public static boolean anyPlayerOwnsContinent( int continent, Country[] countries )	{	ContinentIterator iter = new ContinentIterator( continent, countries );	int owner = iter.next().getOwner();	while (iter.hasNext()) {		if (iter.next().getOwner() != owner)			return false;		}	return true;	}/** This method simply determines if <i>player</i> owns ANY of the countriesthat are part of <i>continent</i>.<p>Each country in the <i>continent</i> is checked for its owner.  If anycountry is owned by <i>player</i>, then true is returned.  If all countriesare checked and none are found to be owned by <i>player</i>, false is returned.<p>To find the countries in the <i>continent</i>s, it uses a ContinentIterator.<p>In the event that either <i>player</i> or <i>continent</i> is invalid, orthe <i>player</i> is no longer in the game, the value returned isfalse.<p>* @param player 	the player interested in* @param continent  the continent interested in* @param countries  the board* @return   		A boolean (true or false)* @see Country* @see CountryIterator* @see ContinentIterator*/public static boolean playerOwnsContinentCountry(  							int player, int continent, Country[] countries )	{	// Loop through all the countries in the desired continent, checking to 	// see if the desired player owns them. If we find a country that he does	// own, then we return true:	ContinentIterator iter = new ContinentIterator( continent, countries );	while (iter.hasNext()) {		if ( iter.next().getOwner() == player ) {			return true;			}		}	// Otherwise we return false:				return false;	}	// End of playerOwnsContinentCountry()	/** Returns the cont-code of the smallest continent that is totally empty, or-1 if there are no unowned conts.<p>This method first searches through all the <i>continents</i> and makes anote of the number of countries in each.  The index of the one with the smallest number of countries, and which is totally unowned (ie, each country is owned by -1, the default at-map-creation-time non-player), is returned.<p>The method iterates over the entire board one continent at a time, checkingfor 'full' ownership by player -1.  If none are found to be owned by the non-player, than -1 is returned. <p>* @param countries  the board* @return   		An integer* @see Country* @see BoardHelper#playerOwnsContinent* @see BoardHelper#getContinentSize*/public static int getSmallestEmptyCont(Country[] countries)	{	int numContinents = BoardHelper.numberOfContinents(countries);	// First of all we look at all the continents, and choose the smallest 	// one that is FULLY empty	int smallUnownedContSize = 1000000;	int smallUnownedCont = -1;	for (int cont = 0; cont < numContinents; cont++) {		int size = BoardHelper.getContinentSize(cont, countries);		if (size < smallUnownedContSize   				&& BoardHelper.playerOwnsContinent( -1, cont, countries ))			{			smallUnownedContSize = size;			smallUnownedCont = cont;			}		}		return smallUnownedCont;	}/**This method is the same as getSmallestEmptyCont() except it onlyconsiders continents that have bonus values of greater than zero.	*/public static int getSmallestPositiveEmptyCont(Country[] countries, Board board)	{	int numContinents = BoardHelper.numberOfContinents(countries);	// First of all we look at all the continents, and choose the smallest 	// one that is FULLY empty	int smallUnownedContSize = 1000000;	int smallUnownedCont = -1;	for (int cont = 0; cont < numContinents; cont++) {		if (board.getContinentBonus(cont) > 0) {			int size = BoardHelper.getContinentSize(cont, countries);			if (size < smallUnownedContSize 					&& BoardHelper.playerOwnsContinent( -1, cont, countries ))				{				smallUnownedContSize = size;				smallUnownedCont = cont;				}			}		}		return smallUnownedCont;	}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -