📄 boardhelper.java
字号:
/** Returns the cont-code of the smallest continent that has at least one unowned country.<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 has at least one country ownedby -1 (ie,the default at-map-creation-time non-player), is returned.<p>The method iterates over the entire board one continent at a time, checkingfor 'any' ownership by player -1. If the entire board is found to be owned by active players, than -1 is returned. <p>* @param countries the board* @return An integer* @see Country* @see BoardHelper#playerOwnsContinentCountry* @see BoardHelper#getContinentSize*/public static int getSmallestOpenCont(Country[] countries) { int numContinents = BoardHelper.numberOfContinents(countries); // First of all we look at all the continents, and choose the smallest // one that has at least one country empty int smallUnownedContSize = 1000000; int smallUnownedCont = -1; for (int cont = 0; cont < numContinents; cont++) { int size = BoardHelper.getContinentSize(cont, countries); if (size < smallUnownedContSize && BoardHelper.playerOwnsContinentCountry( -1, cont, countries )) { smallUnownedContSize = size; smallUnownedCont = cont; } } return smallUnownedCont; }/**This method is the same as getSmallestOpenCont() except it only considerscontinents that have a bonus of greater than zero. */public static int getSmallestPositiveOpenCont(Country[] countries, Board board) { int numContinents = BoardHelper.numberOfContinents(countries); // First of all we look at all the continents, and choose the smallest // one that has at least one country 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.playerOwnsContinentCountry( -1, cont, countries )) { smallUnownedContSize = size; smallUnownedCont = cont; } } } return smallUnownedCont; }/** Find the closest country to <i>CC</i> that is owned by <i>owner</i>.<p>This method uses a self-sorting queueing mechanism to determine the closestcountry to <i>CC</i> that is owned by <i>owner</i>. Starting with <i>CC</i>,it enqueues and follows every path outwards from its neighbors (and theirneighbors, and so on) until itfinds a path that terminates in a country owned by the given <i>owner</i>.That Country object is returned. The actual path information isdiscarded. To capture the path, see easyCostCountryWithOwner.<p>If <i>CC</i> or <i>owner</i> is invalid, null is returned.<p>* @param CC the interesting country* @param owner the index of the interesting player* @param countries the board* @return A country object* @see Country* @see BoardHelper#easyCostCountryWithOwner*/public static Country closestCountryWithOwner( Country CC, int owner, Country[] countries ) { int cc = CC.getCode(); int retval = BoardHelper.closestCountryWithOwner( cc, owner, countries); if (retval < 0) return null; return(countries[retval]); } /** Find the closest country to <i>CC</i> that is owned by <i>owner</i>.<p>This method uses a self-sorting queueing mechanism to determine the closestcountry to <i>CC</i> that is owned by <i>owner</i>. Starting with <i>CC</i>,it enqueues and follows every path outwards from its neighbors (and theirneighbors, and so on) until itfinds a path that terminates in a country owned by the given <i>owner</i>.The country code of that country is returned. The actual path information isdiscarded. To capture the path, see easyCostCountryWithOwner.<p>If <i>CC</i> is invalid, -1 is returned.<p>If <i>owner</i> is invalid, -2 is returned.<p>* @param CC the country code of the interesting country* @param owner the index of the interesting player* @param countries the board* @return An integer* @see Country* @see BoardHelper#easyCostCountryWithOwner*/public static int closestCountryWithOwner( int CC, int owner, Country[] countries ) { if ( CC < 0 || countries.length <= CC ) return -1; int testCode = CC; int distanceSoFar = 0; // 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[CC] = true; // Create a Q to store the country-codes and their distance from the // start country: CountryStack Q = new CountryStack(); // Loop over the expand-enqueue until either the correct // country is found or there are no more countries left: while ( true ) { Country[] neighbors = countries[testCode].getAdjoiningList(); for (int i = 0; i < neighbors.length; i++) { if ( ! haveSeenAlready[ neighbors[i].getCode() ] ) { Q.pushWithValue( neighbors[i], distanceSoFar+1 ); haveSeenAlready[ neighbors[i].getCode() ] = true; } } if ( Q.isEmpty() ) return -2; distanceSoFar = Q.topValue(); testCode = Q.pop(); if ( countries[testCode].getOwner() == owner ) return testCode; } } // End of closestCountryWithOwner /** Find the closest country to a country in <i>startingCountryList</i> that is owned by <i>owner</i>.<p>This method uses a self-sorting queueing mechanism to determine the closestcountry to <i>CC</i> that is owned by <i>owner</i>. Starting with the countries in <i>startingCountryList</i>,it enqueues and follows every path outwards from their neighbors (and theirneighbors, and so on) until itfinds a path that terminates in a country owned by the given <i>owner</i>.The country code of that country is returned. The actual path information isdiscarded. To capture the path, see easyCostCountryWithOwner.<p>If <i>CC</i> is invalid, -1 is returned.<p>If <i>owner</i> is invalid, -2 is returned.<p>* @param startingCountryList a List containing the counries to start searching from* @param owner the index of the interesting player* @param countries the board* @return An integer* @see Country* @see BoardHelper#easyCostCountryWithOwner*/public static int closestCountryWithOwner( List startingCountryList, int owner, Country[] countries ) { if ( startingCountryList.size() < 1 ) return -1; int[] startingCodes = new int[startingCountryList.size()]; for (int i = 0; i < startingCountryList.size(); i++) startingCodes[i] = ((Country) startingCountryList.get(i)).getCode(); // 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; } // Create a Q to store the country-codes and their distance from the // start country: CountryStack Q = new CountryStack(); for (int i = 0; i < startingCodes.length; i++) { haveSeenAlready[startingCodes[i]] = true; Q.pushWithValue( countries[startingCodes[i]], 0 ); } int testCode = Q.pop(); int distanceSoFar = 0; // Loop over the expand-enqueue until either the correct // country is found or there are no more countries left: while ( true ) { Country[] neighbors = countries[testCode].getAdjoiningList(); for (int i = 0; i < neighbors.length; i++) { if ( ! haveSeenAlready[ neighbors[i].getCode() ] ) { Q.pushWithValue( neighbors[i], distanceSoFar+1 ); haveSeenAlready[ neighbors[i].getCode() ] = true; } } if ( Q.isEmpty() ) return -2; distanceSoFar = Q.topValue(); testCode = Q.pop(); if ( countries[testCode].getOwner() == owner ) return testCode; } } // End of closestCountryWithOwner/** This method searches for the country owned by <i>owner</i> that has the easiest path to get to country <i>CC</i>.<p>This method uses a self-sorting queueing mechanism to determine the easiestpath (in terms of enemy armies) between <i>CC</i>, and any one that is owned by <i>owner</i>. Starting with <i>CC</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 owned by the given <i>owner</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 of enqueueing, the zero element of the array is the country with <i>owner</i> that was found, while the last element of the array will be <i>CC</i>. <p>If <i>CC</i> is owned by <i>owner</i>, an array of one length is returned,with <i>CC</i> at index [0].<p>If <i>CC</i> is invalid, or if no paths are found, null is returned.<p>Keep in mind that if you are trying to target a given player, and you own<i>CC</i>, this method does not take into account that you might also ownone of the intervening countries on the path returned. Trying to attacka country that you own is not productive.<p>* @param CC the country code of the interesting country* @param owner the index of the interesting player* @param countries the board* @return An array of Country objects* @see Country* @see BoardHelper#closestCountryWithOwner*/public static Country[] easyCostCountryWithOwner( Country CC, int owner, Country[] countries ) { int cc = CC.getCode(); int[] retval = BoardHelper.easyCostCountryWithOwner( cc, owner, countries); Country[] rets = new Country[retval.length]; for(int i = 0; i < retval.length; i++) { rets[i] = countries[retval[i]]; } return(rets); } /** This method searches for the country owned by <i>owner</i> that has the easiest path to get to country <i>CC</i>.<p>This method uses a self-sorting queueing mechanism to determine the easiestpath (in terms of enemy armies) between <i>CC</i>, and any one that is owned by <i>owner</i>. Starting with <i>CC</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 owned by the given <i>owner</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 with <i>owner</i> that was found, while the last element of the array will be <i>CC</i>. <p>If <i>CC</i> is owned by <i>owner</i>, an array of one length is returned,with <i>CC</i> at index [0].<p>If <i>CC</i> is invalid, or if no paths are found, null is returned.<p>Keep in mind that if you are trying to target a given player, and you own<i>CC</i>, this method does not take into account that you might also ownone of the intervening countries on the path returned. Trying to attacka country that you own is not productive.<p>* @param CC the country code of the interesting country* @param owner the index of the interesting player* @param countries the board* @return An array of integers* @see Country* @see BoardHelper#closestCountryWithOwner*/public static int[] easyCostCountryWithOwner( int CC, int owner, Country[] countries ) { if ( CC < 0 || countries.length <= CC ) { System.out.println("ERROR from easyCostCountryWithOwner: bad params"); return null; } // First let's check to see if country CC is owned by owner: if ( countries[CC].getOwner() == owner ) { // Then we just return a list with only <CC> in it: int[] result = new int[1]; result[0] = CC; return result; } int testCode = CC; int armiesSoFar = 0; int[] testCodeHistory = new int[1]; testCodeHistory[0] = CC; // 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[CC] = 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 ) { Country[] neighbors = countries[testCode].getAdjoiningList(); for (int i = 0; i < neighbors.length; i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -