private void CheckInterval(XInterval xInterval) { if (xInterval.MinX < _minX) { _minX = xInterval.MinX; } if (xInterval.MaxX > _maxX) { _maxX = xInterval.MaxX; } if (xInterval.Y < _minY) { _minY = xInterval.Y; } if (xInterval.Y > _maxY) { _maxY = xInterval.Y; } }
private void BuildBoard() { var lines = _stream.ReadStringDocument(); const string pattern1 = @"^x=(\d+), y=(\d+)\.\.(\d+)"; const string pattern2 = @"^y=(\d+), x=(\d+)\.\.(\d+)"; foreach (var line in lines) { var match = Regex.Match(line, pattern2); if (match.Success) { var groups = match.Groups; var xInterval = new XInterval { Y = int.Parse(groups[1].Value), MinX = int.Parse(groups[2].Value), MaxX = int.Parse(groups[3].Value) }; _xIntervals.Add(xInterval); CheckInterval(xInterval); } else { match = Regex.Match(line, pattern1); if (match.Success) { var groups = match.Groups; var yInterval = new YInterval { X = int.Parse(groups[1].Value), MinY = int.Parse(groups[2].Value), MaxY = int.Parse(groups[3].Value) }; _yIntervals.Add(yInterval); CheckInterval(yInterval); } else { throw new Exception("Invalid"); } } } var xLen = _maxX - _minX + 1; var yLen = _maxY - _minY + 1; _board = new Cell[yLen, xLen + 2]; _boardMaxX = GetX(_maxX); _boardMaxY = GetY(_maxY); foreach (var item in _xIntervals) { var y = GetY(item.Y); for (int i = item.MinX; i <= item.MaxX; i++) { _board[y, GetX(i)] = new Cell { Type = Type.Sand }; } } foreach (var item in _yIntervals) { var x = GetX(item.X); for (int i = item.MinY; i <= item.MaxY; i++) { _board[GetY(i), x] = new Cell { Type = Type.Sand }; } } }