use of soc.util.Queue in project JSettlers2 by jdmonin.
the class SOCRobotBrain method tradeStuff.
/**
* do some trading -- this method is obsolete and not called.
* Instead see {@link #makeOffer(SOCPossiblePiece)}, {@link #considerOffer(SOCTradeOffer)},
* etc, and the javadoc for {@link #negotiator}.
*/
protected void tradeStuff() {
/**
* make a tree of all the possible trades that we can
* make with the bank or ports
*/
SOCTradeTree treeRoot = new SOCTradeTree(ourPlayerData.getResources(), (SOCTradeTree) null);
Hashtable<SOCResourceSet, SOCTradeTree> treeNodes = new Hashtable<SOCResourceSet, SOCTradeTree>();
treeNodes.put(treeRoot.getResourceSet(), treeRoot);
Queue<SOCTradeTree> queue = new Queue<SOCTradeTree>();
queue.put(treeRoot);
while (!queue.empty()) {
SOCTradeTree currentTreeNode = queue.get();
// D.ebugPrintln("%%% Expanding "+currentTreeNode.getResourceSet());
expandTradeTreeNode(currentTreeNode, treeNodes);
Enumeration<SOCTradeTree> childrenEnum = currentTreeNode.getChildren().elements();
while (childrenEnum.hasMoreElements()) {
SOCTradeTree child = childrenEnum.nextElement();
// D.ebugPrintln("%%% Child "+child.getResourceSet());
if (child.needsToBeExpanded()) {
/**
* make a new table entry
*/
treeNodes.put(child.getResourceSet(), child);
queue.put(child);
}
}
}
/**
* find the best trade result and then perform the trades
*/
SOCResourceSet bestTradeOutcome = null;
int bestTradeScore = -1;
Enumeration<SOCResourceSet> possibleTrades = treeNodes.keys();
while (possibleTrades.hasMoreElements()) {
SOCResourceSet possibleTradeOutcome = possibleTrades.nextElement();
// D.ebugPrintln("%%% "+possibleTradeOutcome);
int score = scoreTradeOutcome(possibleTradeOutcome);
if (score > bestTradeScore) {
bestTradeOutcome = possibleTradeOutcome;
bestTradeScore = score;
}
}
/**
* find the trade outcome in the tree, then follow
* the chain of parents until you get to the root
* all the while pushing the outcomes onto a stack.
* then pop outcomes off of the stack and perfoem
* the trade to get each outcome
*/
Stack<SOCTradeTree> stack = new Stack<SOCTradeTree>();
SOCTradeTree cursor = treeNodes.get(bestTradeOutcome);
while (cursor != treeRoot) {
stack.push(cursor);
cursor = cursor.getParent();
}
SOCResourceSet give = new SOCResourceSet();
SOCResourceSet get = new SOCResourceSet();
SOCTradeTree currTreeNode;
SOCTradeTree prevTreeNode;
prevTreeNode = treeRoot;
while (!stack.empty()) {
currTreeNode = stack.pop();
give.setAmounts(prevTreeNode.getResourceSet());
give.subtract(currTreeNode.getResourceSet());
get.setAmounts(currTreeNode.getResourceSet());
get.subtract(prevTreeNode.getResourceSet());
/**
* get rid of the negative numbers
*/
for (int rt = SOCResourceConstants.CLAY; rt <= SOCResourceConstants.WOOD; rt++) {
if (give.getAmount(rt) < 0) {
give.setAmount(0, rt);
}
if (get.getAmount(rt) < 0) {
get.setAmount(0, rt);
}
}
// D.ebugPrintln("Making bank trade:");
// D.ebugPrintln("give: "+give);
// D.ebugPrintln("get: "+get);
client.bankTrade(game, give, get);
pause(2000);
prevTreeNode = currTreeNode;
}
}
use of soc.util.Queue in project JSettlers2 by jdmonin.
the class SOCRobotDM method scoreSettlementsForDumb.
/**
* For each possible settlement in our {@link SOCPlayerTracker},
* update its {@link SOCPossiblePiece#getETA() getETA()} and
* its {@link SOCPossibleSettlement#getRoadPath() getRoadPath()}.
*<P>
* Each {@link SOCPossibleSettlement#getRoadPath()} is calculated
* here by finding the shortest path among its {@link SOCPossibleSettlement#getNecessaryRoads()}.
*<P>
* Calculates ETA by using our current {@link SOCBuildingSpeedEstimate} on the resources
* needed to buy the settlement plus roads/ships for its shortest path's length.
*
* @param settlementETA ETA for building a settlement from now if it doesn't require any roads or ships
* @param ourBSE Current building speed estimate, from our {@code SOCPlayer#getNumbers()}
*
* @see #scorePossibleSettlements(int, int)
*/
protected void scoreSettlementsForDumb(final int settlementETA, SOCBuildingSpeedEstimate ourBSE) {
D.ebugPrintln("-- scoreSettlementsForDumb --");
Queue<Pair<SOCPossibleRoad, List<SOCPossibleRoad>>> queue = new Queue<Pair<SOCPossibleRoad, List<SOCPossibleRoad>>>();
Iterator<SOCPossibleSettlement> posSetsIter = ourPlayerTracker.getPossibleSettlements().values().iterator();
while (posSetsIter.hasNext()) {
SOCPossibleSettlement posSet = posSetsIter.next();
if (D.ebugOn) {
D.ebugPrintln("Estimate speedup of stlmt at " + game.getBoard().nodeCoordToString(posSet.getCoordinates()));
D.ebugPrintln("*** speedup total = " + posSet.getSpeedupTotal());
}
// /
// / find the shortest path to this settlement
// /
final List<SOCPossibleRoad> necRoadList = posSet.getNecessaryRoads();
if (!necRoadList.isEmpty()) {
// will use for BFS if needed:
queue.clear();
for (SOCPossibleRoad necRoad : necRoadList) {
if (D.ebugOn)
D.ebugPrintln("-- queuing necessary road at " + game.getBoard().edgeCoordToString(necRoad.getCoordinates()));
queue.put(new Pair<SOCPossibleRoad, List<SOCPossibleRoad>>(necRoad, null));
}
//
// Do a BFS of the necessary road paths looking for the shortest one.
//
boolean pathTooLong = false;
for (int maxIter = 50; maxIter > 0 && !queue.empty(); --maxIter) {
Pair<SOCPossibleRoad, List<SOCPossibleRoad>> dataPair = queue.get();
SOCPossibleRoad curRoad = dataPair.getA();
final List<SOCPossibleRoad> possRoadsToCur = dataPair.getB();
if (D.ebugOn)
D.ebugPrintln("-- current road at " + game.getBoard().edgeCoordToString(curRoad.getCoordinates()));
final List<SOCPossibleRoad> necRoads = curRoad.getNecessaryRoads();
if (necRoads.isEmpty()) {
//
// we have a path
//
D.ebugPrintln("Found a path!");
Stack<SOCPossibleRoad> path = new Stack<SOCPossibleRoad>();
path.push(curRoad);
if (D.ebugOn)
D.ebugPrintln("possRoadsToCur = " + possRoadsToCur);
if (possRoadsToCur != null)
// push to path, iterating from nearest to curRoad until most distant
for (int i = possRoadsToCur.size() - 1; i >= 0; --i) path.push(possRoadsToCur.get(i));
posSet.setRoadPath(path);
queue.clear();
D.ebugPrintln("Done setting path.");
} else {
final List<SOCPossibleRoad> possRoadsAndCur = (possRoadsToCur != null) ? new ArrayList<SOCPossibleRoad>(possRoadsToCur) : new ArrayList<SOCPossibleRoad>();
possRoadsAndCur.add(curRoad);
if (queue.size() + necRoads.size() > 40) {
// Too many necessary, or dupes led to loop. Bug in necessary road construction?
System.err.println("rDM.scoreSettlementsForDumb: Necessary Road Path too long for road/ship 0x" + Integer.toHexString(curRoad.getCoordinates()) + " for settle 0x" + Integer.toHexString(posSet.getCoordinates()));
pathTooLong = true;
queue.clear();
break;
}
for (SOCPossibleRoad necRoad2 : necRoads) {
if (D.ebugOn)
D.ebugPrintln("-- queuing necessary road at " + game.getBoard().edgeCoordToString(necRoad2.getCoordinates()));
queue.put(new Pair<SOCPossibleRoad, List<SOCPossibleRoad>>(necRoad2, possRoadsAndCur));
}
}
}
if (!queue.empty()) {
System.err.println("rDM.scoreSettlementsForDumb: Necessary Road Path length unresolved for settle 0x" + Integer.toHexString(posSet.getCoordinates()));
pathTooLong = true;
}
D.ebugPrintln("Done searching for path.");
//
if (pathTooLong) {
posSet.setETA(500);
} else {
SOCResourceSet targetResources = new SOCResourceSet();
targetResources.add(SOCSettlement.COST);
Stack<SOCPossibleRoad> path = posSet.getRoadPath();
if (path != null) {
final int pathLength = path.size();
final SOCPossiblePiece pathFirst = (pathLength > 0) ? path.peek() : null;
SOCResourceSet rtype = ((pathFirst != null) && (pathFirst instanceof SOCPossibleShip) && // TODO better coastal ETA scoring
!((SOCPossibleShip) pathFirst).isCoastalRoadAndShip) ? SOCShip.COST : SOCRoad.COST;
for (int i = 0; i < pathLength; i++) targetResources.add(rtype);
}
posSet.setETA(ourBSE.calculateRollsFast(ourPlayerData.getResources(), targetResources, 100, ourPlayerData.getPortFlags()));
}
} else {
//
// no roads are necessary
//
posSet.setRoadPath(null);
posSet.setETA(settlementETA);
}
D.ebugPrintln("Settlement ETA = " + posSet.getETA());
}
}
Aggregations