public Level FindAndCreateLevel(WaterCell wc) { int x = wc.X; int y = wc.Y; Level level = new Level(Levels.GetNextId(), this, wc); level.LevelY = y; Levels.Add(level); //TODO do we really need this? WaterCell initCell = WaterField.GetCell(x, y); level.AddCell(initCell); WaterCell cellRight = initCell.Right; while (cellRight != null) { level.AddCell(cellRight); cellRight = cellRight.Right; } WaterCell cellLeft = initCell.Left; while (cellLeft != null) { level.AddCell(cellLeft); cellLeft = cellLeft.Left; } level.SortAndFindUps(); return(level); }
public Level(int id, PipeGrid pipeGrid, WaterCell initCell) { Id = id; Cells = new List <WaterCell>(); UpCells = new List <WaterCell>(); PipeGrid = pipeGrid; UpCellsFilled = false; InitCell = initCell; }
public void Simulate(int xStart, int yStart, out bool success) { success = false; //TODO: move out of method WaterCell wcStart = WaterField.GetCell(xStart, yStart); wcStart.AddWater(); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { if (!IsPipe(x, y)) { continue; } if (!IsWater(x, y)) { continue; } WaterCell wc = WaterField.GetCell(x, y); //Thread.Sleep(50); if (wc.Down != null && wc.Down.Volume == 0) { wc.RemoveWater(); wc.Down.AddWater(); DrawGrid(wc, wc.Down); } else if (wc.Down != null && wc.Down.Volume == 1 && !wc.Down.LevelIsFull) { wc.RemoveWater(); var wcDownNextAside = wc.Down.NextAside; wcDownNextAside.AddWater(); DrawGrid(wc, wcDownNextAside); OnSimulateStep(new SimulateEventArgs("wc.Down != null && wc.Down.Volume == 1 && !wc.Down.LevelIsFull")); } else if (wc.Down != null && wc.Down.Volume == 1 && wc.LevelIsFull && !wc.MustBeSkipped) { var emptyConnectedNeighbor = wc.EmptyConnectedNeighbor; if (emptyConnectedNeighbor != null) { wc.RemoveWater(); emptyConnectedNeighbor.AddWater(); emptyConnectedNeighbor.MustBeSkipped = true; DrawGrid(wc, emptyConnectedNeighbor); OnSimulateStep(new SimulateEventArgs("emptyConnectedNeighbor")); } } } } }
public void Reset() { WaterArray = new WaterCell[Width, Height]; for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { WaterArray[x, y] = new WaterCell(x, y, PipeGrid); } } }
public WaterCell NextLowerVolumeLeft(WaterCell cell) { int ci = Cells.IndexOf(cell); for (int i = 1; ci - i >= 0; i++) { if (Cells[ci - i].Volume < cell.Volume) { return(Cells[ci - i]); } } return(null); }
public WaterCell NextLowerVolumeRight(WaterCell cell) { int ci = Cells.IndexOf(cell); for (int i = 1; i + ci < Cells.Count; i++) { if (Cells[ci + i].Volume < cell.Volume) { return(Cells[ci + i]); } } return(null); }
public WaterCell NextEmptyLeft(WaterCell cell) { int ci = Cells.IndexOf(cell); for (int i = 1; ci - i >= 0; i++) { if (Cells[ci - i].Volume == 0) { return(Cells[ci - i]); } } return(null); }
public WaterCell NextEmptyRight(WaterCell cell) { int ci = Cells.IndexOf(cell); for (int i = 1; i + ci < Cells.Count; i++) { if (Cells[ci + i].Volume == 0) { return(Cells[ci + i]); } } return(null); }
public void DrawGrid(WaterCell wcFrom, WaterCell wcTo) { if (GridPictureBox.InvokeRequired) { { GridPictureBox.Invoke(new MethodInvoker( delegate() { _DrawGrid(wcFrom, wcTo); })); return; } } else { _DrawGrid(wcFrom, wcTo); } }
public void RefreshWaterCellsGraph() { WaterCell wc = null; for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { if (!IsPipe(x, y)) { continue; } wc = WaterField.GetCell(x, y); wc.SetNeighbors(); } } }
public void _DrawGrid(WaterCell wcFrom, WaterCell wcTo) { //Clear for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { GridGraphics.FillRectangle(BaseBrush, x * _CellSize, y * _CellSize, _CellSize, _CellSize); } } //Grid for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { if (PipesField.IsBase(x, y)) { GridGraphics.DrawRectangle(GridPen, x * _CellSize, y * _CellSize, _CellSize, _CellSize); } } } //Pipes for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { if (PipesField.IsPipe(x, y)) { DrawPipe(x, y); } } } //Water for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { WaterCell wc = WaterField.GetCell(x, y); if (wcFrom != null && wc == wcFrom) { DrawFrom(wc.X, wc.Y, wc.Volume); } if (wc != null) { if (!wc.IsEmpty) { //DrawWater(x, y, wc.Volume, wc.Level.Pressure); DrawWater(x, y, wc.Volume); } } if (wcTo != null && wc == wcTo) { DrawTo(wc.X, wc.Y, wc.Volume); } } } for (int x = 0; x < Width; x++) { GridGraphics.DrawString(x.ToString(), fontVP, ValveBrush, x * _CellSize + 2, 2); } for (int y = 1; y < Width; y++) { GridGraphics.DrawString(y.ToString(), fontVP, ValveBrush, 2, y * _CellSize - 2); } //GridGraphics.FillRectangle(StartBrush, StartPoint.X * _CellSize + 1, StartPoint.Y * _CellSize + 1, _CellSize - 2, _CellSize - 2); //GridGraphics.FillRectangle(WaterBrush, StartPointInPixels.X, StartPointInPixels.Y, 1, 1); GridPictureBox.Image = Buffer; GridPictureBox.Refresh(); }
public void AddUpCell(WaterCell cell) { UpCells.Add(cell); }
public void AddCell(WaterCell cell) { Cells.Add(cell); cell.Level = this; }
public bool CellBelongsTo(WaterCell wc) { return(Cells.Contains(wc)); }