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

📄 boardhelper.java

📁 good project for programmer,,
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			{			if ( ! haveSeenAlready[ neighbors[i].getCode() ] )				{				// Create the new node's history array. (It is just testCode's 				// history with its CC added at the beginning):				int[] newHistory = new int[ testCodeHistory.length + 1 ];				newHistory[0] = neighbors[i].getCode();				for (int j = 1; j < newHistory.length; j++)					{					newHistory[j] = testCodeHistory[j-1];					}				Q.pushWithValueAndHistory( 					neighbors[i], 					// If the neighbor is owned by the proper person then minus 					// its armies from the value so if gets pulled off the Q next.					// Without this there is a bug										armiesSoFar + (neighbors[i].getOwner() == owner ? -neighbors[i].getArmies() : neighbors[i].getArmies()),					newHistory );				haveSeenAlready[ neighbors[i].getCode() ] = true;				}			}				if ( Q.isEmpty() )			{			System.out.println("ERROR in easyCostCountryWithOwner->can't pop");			return null;			}				armiesSoFar = Q.topValue();		testCodeHistory = Q.topHistory();		testCode = Q.pop();				if ( countries[testCode].getOwner() == owner )			return testCodeHistory;		}	} // End of easyCostFromCountries/** This method returns a Country[] containing a list from country <i>CCF</i>to some country in <i>continent</i>, along the easiest path. <p>This method uses a self-sorting queueing mechanism to determine the easiestpath (in terms of enemy armies) between <i>CCF</i>, and any one that is part of <i>continent</i>.  Starting with <i>CCF</i>,it enqueues and follows every path outwards from its neighbors (and theirneighbors, and so on), making note of the accumulation of enemy armies, until it finds a path that terminates in a country which is part of  <i>continent</i>.  <p>This method returns a list that contains the path. The list is simply an array of Country objects, each holding the next countryin line. Due to the method ofenqueueing, the zero element of the array is the country in <i>continent</i> that was found, while the last element of the array (the one with thehighest value index) will be <i>CCF</i>. <p>If <i>CCF</i> is part of <i>continent</i>, an array of length two is returned, with <i>CC</i> at index [1] and one of its neighbors at index [0].<p>If <i>CCF</i> is invalid, or if no paths are found, null is returned.<p>Keep in mind that this method will  take into account the ownership of intervening countries on the path returned.The path is guaranteed to detour around 'friendly' countries.<p>* @param	CCF		the country code of the interesting country* @param	continent  the index of the interesting continent* @param	countries  the board* @return  			An array of Country objects* @see  	Country* @see  	BoardHelper#closestCountryWithOwner*/public static Country[] easyCostFromCountryToContinent( 							Country CCF, int continent, Country[] countries )	{	int ccf = CCF.getCode();	int[] retval = BoardHelper.easyCostFromCountryToContinent(   						ccf, continent, countries);	Country[] rets = new Country[retval.length];	for(int i = 0; i < retval.length; i++)		{		rets[i] = countries[retval[i]];		}	return(rets);	}	/** This method returns an int[] containing a list from country <i>CCF</i>to some country in <i>continent</i>, along the easiest path. <p>This method uses a self-sorting queueing mechanism to determine the easiestpath (in terms of enemy armies) between <i>CCF</i>, and any one that is part of <i>continent</i>.  Starting with <i>CCF</i>,it enqueues and follows every path outwards from its neighbors (and theirneighbors, and so on), making note of the accumulation of enemy armies, until it finds a path that terminates in a country which is part of  <i>continent</i>.  <p>This method returns a list that contains the path. The list is simply an int array, each holding the next country code in line. Due to the method ofenqueueing, the zero element of the array is the country in <i>continent</i> that was found, while the last element of the array (the one with thehighest value index) will be <i>CCF</i>. <p>If <i>CCF</i> is part of <i>continent</i>, an array of length two is returned, with <i>CC</i> at index [1] and one of its neighbors at index [0].<p>If <i>CCF</i> is invalid, or if no paths are found, null is returned.<p>Keep in mind that this method will  take into account the ownership of intervening countries on the path returned.The path is guaranteed to detour around 'friendly' countries.<p>* @param	CCF		the country code of the interesting country* @param	continent  the index of the interesting continent* @param	countries  the board* @return  			An array of integers* @see  	Country* @see  	BoardHelper#closestCountryWithOwner*/public static int[] easyCostFromCountryToContinent( 								int CCF, int continent, Country[] countries )	{	if ( CCF < 0 || countries.length <= CCF || continent < 0 )		{		System.out.println(  		"ERROR in easyCostFromCountryToContinent() -> bad parameters.");		return null;		}		int CCFOwner = countries[CCF].getOwner();	int testCode = CCF;	int armiesSoFar = 0;	int[] testCodeHistory = new int[1];	testCodeHistory[0] = CCF;	// We keep track of which countries we have already seen (so we don't	// consider the same country twice). We do it with a boolean array, with	// a true/false value for each of the countries:	boolean[] haveSeenAlready = new boolean[countries.length];	for (int i = 0; i < countries.length; i++)		{		haveSeenAlready[i] = false;		}			haveSeenAlready[CCF] = true;		// Create a Q (with a history) to store the country-codes and their cost 	// so far:	CountryPathStack Q = new CountryPathStack();		// Loop over the expand-enqueue until either the correct 	// country is found or there are no more countries in the Q:	while ( true )		{		// Get the current countries neighbors, and cycle through them:		Country[] neighbors = countries[testCode].getAdjoiningList();		for (int i = 0; i < neighbors.length; i++)			{			// We only care about them if we haven't seen them before and if 			// they aren't owned by CCF's owner:			if ( ! haveSeenAlready[ neighbors[i].getCode() ]  					&& neighbors[i].getOwner() != CCFOwner )				{				// Create the new node's history array. (It is just 				// testCode's history with its CC added at the end):				int[] newHistory = new int[ testCodeHistory.length + 1 ];				for (int j = 0; j < testCodeHistory.length; j++)					{					newHistory[j] = testCodeHistory[j];					}				newHistory[newHistory.length-1] = neighbors[i].getCode();				Q.pushWithValueAndHistory( 					neighbors[i], 					// If the neighbor is in the proper continent					// its armies from the value so if gets pulled off the Q next.					// Without this there is a bug										armiesSoFar + (neighbors[i].getContinent() == continent ? -neighbors[i].getArmies() : neighbors[i].getArmies()),					newHistory );				haveSeenAlready[ neighbors[i].getCode() ] = true;				}			}				if ( Q.isEmpty() )			{			return null;			}				armiesSoFar = Q.topValue();		testCodeHistory = Q.topHistory();		testCode = Q.pop();				if ( countries[testCode].getContinent() == continent )			return testCodeHistory;		}		}	// End of easyCostFromCountryToContinent/** This method finds the easyest path from country <i>CCF</i> (CCFrom) to <i>CCT</i> (CCTo), that doesn't cross a country owned by <i>CCF</i>'s owner.<p>This method uses a self-sorting queueing mechanism to determine the easiestpath (in terms of enemy armies) between <i>CCF</i> and <i>CCT</i>.  Starting with <i>CCF</i>,it enqueues and follows every path outwards from its neighbors (and theirneighbors, and so on), making note of the accumulation of enemy armies, until it finds a path that terminates in the desired country <i>CCT</i><p>This method returns a list that contains the path. The list is simply an  array of Country objects, each holding the next country in line. Due to the method ofenqueueing, the zero element of the array will be country <i>CCT</i> that was found, while the last element of the array (the one with thehighest value index) will be <i>CCF</i>. <p>Keep in mind that this method will  take into account the ownership of intervening countries on the path returned.The path is guaranteed to detour around 'friendly' countries if possible.<p>If <i>CCF</i> is invalid, or if no paths are found because all paths areblocked by 'friendly' countries, null is returned.<p>* @param	CCF		the country code of the from country* @param	CCT		the country code of the to country* @param	countries  the board* @return  			An array of Country objects* @see  	Country* @see  	BoardHelper#closestCountryWithOwner* @see  	BoardHelper#easyCostFromCountryToContinent*/public static Country[] easyCostBetweenCountries( 								Country CCF, Country CCT, Country[] countries )	{	int ccf = CCF.getCode();	int cct = CCT.getCode();	int[] retval = BoardHelper.easyCostBetweenCountries(   						ccf, cct, countries);	Country[] rets = new Country[retval.length];	for(int i = 0; i < retval.length; i++)		{		rets[i] = countries[retval[i]];		}	return(rets);	}   							/** This method finds the easyest path from country <i>CCF</i> (CCFrom) to <i>CCT</i> (CCTo), that doesn't cross a country owned by <i>CCF</i>'s owner.<p>This method uses a self-sorting queueing mechanism to determine the easiestpath (in terms of enemy armies) between <i>CCF</i> and <i>CCT</i>.  Starting with <i>CCF</i>,it enqueues and follows every path outwards from its neighbors (and theirneighbors, and so on), making note of the accumulation of enemy armies, until it finds a path that terminates in the desired country <i>CCT</i><p>This method returns a list that contains the path. The list is simply an int array, each holding the next country code in line. Due to the method ofenqueueing, the zero element of the array will be country <i>CCT</i>, that was found, while the last element of the array (the one with thehighest value index) will be <i>CCF</i>. <p>Keep in mind that this method will  take into account the ownership of intervening countries on the path returned.The path is guaranteed to detour around 'friendly' countries if possible.<p>If <i>CCF</i> is invalid, or if no paths are found because all paths areblocked by 'friendly' countries, null is returned.<p>* @param	CCF		the country code of the from country* @param	CCT		the country code of the to country* @param	countries  the board* @return  			An array of integers* @see  	Country* @see  	BoardHelper#closestCountryWithOwner* @see  	BoardHelper#easyCostFromCountryToContinent*/public static int[] easyCostBetweenCountries(    									int CCF, int CCT, Country[] countries )	{	if ( CCF < 0 || countries.length <= CCF  				|| CCT < 0 || countries.length <= CCT )		{		System.out.println(  		"ERROR in easyCostBetweenCountries() -> bad parameters.");		return null;		}		int CCFOwner = countries[CCF].getOwner();	int testCode = CCF;	int armiesSoFar = 0;	int[] testCodeHistory = new int[1];	testCodeHistory[0] = CCF;	// We keep track of which countries we have already seen (so we don't 	// consider the same country twice). We do it with a boolean array, with	// a true/false value for each of the countries:	boolean[] haveSeenAlready = new boolean[countries.length];	for (int i = 0; i < countries.length; i++)		{		haveSeenAlready[i] = false;		}			haveSeenAlready[CCF] = true;		// Create a Q (with a history) to store the country-codes and their cost 	// so far:	CountryPathStack Q = new CountryPathStack();		// Loop over the expand-enqueue until either the correct 	// country is found or there are no more countries in the Q:	while ( true )		{		// Get the current countries neighbors, and cycle through them:		Country[] neighbors = countries[testCode].getAdjoiningList();		for (int i = 0; i < neighbors.length; i++)			{			// We only care about them if we haven't seen them before and if 			// they aren't owned by CCF's owner:			if ( ! haveSeenAlready[ neighbors[i].getCode() ]   					&& neighbors[i].getOwner() != CCFOwner )				{				// Create the new node's history array. (It is just 				// testCode's history with its CC added at the beginning):				int[] newHistory = new int[ testCodeHistory.length + 1 ];				newHistory[0] = neighbors[i].getCode();				for (int j = 1; j < newHistory.length; j++)					{					newHistory[j] = testCodeHistory[j-1];					}				Q.pushWithValueAndHistory( 					neighbors[i], 					// If the neighbor is the proper country then minus 					// its armies from the value so if gets pulled off the Q next.					// Without this there is a bug										armiesSoFar + (neighbors[i].getCode() == CCT ? -neighbors[i].getArmies() : neighbors[i].getArmies()),					newHistory );				haveSeenAlready[ neighbors[i].getCode() ] = true;				}			}				if ( Q.isEmpty() )			{			return null;			}				armiesSoFar = Q.topValue();		testCodeHistory = Q.topHistory();		testCode = Q.pop();				if ( testCode == CCT )			return testCodeHistory;		}	} // End of easyCostBetweenCountries/** This method finds the shortest path (in terms of countries) from country <i>CF</i> (CFrom) to <i>CT</i> (CTo), that ONLY goes through countries owned by <i>CF</i>'s owner.<p>This method uses a self-sorting queueing mechanism to determine the shortestpath (in terms of countries crossed) between <i>CF</i> and <i>CT</i>.  Starting with <i>CF</i>,it enqueues and follows every path outwards from its neighbors (and theirneighbors, and so on), making note of the ownership of the neighbors, until it finds a path that terminates in the desired country <i>CT</i><p>This method returns a list that contains the path. The list is simply an  array of Country objects, each holding the next country in line.  The zero element of the array will be country <i>CF</i>, while the last element of the array (the one with the highest value index) will be <i>CT</i>. <p>Keep in mind that this method will take into account the ownership of intervening countries on the path returned.  If a valid path is found, 

⌨️ 快捷键说明

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