public void TestAcceptTradeOffer() { var manager = DoInitialPlacementsAndRoll(false); var activePlayer = manager.ActivePlayer; var player1 = manager.Players.FirstOrDefault(p => p.Id == PLAYER_1); var player2 = manager.Players.FirstOrDefault(p => p.Id == PLAYER_2); Assert.IsNotNull(player1); Assert.IsNotNull(player2); // Active player offer trade of 1 ore and 2 wheat for 3 wood and 4 sheep. activePlayer.RemoveAllResources(); player1.RemoveAllResources(); player2.RemoveAllResources(); var toGive = new ResourceCollection(ore: 1, wheat: 2); var toGet = new ResourceCollection(wood: 3, sheep: 4); activePlayer.AddResources(toGive); player1.AddResources(toGet); manager.PlayerOfferTrade(activePlayer.Id, new TradeOffer(activePlayer.Id, toGive, toGet)); Assert.AreEqual(PlayerTurnState.RequestingPlayerTrade, manager.PlayerTurnState, "Player should be in the 'RequestingTrade' state."); // Player 2 will try to accept the trade, yet he can't afford to do it. var r = manager.AcceptTradeFromActivePlayer(player2.Id); Assert.IsTrue(r.Failed, "This player can't afford to do the trade."); // Player 1 will accept the trade and it should work. r = manager.AcceptTradeFromActivePlayer(player1.Id); Assert.IsTrue(r.Succeeded, "The trade should be successful."); Assert.IsTrue(activePlayer.ResourceCards.Equals(toGet), "Active player did not get his resources from the trade."); Assert.IsTrue(player1.ResourceCards.Equals(toGive), "Player 1 did not get his resources from the trade."); Assert.AreEqual(PlayerTurnState.TakeAction, manager.PlayerTurnState, "The trade is complete. Player should be in the 'TakeAction' state."); }
public void TestAcceptTradeCounterOffer() { var manager = DoInitialPlacementsAndRoll(false); var activePlayer = manager.ActivePlayer; var player1 = manager.Players.FirstOrDefault(p => p.Id == PLAYER_1); Assert.IsNotNull(player1); // Active player offer trade of 1 ore and 2 wheat for 3 wood and 4 sheep. activePlayer.RemoveAllResources(); player1.RemoveAllResources(); var toOtherPlayer = new ResourceCollection(ore: 1, wheat: 2); var toActivePlayer = new ResourceCollection(wood: 3, sheep: 4); var toActivePlayerCounter = new ResourceCollection(brick: 5, sheep: 1); var originalOffer = new TradeOffer(activePlayer.Id, toOtherPlayer, toActivePlayer); var counterOffer = new TradeOffer(player1.Id, toActivePlayerCounter, toOtherPlayer); activePlayer.AddResources(toOtherPlayer); player1.AddResources(toActivePlayerCounter); manager.PlayerOfferTrade(activePlayer.Id, originalOffer); Assert.AreEqual(PlayerTurnState.RequestingPlayerTrade, manager.PlayerTurnState, "Player should be in the 'RequestingTrade' state."); // Player 1 can't afford it and will send a counter-offer. var r = manager.AcceptTradeFromActivePlayer(player1.Id); Assert.IsTrue(r.Failed, "Player 1 can't afford the current trade."); r = manager.SendCounterTradeOffer(player1.Id, counterOffer); Assert.IsTrue(r.Succeeded, "The counter offer send action should work."); r = manager.PlayerAcceptCounterTradeOffer(activePlayer.Id, player1.Id); Assert.IsTrue(r.Succeeded, "The counter offer should be accepted."); Assert.IsTrue(activePlayer.ResourceCards.Equals(toActivePlayerCounter), "Active player did not get his resources from the trade."); Assert.IsTrue(player1.ResourceCards.Equals(toOtherPlayer), "Player 1 did not get his resources from the trade."); Assert.AreEqual(PlayerTurnState.TakeAction, manager.PlayerTurnState, "The trade is complete. Player should be in the 'TakeAction' state."); }
/// <summary> /// Creates a new player. /// </summary> public Player(int id, string name, uint color) { Name = name; Id = id; Color = color; RoadsAvailable = 15; SettlementsAvailable = 5; CitiesAvailable = 4; ResourceCards = new ResourceCollection(); DevelopmentCards = new List<DevelopmentCards>(); DevelopmentCardsInPlay = new List<DevelopmentCards>(); }
/// <summary> /// Makes a certain player discard resources. Used when a 7 is rolled /// and the player has too many cards. Can be called by any player. /// </summary> public ActionResult PlayerDiscardResources(int playerId, ResourceCollection toDiscard) { var validation = ValidatePlayerAction(PlayerTurnState.AnyPlayerSelectingCardsToLose); if (validation.Failed) return validation; var apr = GetPlayerFromId(playerId); if (apr.Failed) return apr; var player = apr.Data; if (!_playersCardsToLose.ContainsKey(player)) return ActionResult.CreateFailed("Discarding resources is not required."); var toDiscardCount = toDiscard.GetResourceCount(); var requiredCount = _playersCardsToLose[player]; if (toDiscardCount != requiredCount) return ActionResult.CreateFailed(string.Format("Incorrect number of cards to discard. Selected: {0}. Required: {1}.", toDiscardCount, requiredCount)); if(!player.RemoveResources(toDiscard)) return ActionResult.CreateFailed("Player does not have the cards selected to discard."); // Success. Remove player from pending discard list. _playersCardsToLose.Remove(player); // If the player-discard list is empty now, let the active player place the robber. if (_playersCardsToLose.Count == 0) { _playerTurnState = _gameSettings.RobberMode == RobberMode.None ? PlayerTurnState.TakeAction : PlayerTurnState.PlacingRobber; } return ActionResult.CreateSuccess(); }
/// <summary> /// Returns true if both collections contains the same number of resources. /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Equals(ResourceCollection other) { return (Brick == other.Brick && Ore == other.Ore && Sheep == other.Sheep && Wheat == other.Wheat && Wood == other.Wood); }
/// <summary> /// Adds a collection to another collection. /// </summary> public void Add(ResourceCollection collection) { Wood += collection.Wood; Brick += collection.Brick; Sheep += collection.Sheep; Wheat += collection.Wheat; Ore += collection.Ore; }
/// <summary> /// Removes a number of resource cards of a certain type. Returns false if the player doesn't have enough. /// </summary> public bool RemoveResources(ResourceCollection collection) { if (!HasAtLeast(collection)) return false; foreach (var stack in collection.ToList()) RemoveResources(stack); return true; }
/// <summary> /// Returns true if the player has at least the resouces contained in the collection. /// </summary> public bool HasAtLeast(ResourceCollection collection) { foreach (var stack in collection.ToList()) { if (!HasAtLeast(stack)) return false; } return true; }
/// <summary> /// Adds a collection of resources into the player's hand. /// </summary> public void AddResources(ResourceCollection collection) { ResourceCards.Add(collection); }
/// <summary> /// Gets the resources generated by a building location. Ignores the robber. /// </summary> public ResourceCollection GetResourcesForBuilding(HexPoint location, BuildingTypes type) { var result = new ResourceCollection(); foreach (var hex in location.GetHexes()) { if (_resourceTiles.ContainsKey(hex)) { var tile = _resourceTiles[hex]; int count = (type == BuildingTypes.Settlement) ? 1 : 2; result[tile.Resource] += count; } } return result; }
/// <summary> /// Creates a new trade offer. /// </summary> public TradeOffer(int playerId, ResourceCollection toGive, ResourceCollection toReceive) { this.CreatorPlayerId = playerId; this.ToGive = toGive; this.ToReceive = toReceive; }
/// <summary> /// Converts to a resource collection. /// </summary> /// <returns></returns> public ResourceCollection ToResourceCollection() { var collection = new ResourceCollection(); collection[this.Type] = this.Count; return collection; }
public void TestTradeWithBank() { var manager = DoInitialPlacementsAndRoll(false); var player = manager.ActivePlayer; // Trade 4 ore for 1 brick. var toGive = new ResourceCollection(ore: 4); var toGet = new ResourceCollection(brick: 1); player.RemoveAllResources(); player.AddResources(toGive); Assert.IsTrue(player.ResourceCards.IsSingleResourceType && player.ResourceCards.Equals(toGive)); var offer = new TradeOffer(player.Id, toGive, toGet); var tradeResult = manager.PlayerTradeWithBank(player.Id, offer); Assert.IsTrue(tradeResult.Succeeded, "The bank trade should succeed."); Assert.IsTrue(player.ResourceCards.IsSingleResourceType && player.ResourceCards.Equals(toGet)); Assert.AreEqual(PlayerTurnState.TakeAction, manager.PlayerTurnState, "Player should be in the 'TakeAction' state."); // Trade 8 wheat for 2 wood. toGive = new ResourceCollection(wheat: 8); toGet = new ResourceCollection(wood: 2); player.RemoveAllResources(); player.AddResources(toGive); Assert.IsTrue(player.ResourceCards.IsSingleResourceType && player.ResourceCards.Equals(toGive)); offer = new TradeOffer(player.Id, toGive, toGet); tradeResult = manager.PlayerTradeWithBank(player.Id, offer); Assert.IsTrue(tradeResult.Succeeded, "The bank trade should succeed."); Assert.IsTrue(player.ResourceCards.IsSingleResourceType && player.ResourceCards.Equals(toGet)); Assert.AreEqual(PlayerTurnState.TakeAction, manager.PlayerTurnState, "Player should be in the 'TakeAction' state."); // Trade 2 wheat for 2 wood. Should fail. toGive = new ResourceCollection(wheat: 2); toGet = new ResourceCollection(wood: 2); player.RemoveAllResources(); player.AddResources(toGive); Assert.IsTrue(player.ResourceCards.IsSingleResourceType && player.ResourceCards.Equals(toGive)); offer = new TradeOffer(player.Id, toGive, toGet); tradeResult = manager.PlayerTradeWithBank(player.Id, offer); Assert.IsTrue(tradeResult.Failed, "The bank trade should fail."); Assert.IsTrue(player.ResourceCards.IsSingleResourceType && player.ResourceCards.Equals(toGive)); Assert.AreEqual(PlayerTurnState.TakeAction, manager.PlayerTurnState, "Player should be in the 'TakeAction' state."); }