Search in sources :

Example 21 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCMakeOffer method parseDataStr.

/**
 * Parse the command String into a MakeOffer message
 *
 * @param s   the String to parse
 * @return    a MakeOffer message, or null if the data is garbled
 */
public static SOCMakeOffer parseDataStr(String s) {
    // the game name
    String ga;
    // the number of the offering player
    int from;
    // the players to which this trade is offered
    boolean[] to;
    // the set of resources being asked for
    SOCResourceSet give;
    // the set of resources that the offerer wants in exchange
    SOCResourceSet get;
    give = new SOCResourceSet();
    get = new SOCResourceSet();
    StringTokenizer st = new StringTokenizer(s, sep2);
    try {
        ga = st.nextToken();
        from = Integer.parseInt(st.nextToken());
        // Should be == game.maxPlayers
        final int numPlayerTokens = st.countTokens() - (2 * 5);
        to = new boolean[numPlayerTokens];
        for (int i = 0; i < numPlayerTokens; i++) {
            to[i] = (Boolean.valueOf(st.nextToken())).booleanValue();
        }
        /**
         * Note: this only works if SOCResourceConstants.CLAY == 1
         */
        for (int i = 1; i <= SOCResourceConstants.WOOD; i++) {
            give.setAmount(Integer.parseInt(st.nextToken()), i);
        }
        for (int i = 1; i <= SOCResourceConstants.WOOD; i++) {
            get.setAmount(Integer.parseInt(st.nextToken()), i);
        }
    } catch (Exception e) {
        return null;
    }
    return new SOCMakeOffer(ga, new SOCTradeOffer(ga, from, to, give, get));
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) StringTokenizer(java.util.StringTokenizer) SOCResourceSet(soc.game.SOCResourceSet)

Example 22 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCRobotBrain method scoreTradeOutcome.

/**
 * evaluate a trade outcome by calculating how much you could build with it
 *
 * @param tradeOutcome  a set of resources that would be the result of trading
 */
protected int scoreTradeOutcome(SOCResourceSet tradeOutcome) {
    int score = 0;
    SOCResourceSet tempTO = tradeOutcome.copy();
    if ((ourPlayerData.getNumPieces(SOCPlayingPiece.SETTLEMENT) >= 1) && (ourPlayerData.hasPotentialSettlement())) {
        while (tempTO.contains(SOCSettlement.COST)) {
            score += 2;
            tempTO.subtract(SOCSettlement.COST);
        }
    }
    if ((ourPlayerData.getNumPieces(SOCPlayingPiece.ROAD) >= 1) && (ourPlayerData.hasPotentialRoad())) {
        while (tempTO.contains(SOCRoad.COST)) {
            score += 1;
            tempTO.subtract(SOCRoad.COST);
        }
    }
    if ((ourPlayerData.getNumPieces(SOCPlayingPiece.CITY) >= 1) && (ourPlayerData.hasPotentialCity())) {
        while (tempTO.contains(SOCCity.COST)) {
            score += 2;
            tempTO.subtract(SOCCity.COST);
        }
    }
    // D.ebugPrintln("Score for "+tradeOutcome+" : "+score);
    return score;
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 23 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCRobotBrain method chooseFreeResourcesIfNeeded.

/**
 * Do we need to acquire at least <tt>numChoose</tt> resources to build our next piece?
 * Choose the resources we need most; used when we want to play a discovery development card
 * or when a Gold Hex number is rolled.
 * If returns true, has called {@link #chooseFreeResources(SOCResourceSet, int, boolean)}
 * and has set {@link #resourceChoices}.
 *
 * @param targetResources  Resources needed to build our next planned piece,
 *             from {@link SOCPossiblePiece#getResourcesToBuild()}
 *             for {@link #buildingPlan}.
 *             If {@code null}, returns false (no more resources required).
 * @param numChoose  Number of resources to choose
 * @param chooseIfNotNeeded  Even if we find we don't need them, choose anyway;
 *             set true for Gold Hex choice, false for Discovery card pick.
 * @return  true if we need <tt>numChoose</tt> resources
 * @since 2.0.00
 */
private boolean chooseFreeResourcesIfNeeded(SOCResourceSet targetResources, final int numChoose, final boolean chooseIfNotNeeded) {
    if (targetResources == null)
        return false;
    if (chooseIfNotNeeded)
        resourceChoices.clear();
    final SOCResourceSet ourResources = ourPlayerData.getResources();
    int numMore = numChoose;
    // Used only if chooseIfNotNeeded:
    // for ourBuildingPlan.peek
    int buildingItem = 0;
    boolean stackTopIs0 = false;
    /**
     * If ! chooseIfNotNeeded, this loop
     * body will only execute once.
     */
    do {
        int numNeededResources = 0;
        if (// can be null from SOCPossiblePickSpecialItem.cost
        targetResources == null)
            break;
        for (int resource = SOCResourceConstants.CLAY; resource <= SOCResourceConstants.WOOD; resource++) {
            final int diff = targetResources.getAmount(resource) - ourResources.getAmount(resource);
            if (diff > 0)
                numNeededResources += diff;
        }
        if (// TODO >= numMore ? (could change details of current bot behavior)
        (numNeededResources == numMore) || (chooseIfNotNeeded && (numNeededResources > numMore))) {
            chooseFreeResources(targetResources, numMore, !chooseIfNotNeeded);
            return true;
        }
        if (!chooseIfNotNeeded)
            return false;
        // Assert: numNeededResources < numMore.
        // Pick the first numNeeded, then loop to pick additional ones.
        chooseFreeResources(targetResources, numMore, false);
        numMore = numChoose - resourceChoices.getTotal();
        if (numMore > 0) {
            // Pick a new target from building plan, if we can.
            // Otherwise, choose our least-frequently-rolled resources.
            ++buildingItem;
            final int bpSize = buildingPlan.size();
            if (bpSize > buildingItem) {
                if (buildingItem == 1) {
                    // validate direction of stack growth for buildingPlan
                    stackTopIs0 = (0 == buildingPlan.indexOf(buildingPlan.peek()));
                }
                int i = (stackTopIs0) ? buildingItem : (bpSize - buildingItem) - 1;
                SOCPossiblePiece targetPiece = buildingPlan.elementAt(i);
                // may be null
                targetResources = targetPiece.getResourcesToBuild();
            // Will continue at top of loop to add
            // targetResources to resourceChoices.
            } else {
                // This will be the last iteration.
                // Choose based on our least-frequent dice rolls.
                final int[] resourceOrder = SOCBuildingSpeedEstimate.getRollsForResourcesSorted(ourPlayerData);
                int curRsrc = 0;
                while (numMore > 0) {
                    resourceChoices.add(1, resourceOrder[curRsrc]);
                    --numMore;
                    ++curRsrc;
                    if (curRsrc == resourceOrder.length)
                        curRsrc = 0;
                }
            // now, numMore == 0, so do-while loop will exit at bottom.
            }
        }
    } while (numMore > 0);
    return true;
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 24 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCRobotBrain method tradeStopWaitingClearOffer.

/**
 * Stop waiting for responses to a trade offer.
 * Remember other players' responses,
 * Call {@link SOCRobotClient#clearOffer(SOCGame) client.clearOffer},
 * clear {@link #waitingForTradeResponse} and {@link #counter}.
 * @since 1.1.09
 */
private void tradeStopWaitingClearOffer() {
    // /
    // / record which players said no by not saying anything
    // /
    SOCTradeOffer ourCurrentOffer = ourPlayerData.getCurrentOffer();
    if (ourCurrentOffer != null) {
        boolean[] offeredTo = ourCurrentOffer.getTo();
        SOCResourceSet getSet = ourCurrentOffer.getGetSet();
        for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
            if (getSet.contains(rsrcType)) {
                for (int pn = 0; pn < game.maxPlayers; pn++) {
                    if (offeredTo[pn]) {
                        negotiator.markAsNotSelling(pn, rsrcType);
                        negotiator.markAsNotWantingAnotherOffer(pn, rsrcType);
                    }
                }
            }
        }
        pause(1500);
        client.clearOffer(game);
        pause(500);
    }
    counter = 0;
    waitingForTradeResponse = false;
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCResourceSet(soc.game.SOCResourceSet)

Example 25 with SOCResourceSet

use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.

the class SOCRobotBrain method chooseFreeResources.

/**
 * Choose the resources we need most, for playing a Discovery development card
 * or when a Gold Hex number is rolled.
 * Find the most needed resource by looking at
 * which of the resources we still need takes the
 * longest to acquire, then add to {@link #resourceChoices}.
 * Looks at our player's current resources.
 * @param targetResources  Resources needed to build our next planned piece,
 *             from {@link SOCPossiblePiece#getResourcesToBuild()}
 *             for {@link #buildingPlan}.peek()
 * @param numChoose  Number of resources to choose
 * @param clearResChoices  If true, clear {@link #resourceChoices} before choosing what to add to it;
 *             set false if calling several times to iteratively build up a big choice.
 * @return  True if we could choose <tt>numChoose</tt> resources towards <tt>targetResources</tt>,
 *             false if we could fully satisfy <tt>targetResources</tt>
 *             from our current resources + less than <tt>numChoose</tt> more.
 *             Examine {@link #resourceChoices}{@link SOCResourceSet#getTotal() .getTotal()}
 *             to see how many were chosen.
 */
protected boolean chooseFreeResources(final SOCResourceSet targetResources, final int numChoose, final boolean clearResChoices) {
    /**
     * clear our resource choices
     */
    if (clearResChoices)
        resourceChoices.clear();
    /**
     * find the most needed resource by looking at
     * which of the resources we still need takes the
     * longest to acquire
     */
    SOCResourceSet rsCopy = ourPlayerData.getResources().copy();
    SOCBuildingSpeedEstimate estimate = new SOCBuildingSpeedEstimate(ourPlayerData.getNumbers());
    int[] rollsPerResource = estimate.getRollsPerResource();
    for (int resourceCount = 0; resourceCount < numChoose; resourceCount++) {
        int mostNeededResource = -1;
        for (int resource = SOCResourceConstants.CLAY; resource <= SOCResourceConstants.WOOD; resource++) {
            if (rsCopy.getAmount(resource) < targetResources.getAmount(resource)) {
                if (mostNeededResource < 0) {
                    mostNeededResource = resource;
                } else {
                    if (rollsPerResource[resource] > rollsPerResource[mostNeededResource]) {
                        mostNeededResource = resource;
                    }
                }
            }
        }
        if (mostNeededResource == -1)
            // <--- Early return: couldn't choose enough ---
            return false;
        resourceChoices.add(1, mostNeededResource);
        rsCopy.add(1, mostNeededResource);
    }
    return true;
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Aggregations

SOCResourceSet (soc.game.SOCResourceSet)54 Test (org.junit.Test)14 SOCTradeOffer (soc.game.SOCTradeOffer)10 SOCPlayer (soc.game.SOCPlayer)8 Stack (java.util.Stack)4 SOCShip (soc.game.SOCShip)3 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)2 SOCBoardLarge (soc.game.SOCBoardLarge)2 SOCGame (soc.game.SOCGame)2 SOCRoad (soc.game.SOCRoad)2 Queue (soc.util.Queue)2 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 List (java.util.List)1 MissingResourceException (java.util.MissingResourceException)1 Vector (java.util.Vector)1 SOCInventoryItem (soc.game.SOCInventoryItem)1 SOCLRPathData (soc.game.SOCLRPathData)1 SOCSpecialItem (soc.game.SOCSpecialItem)1