Search in sources :

Example 16 with SOCResourceSet

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

the class TestResourceSet method removeTwoResources_doesNotThrowException.

@Test
public void removeTwoResources_doesNotThrowException() {
    SOCResourceSet rs = onePerType();
    rs.subtract(2, SOCResourceConstants.CLAY);
    assertEquals(3, rs.getTotal());
    assertEquals(0, rs.getAmount(SOCResourceConstants.CLAY));
    assertEquals(-1, rs.getAmount(SOCResourceConstants.UNKNOWN));
}
Also used : SOCResourceSet(soc.game.SOCResourceSet) Test(org.junit.Test)

Example 17 with SOCResourceSet

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

the class TestResourceSet method onePerType_hasOnePerType.

@Test
public void onePerType_hasOnePerType() {
    SOCResourceSet rs = onePerType();
    assertTrue(rs.contains(SOCResourceConstants.CLAY));
    assertTrue(rs.contains(SOCResourceConstants.WHEAT));
    assertTrue(rs.contains(SOCResourceConstants.WOOD));
    assertTrue(rs.contains(SOCResourceConstants.ORE));
    assertTrue(rs.contains(SOCResourceConstants.SHEEP));
}
Also used : SOCResourceSet(soc.game.SOCResourceSet) Test(org.junit.Test)

Example 18 with SOCResourceSet

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

the class TestResourceSet method copyConstructor_ConstructsACopy.

@Test
public void copyConstructor_ConstructsACopy() {
    SOCResourceSet all = onePerType();
    SOCResourceSet copy = new SOCResourceSet(all);
    assertEquals(all, copy);
}
Also used : SOCResourceSet(soc.game.SOCResourceSet) Test(org.junit.Test)

Example 19 with SOCResourceSet

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

the class SOCDisplaylessPlayerClient method handlePLAYERELEMENT_numRsrc.

/**
 * Update a player's amount of a resource, for {@link #handlePLAYERELEMENT(SOCGame, SOCPlayer, int, int, int, int)}.
 *<ul>
 *<LI> If this is a {@link SOCPlayerElement#LOSE} action, and the player does not have enough of that type,
 *     the rest are taken from the player's UNKNOWN amount.
 *<LI> If we are losing from type UNKNOWN,
 *     first convert player's known resources to unknown resources
 *     (individual amount information will be lost),
 *     then remove mes's unknown resources from player.
 *</ul>
 *<P>
 * To avoid code duplication, also called from
 * {@link SOCPlayerClient.MessageTreater#handlePLAYERELEMENT(SOCPlayerElement)}
 * and {@link soc.robot.SOCRobotBrain#run()}.
 *<P>
 * Before v2.0.00 this method directly took a {@link SOCPlayerElement} instead of that message's
 * {@code action} and {@code amount} fields.
 *
 * @param pl     Player to update
 * @param action   {@link SOCPlayerElement#SET}, {@link SOCPlayerElement#GAIN GAIN},
 *     or {@link SOCPlayerElement#LOSE LOSE}
 * @param rtype  Type of resource, like {@link SOCResourceConstants#CLAY}
 * @param amount    The new value to set, or the delta to gain/lose
 */
public static void handlePLAYERELEMENT_numRsrc(final SOCPlayer pl, final int action, final int rtype, final int amount) {
    switch(action) {
        case SOCPlayerElement.SET:
            pl.getResources().setAmount(amount, rtype);
            break;
        case SOCPlayerElement.GAIN:
            pl.getResources().add(amount, rtype);
            break;
        case SOCPlayerElement.LOSE:
            if (rtype != SOCResourceConstants.UNKNOWN) {
                int playerAmt = pl.getResources().getAmount(rtype);
                if (playerAmt >= amount) {
                    pl.getResources().subtract(amount, rtype);
                } else {
                    pl.getResources().subtract(amount - playerAmt, SOCResourceConstants.UNKNOWN);
                    pl.getResources().setAmount(0, rtype);
                }
            } else {
                SOCResourceSet rs = pl.getResources();
                /**
                 * first convert player's known resources to unknown resources,
                 * then remove mes's unknown resources from player
                 */
                rs.convertToUnknown();
                pl.getResources().subtract(amount, SOCResourceConstants.UNKNOWN);
            }
            break;
    }
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 20 with SOCResourceSet

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

the class SOCDisplaylessPlayerClient method handleDICERESULTRESOURCES.

/**
 * Handle all players' dice roll result resources: static version to share with SOCPlayerClient.
 * Game players gain resources.
 * @param mes  Message data
 * @param ga  Game to update
 * @param nickname  Our client player's nickname, needed for element data update
 * @param skipResourceCount  If true, ignore the resource part of the message
 *     because caller will handle that separately.
 * @since 2.0.00
 */
public static final void handleDICERESULTRESOURCES(final SOCDiceResultResources mes, final SOCGame ga, final String nickname, final boolean skipResourceCount) {
    final int n = mes.playerNum.size();
    for (// current index reading from playerNum and playerRsrc
    int p = 0; // current index reading from playerNum and playerRsrc
    p < n; // current index reading from playerNum and playerRsrc
    ++p) {
        final SOCResourceSet rs = mes.playerRsrc.get(p);
        final int pn = mes.playerNum.get(p);
        final SOCPlayer pl = ga.getPlayer(pn);
        pl.getResources().add(rs);
        if (!skipResourceCount)
            handlePLAYERELEMENT_simple(ga, pl, pn, SOCPlayerElement.SET, SOCPlayerElement.RESOURCE_COUNT, mes.playerResTotal.get(p), nickname);
    }
}
Also used : SOCResourceSet(soc.game.SOCResourceSet) SOCPlayer(soc.game.SOCPlayer)

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