//bank idx is what you want to exchange with the bank //bank trade shoudl be good all the time, so return false because you //dont want to trade with the other players public void tradeWithBank(Player theTrader, SettlersOfCatan game, int bankIdx) { //Get a list of the nodes you own List<GameNode> ownedNodes = game.gameBoard.getPlayerNodes(theTrader); Trade myTrade = new Trade(); int[] currentResources = resourcesToArray(theTrader); int resourceOffer; //Yuck foreach (GameNode gn in ownedNodes) { //Trade 2:1 for a brick if ((gn.brickPort) && (bankIdx == 0)) { resourceOffer = indexOfMostResource(theTrader, new int[1] { 0 }); myTrade = buildBankTrade(theTrader, 0, resourceOffer, 2); } //Trade 2:1 for a wheat else if ((gn.grainPort) && (bankIdx == 1)) { resourceOffer = indexOfMostResource(theTrader, new int[1] { 1 }); myTrade = buildBankTrade(theTrader, 1, resourceOffer, 2); } //Trade 2:1 for a wood else if ((gn.woodPort) && (bankIdx == 2)) { resourceOffer = indexOfMostResource(theTrader, new int[1] { 2 }); myTrade = buildBankTrade(theTrader, 2, resourceOffer, 2); } //Trade 2:1 for a wool else if ((gn.woolPort) && (bankIdx == 3)) { resourceOffer = indexOfMostResource(theTrader, new int[1] { 3 }); myTrade = buildBankTrade(theTrader, 3, resourceOffer, 2); } //Trade 2:1 for an ore else if ((gn.orePort) && (bankIdx == 4)) { resourceOffer = indexOfMostResource(theTrader, new int[1] { 4 }); myTrade = buildBankTrade(theTrader, 4, resourceOffer, 2); } //Trade 3:1 for anything except what you want else if (gn.threePort) { resourceOffer = indexOfMostResource(theTrader); if ((currentResources[resourceOffer] > 3) && (resourceOffer != bankIdx)) myTrade = buildBankTrade(theTrader, bankIdx, resourceOffer, 3); } //Trade 4:1 for anything else { resourceOffer = indexOfMostResource(theTrader); if ((currentResources[resourceOffer] > 4) && (bankIdx != resourceOffer)) myTrade = buildBankTrade(theTrader, bankIdx, resourceOffer, 4); } if (myTrade != null) break; } if (myTrade != null) { //Console.WriteLine("Trading With The Bank: " + theTrader.brick.ToString() + " " + theTrader.wheat.ToString() + " " + theTrader.wood.ToString() + " " + theTrader.wool.ToString() + " " + theTrader.ore.ToString()); for (int x = 0; x < 5; x++) { if(myTrade.tradeOffer[x] != 0) { for (int y = 0; y < myTrade.tradeOffer[x]; y++) { theTrader.decResource(x, theTrader); } } } for(int x = 0; x<5; x++) { if (myTrade.tradeRequest[x] != 0) { for (int y = 0; y < myTrade.tradeRequest[x]; y++) { theTrader.incResource(x); } } } //Console.WriteLine("Traded With The Bank: " + theTrader.brick.ToString() + " " + theTrader.wheat.ToString() + " " + theTrader.wood.ToString() + " " + theTrader.wool.ToString() + " " + theTrader.ore.ToString()); //PlayingState ps = (PlayingState)game.PlayingState; //ps.currentTrade = myTrade; } }
//Switch around resources between the two AIs public void processTrade(Player traderOfferer, Player traderAcceptor, SettlersOfCatan game) { PlayingState ps = (PlayingState)game.PlayingState; //Loop through tradeRequest and add those to tradeOfferer and remove from tradeAccepter for (int x = 0; x < 5; x++) { if (ps.currentTrade.tradeRequest[x] != 0) { for(int y=0; y< ps.currentTrade.tradeRequest[x]; y++) { traderOfferer.incResource(x); traderOfferer.decResource(x, traderAcceptor); } } } //Loop through tradeOffer and add those to tradeAccepter and remove from tradeOfferer for (int x = 0; x < 5; x++) { if (ps.currentTrade.tradeOffer[x] != 0) { for (int y = 0; y < ps.currentTrade.tradeOffer[x]; y++) { traderAcceptor.incResource(x); traderAcceptor.decResource(x, traderOfferer); } } } }
public void discardHalf(Player px, SettlersOfCatan game) { int mustDiscard = (px.brick + px.wood + px.wool + px.ore + px.wheat) / 2; int discarded = 0; int[] discardedCards = new int[5]; int[] currentResources = new int[5] { px.brick, px.wheat, px.wood, px.wool, px.ore }; bool keepGoing = true; while (keepGoing) { int idxOfMost = indexOfMostResource(px, currentResources); discardedCards[idxOfMost]++; currentResources[idxOfMost]--; discarded++; if (mustDiscard == discarded) keepGoing = false; } //actually remove everything in the discardedCards for (int x = 0; x < 5; x++) { for (int y = 0; y < discardedCards[x]; y++) { px.decResource(x, px); } } }
//Purpose: increments/decrements appropriate resources for the two players trading public void humanTradeWithAI(Player humanPlayer, Player aiTradingWith) { for (int j = 0; j < 5; j++) { if (tradeOffer[j] > 0) { while (tradeOffer[j] > 0) { humanPlayer.decResource(j, humanPlayer); aiTradingWith.incResource(j); tradeOffer[j]--; } } if (tradeRequest[j] > 0) { while (tradeRequest[j] > 0) { humanPlayer.incResource(j); aiTradingWith.decResource(j, aiTradingWith); tradeRequest[j]--; } } } }
//Purpose: make sure robber give away is correct amount of cards an if so remove the cards offered //Returns: boolean that says if this was valid or not public bool robberGiveAway(Player px) { int[] temp = new int[5]; yopRequest.CopyTo(temp, 0); int amountGive = px.totalResources / 2; int counter = 0; for (int j = 0; j < yopRequest.Length; j++) { if (temp[j] > 0) { while (temp[j] > 0) { counter++; temp[j]--; } } } if (counter == amountGive) { for (int j = 0; j < yopRequest.Length; j++) { if (yopRequest[j] > 0) { while (yopRequest[j] > 0) { px.decResource(j, px); yopRequest[j]--; } } } return true; } return false; }
//Purpose: to see if trade to bank was valid, if so decrements player cards offered and increments player cards desired //Returns: boolean to determine whether the offer was a valid trade offer public bool isBankTradeValid(Player px) { bool tradeComplete = false; if (px.hasBrick && tradeOffer[0] > 1 && px.brick > 1) { for (int j = 0; j < 5; j++) { if (tradeRequest[j] > 0) { if (j != 0) { px.decResource(0, px); px.decResource(0, px); px.incResource(j); tradeComplete = true; return tradeComplete; } } } } if (px.hasGrain && tradeOffer[1] > 1 && px.wheat > 1) { for (int j = 0; j < 5; j++) { if (tradeRequest[j] > 0) { if (j != 1) { px.decResource(1, px); px.decResource(1, px); px.incResource(j); tradeComplete = true; return tradeComplete; } } } } if (px.hasLumber && tradeOffer[2] > 1 && px.wood > 1) { for (int j = 0; j < 5; j++) { if (tradeRequest[j] > 0) { if (j != 2) { px.decResource(2, px); px.decResource(2, px); px.incResource(j); tradeComplete = true; return tradeComplete; } } } } if (px.hasWool && tradeOffer[3] > 1 && px.wool > 1) { for (int j = 0; j < 5; j++) { if (tradeRequest[j] > 0) { if (j != 4) { px.decResource(3, px); px.decResource(3, px); px.incResource(j); tradeComplete = true; return tradeComplete; } } } } if (px.hasOre && tradeOffer[4] > 1 && px.ore > 1) { for (int j = 0; j < 5; j++) { if (tradeRequest[j] > 0) { if (j != 3) { px.decResource(4, px); px.decResource(4, px); px.incResource(j); tradeComplete = true; return tradeComplete; } } } } if (px.hasThree) { for (int i = 0; i < 5; i++) { if (tradeOffer[i] > 2) { for (int j = 0; j < 5; j++) { if (tradeRequest[j] > 0) { if (j != i) { px.decResource(i, px); px.decResource(i, px); px.decResource(i, px); px.incResource(j); tradeComplete = true; return tradeComplete; } } } } } } else { for (int i = 0; i < 5; i++) { if (tradeOffer[i] > 3) { for (int j = 0; j < 5; j++) { if (tradeRequest[j] > 0) { if (j != i) { px.decResource(i, px); px.decResource(i, px); px.decResource(i, px); px.decResource(i, px); px.incResource(j); tradeComplete = true; return tradeComplete; } } } } } } return tradeComplete; }