Search in sources :

Example 51 with SOCResourceSet

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

the class SOCDiceResultResources method buildForGame.

/**
 * Builder for server to tell clients about players' gained resources and new total counts.
 * Only players with nonzero {@link SOCPlayer#getRolledResources()}
 * {@link SOCResourceSet#getKnownTotal() .getKnownTotal()} are included.
 * If no player gained any known resources, returns {@code null}.
 * @param ga  Game to check for rolled resources
 * @return  Message for this game's rolled resources, or {@code null} if no players gained known resources
 */
public static final SOCDiceResultResources buildForGame(final SOCGame ga) {
    ArrayList<Integer> pnum = null;
    ArrayList<SOCResourceSet> rsrc = null;
    for (int pn = 0; pn < ga.maxPlayers; ++pn) {
        if (ga.isSeatVacant(pn))
            continue;
        final SOCPlayer pp = ga.getPlayer(pn);
        final SOCResourceSet rs = pp.getRolledResources();
        if (rs.getKnownTotal() == 0)
            continue;
        if (pnum == null) {
            pnum = new ArrayList<Integer>();
            rsrc = new ArrayList<SOCResourceSet>();
        }
        pnum.add(Integer.valueOf(pn));
        rsrc.add(rs);
    }
    if (pnum == null)
        return null;
    ArrayList<Integer> rTotal = new ArrayList<Integer>();
    for (int pn : pnum) rTotal.add(Integer.valueOf(ga.getPlayer(pn).getResources().getTotal()));
    return new SOCDiceResultResources(ga.getName(), pnum, rTotal, rsrc);
}
Also used : ArrayList(java.util.ArrayList) SOCResourceSet(soc.game.SOCResourceSet) SOCPlayer(soc.game.SOCPlayer)

Example 52 with SOCResourceSet

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

the class DiscardStrategy method discard.

/**
 * debug logging
 */
// private transient Logger log = Logger.getLogger(this.getClass().getName());
/**
 * When we have to discard, try to keep the resources needed for our building plan.
 * If we don't have a plan, make one by calling {@link SOCRobotDM#planStuff(int)}.
 * Otherwise, discard at random.
 * Calls {@link SOCRobotNegotiator#setTargetPiece(int, SOCPossiblePiece)}
 * to remember the piece we want to build,
 * in case we'll need to trade for its lost resources.
 */
