title: change 2d array values wave like
body: <p>I have a two-dimensional array, each cell stores a value. At some point, the value of some cell changes. Let's call that cell Original. I want the values of all neighboring cells to change, say, to add some small value. I did it, but what to do with the neighbors of the neighbors? I wrote this method, but for some reason the result looks irregulary. It would be great if you could help me or at least tell me what to google because I can't find anything useful.</p>
  <p>My method:</p>
  <pre><code>public void UpdateNearbyPrices(
      ProductType productType, Cell[,] cells, int depth, List&lt;Cell&gt; updated)
  {
      if (depth == 0) return;
      var neighbours = GetNeighbours(cells);
      var deltaPrice = depth * 0.10f;
      foreach (Cell n in neighbours)
      {
          if (updated.Contains(n)) continue;
          n.GetProduct(productType).Price += deltaPrice;
          updated.Add(n);
      }
  
      foreach (Cell n in neighbours)
      {
          n.UpdateNearbyPrices(productType, cells, depth - 1, updated);
      }
  }
  </code></pre>
  <p>Cell is a class that stores list of products. Let's say it has only one product and value is the product's price. I display prices on grid.
  I call it from cell class like this:</p>
  <pre><code>List&lt;Cell&gt; updated = new List&lt;Cell&gt;();
  updated.Add(this);
  var depth = (int)(product.Price / 10) - 1;
  UpdateNearbyPrices(productType, cells, Mathf.Min(depth, 5), updated);
  </code></pre>
  <p>When I run the app I add some value to product's price, expecting to see more or less stone-thrown-into-a-puddle-like behaviour, but I see this:
  <a href="https://i.imgur.com/ZZCCXxl.png" rel="nofollow noreferrer">mypicture</a>. The greenest cell is the Original cell. This project is made in Godot.</p>
  <p>Minimal reproductive console app:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace UpdateNearbyPrices
  {
      class Program
      {
          static void Main(string[] args)
          {
              int rows = 10; int cols = 10;
              Cell[,] cells = new Cell[rows, cols];
  
              for (int i = 0; i &lt; rows; i++)
              for (int j = 0; j &lt; cols; j++)
              {
                  var cell = new Cell 
                  { 
                      Id = i.ToString() + '_' + j.ToString(),
                      ProductList = new List&lt;Product&gt; { new Product() }
                  };
                  cells[i, j] = cell;
              }
  
              while(true)
              {
                  cells[5, 5].SetNewPrice(ProductType.Lumber, cells);
                  Console.Clear();
                  Print(cells);
                  Console.ReadLine();
              }
          }
  
          public static void Print(Cell[,] cells)
          {
              for (int i = 0; i &lt; cells.GetLength(0); i++)
              {
                  for (int j = 0; j &lt; cells.GetLength(1); j++)
                  {
                      float value = cells[i, j].GetProduct(ProductType.Lumber).Price;
                      Console.Write(value.ToString(&quot;000.00&quot;) + &quot;|&quot;);
                  }
  
                  Console.WriteLine(&quot;\n----------------------------------------------------------------------&quot;);
              }
              Console.WriteLine(&quot;Press Enter to 0.5f to cell[5, 5].&quot;);
          }
      }
  
      public class Cell
      {
          public string Id { get; set; }
          public List&lt;Product&gt; ProductList { get; set; }
  
          public Product GetProduct(ProductType type) =&gt; ProductList.First(p =&gt; p.ProductType == type);
  
          internal void SetNewPrice(ProductType productType, Cell[,] cells)
          {
              Product product = GetProduct(productType);
              product.Price += 0.5f;
  
              //change neighbours prices
              List&lt;Cell&gt; updated = new List&lt;Cell&gt;();
              updated.Add(this);
              var depth = (int)(product.Price / 10) - 1;
              UpdateNearbyPrices(productType, cells, Math.Min(depth, 5), updated); 
          }
  
          public void UpdateNearbyPrices(
              ProductType productType, Cell[,] cells, int depth, List&lt;Cell&gt; updated)
          {
              if (depth == 0) return;
              var neighbours = GetNeighbours(cells);
              var deltaPrice = depth * 0.10f;
              foreach (Cell n in neighbours)
              {
                  if (updated.Contains(n)) continue;
                  n.GetProduct(productType).Price += deltaPrice;
                  updated.Add(n);
              }
  
              foreach (Cell n in neighbours)
              {
                  n.UpdateNearbyPrices(productType, cells, depth - 1, updated);
              }
          }
  
          public List&lt;Cell&gt; GetNeighbours(Cell[,] cells)
          {
              int row = int.Parse(Id.Split('_')[0]);
              int col = int.Parse(Id.Split('_')[1]);
              List&lt;Cell&gt; neighbours = new List&lt;Cell&gt;();
  
              for (var dy = -1; dy &lt;= 1; dy++)
                  for (var dx = -1; dx &lt;= 1; dx++)
                  {
                      if (dx == 0 &amp;&amp; dy == 0) continue;
                      if (row + dx &lt; 0 || col + dy &lt; 0) continue;
                      if (row + dx &gt; cells.GetLength(0) - 1 || col + dy &gt; cells.GetLength(1) - 1) continue;
  
                      Cell neighbour = cells[row + dx, col + dy];
                      neighbours.Add(neighbour);
                  }
              return neighbours;
          }
      }
  
      public class Product
      {
          public ProductType ProductType { get; set; } = ProductType.Lumber;
          public float Price { get; set; } = 30;
      }
  
      public enum ProductType
      {
          Lumber
      }
  }
  </code></pre>