AIsOfCatan.GameController.ProposeTrade C# (CSharp) Method

ProposeTrade() public method

public ProposeTrade ( Player player, List give, List take ) : ITrade>.Dictionary
player Player
give List
take List
return ITrade>.Dictionary
        public Dictionary<int, ITrade> ProposeTrade(Player player, List<List<Resource>> give, List<List<Resource>> take)
        {
            var trade = new Trade(give, take);

            var dict = new Dictionary<int, Trade>(); //Reversed trades
            var replyDict = new Dictionary<int, ITrade>();

            Log(new ProposeTradeLogEvent(player.Id, trade.Give, trade.Take));

            var state = CurrentGamestate();

            foreach (var other in players)
            {
                if (other.Id == player.Id) continue; //No need to propose a trade with yourself
                dict[other.Id] = (Trade)other.Agent.HandleTrade(state, trade.Reverse(), player.Id);
                if (dict[other.Id].Status == TradeStatus.Countered)
                {
                    replyDict[other.Id] = dict[other.Id].Reverse();
                    //Note, take and give are swapped since dict[other.Id] is as seen from the opponent
                    var giveLog = dict[other.Id].Take.Where(c => c.Count > 0).Select(r => r[0]).ToList();
                    var takeLog = dict[other.Id].Give.Where(c => c.Count > 0).Select(r => r[0]).ToList();
                    Log(new CounterTradeLogEvent(other.Id, giveLog, takeLog));
                    break;
                }
            }
            proposedTrades[player.Id] = dict;

            return replyDict;
        }

Usage Example

Example #1
0
        //Trading

        public Dictionary <int, ITrade> ProposeTrade(List <List <Resource> > give, List <List <Resource> > take)
        {
            if (!valid)
            {
                throw new IllegalActionException("Tried to perform an action on an invalid GameAction");
            }
            if (!isAfterDieRoll)
            {
                throw new IllegalActionException("Tried to propose trade before the die roll");
            }
            return(controller.ProposeTrade(player, give, take));
        }