public static SOCResourceSet discard(final int numDiscards, Stack<SOCPossiblePiece> buildingPlan, Random rand, SOCPlayer ourPlayerData, SOCRobotParameters robotParameters, SOCRobotDM decisionMaker, SOCRobotNegotiator negotiator) {
    // log.debug("DISCARDING...");
    /**
     * if we have a plan, then try to keep the resources
     * needed for that plan, otherwise discard at random
     */
    SOCResourceSet discards = new SOCResourceSet();
    /**
     * make a plan if we don't have one
     */
    if (buildingPlan.empty()) {
        decisionMaker.planStuff(robotParameters.getStrategyType());
    }
    if (!buildingPlan.empty()) {
        SOCPossiblePiece targetPiece = buildingPlan.peek();
        negotiator.setTargetPiece(ourPlayerData.getPlayerNumber(), targetPiece);
        // log.debug("targetPiece="+targetPiece);
        final SOCResourceSet targetResources = targetPiece.getResourcesToBuild();
        // may be null
        /**
         * figure out what resources are NOT the ones we need
         */
        SOCResourceSet leftOvers = ourPlayerData.getResources().copy();
        if (targetResources != null)
            for (int rsrc = SOCResourceConstants.CLAY; rsrc <= SOCResourceConstants.WOOD; rsrc++) if (leftOvers.getAmount(rsrc) > targetResources.getAmount(rsrc))
                leftOvers.subtract(targetResources.getAmount(rsrc), rsrc);
            else
                leftOvers.setAmount(0, rsrc);
        SOCResourceSet neededRsrcs = ourPlayerData.getResources().copy();
        neededRsrcs.subtract(leftOvers);
        /**
         * figure out the order of resources from
         * easiest to get to hardest
         */
        // log.debug("our numbers="+ourPlayerData.getNumbers());
        final int[] resourceOrder = SOCBuildingSpeedEstimate.getRollsForResourcesSorted(ourPlayerData);
        /**
         * pick the discards
         */
        int curRsrc = 0;
        while (discards.getTotal() < numDiscards) {
            /**
             * choose from the left overs
             */
            while ((discards.getTotal() < numDiscards) && (curRsrc < 5)) {
                // log.debug("(1) dis.tot="+discards.getTotal()+" curRsrc="+curRsrc);
                if (leftOvers.contains(resourceOrder[curRsrc])) {
                    discards.add(1, resourceOrder[curRsrc]);
                    leftOvers.subtract(1, resourceOrder[curRsrc]);
                // keep looping at this resource until finished
                } else {
                    curRsrc++;
                }
            }
            curRsrc = 0;
            /**
             * choose from what we need
             */
            while ((discards.getTotal() < numDiscards) && (curRsrc < 5)) {
                // log.debug("(2) dis.tot="+discards.getTotal()+" curRsrc="+curRsrc);
                if (neededRsrcs.contains(resourceOrder[curRsrc])) {
                    discards.add(1, resourceOrder[curRsrc]);
                    neededRsrcs.subtract(1, resourceOrder[curRsrc]);
                } else {
                    curRsrc++;
                }
            }
        }
        if (curRsrc == 5) {
            // log.error("PROBLEM IN DISCARD - curRsrc == 5");
            System.err.println("discardStrategy: PROBLEM IN DISCARD - curRsrc == 5");
        }
    } else {
        /**
         *  choose discards at random
         */
        SOCGame.discardOrGainPickRandom(ourPlayerData.getResources(), numDiscards, true, discards, rand);
    }
    return discards;
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 53 with SOCResourceSet

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

the class SOCBankTrade method parseDataStr.

/**
 * Parse the command String into a BankTrade message
 *
 * @param s   the String to parse
 * @return    a BankTrade message, or null if the data is garbled
 */
public static SOCBankTrade parseDataStr(String s) {
    // the game name
    String ga;
    // the set of resources being given to the bank/port
    SOCResourceSet give;
    // the set of resources being taken from the bank/port
    SOCResourceSet get;
    // player number if sent
    int pn = -1;
    give = new SOCResourceSet();
    get = new SOCResourceSet();
    StringTokenizer st = new StringTokenizer(s, sep2);
    try {
        ga = st.nextToken();
        /**
         * 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);
        }
        if (st.hasMoreTokens())
            pn = Integer.parseInt(st.nextToken());
    } catch (Exception e) {
        return null;
    }
    return new SOCBankTrade(ga, give, get, pn);
}
Also used : StringTokenizer(java.util.StringTokenizer) SOCResourceSet(soc.game.SOCResourceSet)

Example 54 with SOCResourceSet

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

the class SOCMakeOffer method toCmd.

/**
 * @return the command string
 *
 * @param ga  the name of the game
 * @param of   the offer being made
 */
public static String toCmd(String ga, SOCTradeOffer of) {
    String cmd = MAKEOFFER + sep + ga;
    cmd += (sep2 + of.getFrom());
    boolean[] to = of.getTo();
    for (// length should be == game.maxPlayers
    int i = 0; // length should be == game.maxPlayers
    i < to.length; // length should be == game.maxPlayers
    i++) {
        cmd += (sep2 + to[i]);
    }
    SOCResourceSet give = of.getGiveSet();
    for (int i = SOCResourceConstants.CLAY; i <= SOCResourceConstants.WOOD; i++) {
        cmd += (sep2 + give.getAmount(i));
    }
    SOCResourceSet get = of.getGetSet();
    for (int i = SOCResourceConstants.CLAY; i <= SOCResourceConstants.WOOD; i++) {
        cmd += (sep2 + get.getAmount(i));
    }
    return cmd;
}
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