Skip to content

Instantly share code, notes, and snippets.

@tslater2006
Created December 24, 2019 21:04
Show Gist options
  • Select an option

  • Save tslater2006/2da2ed90bdb6d8ad7a41af3056fe482f to your computer and use it in GitHub Desktop.

Select an option

Save tslater2006/2da2ed90bdb6d8ad7a41af3056fe482f to your computer and use it in GitHub Desktop.
Dictionary<int, bool[,]> EvolveWithLevels(Dictionary<int,bool[,]> levels)
{
Dictionary<int, bool[,]> newLevels = new Dictionary<int, bool[,]>();
foreach(var kvp in levels)
{
int curLevel = kvp.Key;
bool[,] map = kvp.Value;
bool[,] newMap = new bool[mapHeight, mapWidth];
for(var y = 0; y < map.GetLength(0); y++)
{
for (var x = 0; x < map.GetLength(1); x++)
{
if (x == 2 && y == 2)
{
/* skip the center */
continue;
}
// do stuff here
var curPoint = new Point(x, y);
int bugsAroundCount = curPoint.Around(0, 0, mapWidth - 1, mapHeight - 1).Count(p => levels[curLevel][p.Y, p.X]);
if (levels[curLevel][2,2])
{
bugsAroundCount--;
}
var parentLevel = curLevel - 1;
/* if we are on the outside border */
if (x == 0) /* left column */
{
if (levels.ContainsKey(parentLevel))
{
bugsAroundCount += levels[parentLevel][2, 1] == true ? 1 : 0;
}
else
{
if (newLevels.ContainsKey(parentLevel) == false)
{
newLevels.Add(parentLevel, new bool[mapHeight, mapWidth]);
}
}
}
else if (x == mapWidth - 1) /* right column */
{
if (levels.ContainsKey(parentLevel))
{
bugsAroundCount += levels[parentLevel][2, 3] == true ? 1 : 0;
}
else
{
if (newLevels.ContainsKey(parentLevel) == false)
{
newLevels.Add(parentLevel, new bool[mapHeight, mapWidth]);
}
}
}
if (y == 0) /* top row */
{
if (levels.ContainsKey(parentLevel))
{
bugsAroundCount += levels[parentLevel][1, 2] == true ? 1 : 0;
}
else
{
if (newLevels.ContainsKey(parentLevel) == false)
{
newLevels.Add(parentLevel, new bool[mapHeight, mapWidth]);
}
}
} else if (y == mapHeight - 1) /* bottom row */
{
if (levels.ContainsKey(parentLevel))
{
bugsAroundCount += levels[parentLevel][3, 2] == true ? 1 : 0;
}
else
{
if (newLevels.ContainsKey(parentLevel) == false)
{
newLevels.Add(parentLevel, new bool[mapHeight, mapWidth]);
}
}
}
/* check the inside border */
var childLevel = curLevel + 1;
if (x == 2 && y == 1) /* top cell inner ring */
{
/* whole top row of child */
if (levels.ContainsKey(childLevel))
{
Point[] childPoints = new Point[] { new Point(0, 0), new Point(1, 0), new Point(2, 0), new Point(3, 0), new Point(4, 0) };
bugsAroundCount += childPoints.Count(p => levels[childLevel][p.Y, p.X]);
} else
{
if (newLevels.ContainsKey(childLevel) == false)
{
newLevels.Add(childLevel, new bool[mapHeight, mapWidth]);
}
}
} else if (x == 2 && y == 3) /* bottom cell inner ring */
{
/* whole bottom row of child */
if (levels.ContainsKey(childLevel))
{
Point[] childPoints = new Point[] { new Point(0, 4), new Point(1, 4), new Point(2, 4), new Point(3, 4), new Point(4, 4) };
bugsAroundCount += childPoints.Count(p => levels[childLevel][p.Y, p.X]);
}
else
{
if (newLevels.ContainsKey(childLevel) == false)
{
newLevels.Add(childLevel, new bool[mapHeight, mapWidth]);
}
}
} else if (x == 1 && y == 2) /* left cell of inner ring */
{
/* left column of child */
if (levels.ContainsKey(childLevel))
{
Point[] childPoints = new Point[] { new Point(0, 0), new Point(0, 1), new Point(0, 2), new Point(0, 3), new Point(0, 4) };
bugsAroundCount += childPoints.Count(p => levels[childLevel][p.Y, p.X]);
}
else
{
if (newLevels.ContainsKey(childLevel) == false)
{
newLevels.Add(childLevel, new bool[mapHeight, mapWidth]);
}
}
}
else if (x == 3 && y == 2) /* right cell of inner ring */
{
/* right column of child */
if (levels.ContainsKey(childLevel))
{
Point[] childPoints = new Point[] { new Point(4, 0), new Point(4, 1), new Point(4, 2), new Point(4, 3), new Point(4, 4) };
bugsAroundCount += childPoints.Count(p => levels[childLevel][p.Y, p.X]);
}
else
{
if (newLevels.ContainsKey(childLevel) == false)
{
newLevels.Add(childLevel, new bool[mapHeight, mapWidth]);
}
}
}
if (levels[curLevel][y, x]) /* currently has a bug */
{
if (bugsAroundCount != 1)
{
newMap[y, x] = false;
}
else
{
newMap[y, x] = true;
}
}
else
{
if (bugsAroundCount == 1 || bugsAroundCount == 2)
{
newMap[y, x] = true;
}
else
{
newMap[y, x] = false;
}
}
}
}
newLevels.Add(curLevel, newMap);
}
return newLevels;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment