Пример #1
0
        /// <summary>
        /// Returns the neighborhood of a player
        /// </summary>
        /// <param name="grid">The Grid</param>
        /// <param name="player">The player</param>
        /// <param name="cells">List of all cells on the grid</param>
        /// <returns>A neighborhood</returns>
        private Neighborhood PlayerNeighborhood(Grid grid, Player player, List<Cell> cells)
        {
            var nei = new Neighborhood();

            var sizeX = grid.SizeX;
            var sizeY = grid.SizeY;

            // Find my cell, type and coordinates
            var cell = cells.FirstOrDefault(c => c.PlayerId == player.PlayerId);
            if (cell != null)
            {
                var myCell = cell.CellNr;
                var myType = player.PlayerType;
                var myX = cell.X;
                var myY = cell.Y;

                // Find CellNr of neighbors
                List<int> neighborCells = new List<int>();
                var nCell = (myY == 1) ? myCell + (sizeY - 1) * sizeX : myCell - sizeX;      //N
                neighborCells.Add(nCell);
                var sCell = (myY == sizeY) ? myCell - (sizeY - 1) * sizeX : myCell + sizeX;
                neighborCells.Add(sCell);                                                     //S
                neighborCells.Add((myX == 1) ? myCell + (sizeX - 1) : myCell - 1);            //W
                neighborCells.Add((myX == sizeX) ? myCell - (sizeX - 1) : myCell + 1);        //E;
                neighborCells.Add((myX == 1) ? nCell + (sizeX - 1) : nCell - 1);              //NW
                neighborCells.Add((myX == sizeX) ? nCell - (sizeX - 1) : nCell + 1);          //NE
                neighborCells.Add((myX == 1) ? sCell + (sizeX - 1) : sCell - 1);              //SW
                neighborCells.Add((myX == sizeX) ? sCell - (sizeX - 1) : sCell + 1);          //SE

                // Iterate neighbors
                var ownTypeCount = 0;
                var otherTypeCount = 0;
                var empty = 0;

                foreach (var nr in neighborCells)
                {
                    var neighbor = cells.FirstOrDefault(c => c.CellNr == nr);
                    if (neighbor.Player != null)
                    {
                        if (neighbor.Player.PlayerType == myType) ownTypeCount++;
                        if (neighbor.Player.PlayerType != myType) otherTypeCount++;
                    }
                    else
                    {
                        empty++;
                    }
                }

                // Compute shares
                double ownShare = 0;
                double otherShare = 0;

                if ((ownTypeCount + otherTypeCount) > 0)
                {
                    ownShare = (double)ownTypeCount / ((double)ownTypeCount + (double)otherTypeCount);
                    otherShare = 1 - ownShare;
                }

                nei = new Neighborhood { PlayerId = player.PlayerId, OwnTypeCount = ownTypeCount, OtherTypeCount = otherTypeCount, EmptyCount = empty, OwnShare = ownShare, OtherShare = otherShare };
            }

            return nei;
        }
Пример #2
0
 public Player()
 {
     Neighborhood = new Neighborhood();
 }
Пример #3
0
 /// <summary>
 /// Return the 
 /// </summary>
 /// <param name="treatmentType"></param>
 /// <param name="nei"></param>
 /// <returns></returns>
 public double GetPayoffOld(Grid grid, Neighborhood nei)
 {
     switch (grid.TreatmentTypeId)
     {
         case 0: // Treatment is not set, make same as 1
             if (nei.OtherShare <= grid.UpperThreshold)
             {
                 return grid.HighPayoff;
             }
             else
             {
                 return 0;
             }
         case 1: // High Tolerance
             if (nei.OtherShare <= grid.UpperThreshold)
             {
                 return grid.HighPayoff;
             }
             else
             {
                 return 0;
             }
         case 2: // Extreme Tolerance
             if (nei.OtherShare <= grid.UpperThreshold)
             {
                 return grid.HighPayoff;
             }
             else
             {
                 return 0;
             }
         case 3: // Preference for mixing
             if (nei.OtherShare <= grid.LowerThreshold)
             {
                 return grid.LowPayoff;
             }
             else if (nei.OtherShare <= grid.UpperThreshold)
             {
                 return grid.HighPayoff;
             }
             else
             {
                 return 0;
             }
     }
     return -1;
 }