bool CheckForEnclosed(Point p) { bool _ret = false; Point left = p; Point right = p; bool rightWall = false; bool leftWall = false; bool falling = false; //if (this.CurrentBoard.Get(p) == '~') //return true; while (!leftWall && left.X > this.CurrentBoard.GetMinX() && !falling) { leftWall = (this.CurrentBoard.Get(left) == '#'); if (!(p.Down().Y > this.CurrentBoard.GetMaxY() - 1)) { falling = (this.CurrentBoard.Get(left.Down()) == '.'); } left = left.Left(); } while (!rightWall && right.X < this.CurrentBoard.GetMaxX() && !falling) { rightWall = (this.CurrentBoard.Get(right) == '#'); if (!(p.Down().Y > this.CurrentBoard.GetMaxY() - 1)) { falling = (this.CurrentBoard.Get(right.Down()) == '.'); } right = right.Right(); } if (rightWall && leftWall) { _ret = true; } return(_ret); }
public void Initializer() { thisExe = System.Reflection.Assembly.GetExecutingAssembly(); Regex regex = new Regex(@"^([xy]{1})={1}([0-9]+), [xy]{1}={1}([0-9]+).{2}([0-9]+)$"); List <Point> clayList = new List <Point>(); int xMin = 0; int xMax = 999999; int yMin = 999; using (Stream stream = thisExe.GetManifestResourceStream(resource)) using (StreamReader reader = new StreamReader(stream)) { string line; while ((line = reader.ReadLine()) != null) { Match match = regex.Match(line); if (match.Success) { if (match.Groups[1].Value == "x") { int x = Convert.ToInt32(match.Groups[2].Value); for (int i = Convert.ToInt32(match.Groups[3].Value); i <= Convert.ToInt32(match.Groups[4].Value); i++) { clayList.Add(new Point(x, i)); if (i < yMin) { yMin = i; } } } else if (match.Groups[1].Value == "y") { int y = Convert.ToInt32(match.Groups[2].Value); for (int i = Convert.ToInt32(match.Groups[3].Value); i <= Convert.ToInt32(match.Groups[4].Value); i++) { clayList.Add(new Point(i, y)); if (y < yMin) { yMin = y; } } } } } } this.height = clayList.OrderByDescending(o => o.Y).FirstOrDefault().Y + 1; xMin = clayList.OrderBy(o => o.X).FirstOrDefault().X - 2; xMax = clayList.OrderByDescending(o => o.X).FirstOrDefault().X + 2; this.width = xMax - xMin; // Setup board size currentBoard = new ModArray2D <char>(width, height, xMin, 0); currentBoard.yMin = yMin; // Fill board with sand currentBoard.Populate('.'); // Populate spring Point springPoint = new Point(500, 0); currentBoard.Set(500, 0, '+'); this.Cursors.Add(springPoint.Down()); // Setup clay foreach (Point p in clayList) { currentBoard.Set(p.X, p.Y, '#'); } this.pixelHeight = form.pbGameBoard.Size.Height / this.RowsToDisplay; this.pixelWidth = form.pbGameBoard.Size.Width / width; }
void DoWork(ref BackgroundWorker bg) { // If there are no more cursors, we should be done. if (Cursors.Count < 1) { this.complete = true; return; } List <Point> newCursors = new List <Point>(); foreach (Point p in Cursors) { Point down = p.Down(); Point up = p.Up(); Point left = p.Left(); Point right = p.Right(); bool enclosed = CheckForEnclosed(p); bool skip = false; // If the cursor has fallen below MaxY, drop that cursor path if (down.Y > this.CurrentBoard.GetMaxY() - 1) { this.CurrentBoard.Set(p, '|'); skip = true; } // If there's already water here else if (this.CurrentBoard.Get(down) == '|') { this.CurrentBoard.Set(p, '|'); skip = true; } // If it's falling through sand unobstructed else if (this.CurrentBoard.Get(down) == '.') { this.CurrentBoard.Set(p, '|'); if (!newCursors.Contains(down) && !Cursors.Contains(down)) { newCursors.Add(down); } } // It hits clay else if ((this.CurrentBoard.Get(down) == '#' || this.CurrentBoard.Get(down).IsWet())) { this.CurrentBoard.Set(p, '~'); while ((this.CurrentBoard.Get(left.Down()) == '#' || this.CurrentBoard.Get(left.Down()).IsWet()) && this.CurrentBoard.Get(left) != '#') { this.CurrentBoard.Set(left, '~'); if (this.CurrentBoard.Get(left.Left()) == '.' || this.CurrentBoard.Get(left.Left()) == '|') { left = left.Left(); } else { break; } } while ((this.CurrentBoard.Get(right.Down()) == '#' || this.CurrentBoard.Get(right.Down()).IsWet()) && this.CurrentBoard.Get(right) != '#') { this.CurrentBoard.Set(right, '~'); if (this.CurrentBoard.Get(right.Right()) == '.' || this.CurrentBoard.Get(right.Right()) == '|') { right = right.Right(); } else { break; } } if (this.CurrentBoard.Get(right.Down()) == '.') { Point tmpLeft = right.Left(); this.CurrentBoard.Set(p, '|'); if (!newCursors.Contains(right)) { newCursors.Add(right); } while (this.CurrentBoard.Get(tmpLeft).IsWet()) { this.CurrentBoard.Set(tmpLeft, '|'); tmpLeft = tmpLeft.Left(); } } if (this.CurrentBoard.Get(left.Down()) == '.') { Point tmpRight = left.Right(); this.CurrentBoard.Set(p, '|'); if (!newCursors.Contains(left)) { newCursors.Add(left); } while (this.CurrentBoard.Get(tmpRight).IsWet()) { this.CurrentBoard.Set(tmpRight, '|'); tmpRight = tmpRight.Right(); } } if (enclosed && !skip) { if (!newCursors.Contains(up)) { newCursors.Add(up); } } } } Cursors.Clear(); Cursors = new List <Point>(newCursors); if (Cursors.Count() > 0) { CurrentLine = Cursors.OrderByDescending(o => o.Y).First().Y; } if (Visualization) { SetupBoardVisual(); //Thread.Sleep(1); } }