public Sandbox Play() { CreateTemporaryDirectory(); Grains.ToList().ForEach(x => x.Setup(this)); return(this); }
public void Dispose() { Grains.ToList().ForEach(x => x.Dispose()); TryDeleteFilesAndFoldersRecursively(Location); }
public void GrowGrains(int boardWidth, int boardHeight, double energy) { List <Grain> temporaryGrains = Grains.ToList(); Random rand = new Random(); while (temporaryGrains.Count > 0) { int randomIndeks = rand.Next(temporaryGrains.Count); int firstEnergy = CheckEnergy(temporaryGrains[randomIndeks].Position.X, temporaryGrains[randomIndeks].Position.Y, boardWidth, boardHeight); int xPosition = temporaryGrains[randomIndeks].Position.X; int yPosition = temporaryGrains[randomIndeks].Position.Y; int n = rand.Next(1, 8); temporaryGrains.Remove(temporaryGrains[randomIndeks]); switch (n) { case 1: if (xPosition > 0 && yPosition > 0) { int secondEnergy = CheckEnergy(xPosition - 1, yPosition - 1, boardWidth, boardHeight); double delta = energy * secondEnergy - firstEnergy; if (delta <= 0) { GrainsInPreviousStep[xPosition, yPosition].Alive = GrainsInPreviousStep[xPosition - 1, yPosition - 1].Alive; GrainsInPreviousStep[xPosition, yPosition].PenColor = GrainsInPreviousStep[xPosition - 1, yPosition - 1].PenColor; } } break; case 2: if (xPosition > 0 && yPosition < boardHeight - 1) { int secondEnergy = CheckEnergy(xPosition - 1, yPosition + 1, boardWidth, boardHeight); double delta = energy * secondEnergy - firstEnergy; if (delta <= 0) { GrainsInPreviousStep[xPosition, yPosition].Alive = GrainsInPreviousStep[xPosition - 1, yPosition + 1].Alive; GrainsInPreviousStep[xPosition, yPosition].PenColor = GrainsInPreviousStep[xPosition - 1, yPosition + 1].PenColor; } } break; case 3: if (xPosition > 0) { int secondEnergy = CheckEnergy(xPosition - 1, yPosition, boardWidth, boardHeight); double delta = energy * secondEnergy - firstEnergy; if (delta <= 0) { GrainsInPreviousStep[xPosition, yPosition].Alive = GrainsInPreviousStep[xPosition - 1, yPosition].Alive; GrainsInPreviousStep[xPosition, yPosition].PenColor = GrainsInPreviousStep[xPosition - 1, yPosition].PenColor; } } break; case 4: if (xPosition < boardWidth - 1 && yPosition > 0) { int secondEnergy = CheckEnergy(xPosition + 1, yPosition - 1, boardWidth, boardHeight); double delta = energy * secondEnergy - firstEnergy; if (delta <= 0) { GrainsInPreviousStep[xPosition, yPosition].Alive = GrainsInPreviousStep[xPosition + 1, yPosition - 1].Alive; GrainsInPreviousStep[xPosition, yPosition].PenColor = GrainsInPreviousStep[xPosition + 1, yPosition - 1].PenColor; } } break; case 5: if (yPosition > 0) { int secondEnergy = CheckEnergy(xPosition, yPosition - 1, boardWidth, boardHeight); double delta = energy * secondEnergy - firstEnergy; if (delta <= 0) { GrainsInPreviousStep[xPosition, yPosition].Alive = GrainsInPreviousStep[xPosition, yPosition - 1].Alive; GrainsInPreviousStep[xPosition, yPosition].PenColor = GrainsInPreviousStep[xPosition, yPosition - 1].PenColor; } } break; case 6: if (xPosition < boardWidth - 1 && yPosition < boardHeight - 1) { int secondEnergy = CheckEnergy(xPosition + 1, yPosition + 1, boardWidth, boardHeight); double delta = energy * secondEnergy - firstEnergy; if (delta <= 0) { GrainsInPreviousStep[xPosition, yPosition].Alive = GrainsInPreviousStep[xPosition + 1, yPosition + 1].Alive; GrainsInPreviousStep[xPosition, yPosition].PenColor = GrainsInPreviousStep[xPosition + 1, yPosition + 1].PenColor; } } break; case 7: if (xPosition < boardWidth - 1) { int secondEnergy = CheckEnergy(xPosition + 1, yPosition, boardWidth, boardHeight); double delta = energy * secondEnergy - firstEnergy; if (delta <= 0) { GrainsInPreviousStep[xPosition, yPosition].Alive = GrainsInPreviousStep[xPosition + 1, yPosition].Alive; GrainsInPreviousStep[xPosition, yPosition].PenColor = GrainsInPreviousStep[xPosition + 1, yPosition].PenColor; } } break; case 8: if (yPosition < boardHeight - 1) { int secondEnergy = CheckEnergy(xPosition, yPosition + 1, boardWidth, boardHeight); double delta = energy * secondEnergy - firstEnergy; if (delta <= 0) { GrainsInPreviousStep[xPosition, yPosition].Alive = GrainsInPreviousStep[xPosition, yPosition + 1].Alive; GrainsInPreviousStep[xPosition, yPosition].PenColor = GrainsInPreviousStep[xPosition, yPosition + 1].PenColor; } } break; } } }