AIsOfCatan.GameController.HandOutResources C# (CSharp) Method

HandOutResources() private method

Hand out resources to players according to a roll Gives resources to players with buildings on tiles with a number corresponding to the roll and only if the tile doesn't have the robber. If there is not enough resources of a kind in the bank so that all player who shall receive, can get the amount they are allowed to, none of that kind are dealt (see rules p. 8 top)
private HandOutResources ( int roll ) : void
roll int The value of the roll
return void
        private void HandOutResources(int roll)
        {
            //Map from PlayerID to dictionary that maps resource to amount
            var handouts = new Dictionary<int, Dictionary<Resource, int>>();
            var handoutSums = new Dictionary<Resource, int>();
            for (int i = 0; i < players.Length; i++)
            {
                handouts[i] = new Dictionary<Resource, int>();
                foreach (Resource r in Enum.GetValues(typeof(Resource)))
                    handouts[i][r] = 0;
            }
            foreach (Resource r in Enum.GetValues(typeof(Resource)))
                handoutSums[r] = 0;
            //Count how many resources to be dealt
            for (int i = 0; i <= 44; i++)
            {
                var tile = board.GetTile(i);
                if (tile.Value != roll || board.GetRobberLocation() == i) continue;
                if (tile.Terrain == Terrain.Desert || tile.Terrain == Terrain.Water) continue;
                foreach (var piece in board.GetPieces(i))
                {
                    int incr = (piece.Token == Token.Settlement) ? 1 : 2;
                    handouts[piece.Player][(Resource)tile.Terrain] += incr;
                    handoutSums[(Resource)tile.Terrain] += incr;
                }
            }

            //Check if there are enough resources in the bank
            foreach (var resource in handoutSums.Keys)
            {
                if (resourceBank[(int)resource] < handoutSums[resource])
                {
                    for (int i = 0; i < players.Length; i++)
                    {
                        handouts[i][resource] = 0;
                    }
                }
            }

            //Hand out resources
            foreach (var player in players)
            {
                List<Resource> logResources = new List<Resource>();
                foreach (var resource in handouts[player.Id].Keys)
                {
                    GetResource(player, resource, handouts[player.Id][resource]);
                    for (var i = 0; i < handouts[player.Id][resource]; i++)
                        logResources.Add(resource);
                }
                Log(new ReceiveResourcesLogEvent(logResources.ToArray(), player.Id));
            }
        }