public Int32 SampleInt32() { Int32 sum = 0; for (Int32 i = 0; i < Iterations; i++) { sum = unchecked (sum + _uniformInt32.Sample(_rng)); } return(sum); }
/// <summary> /// Changes the current medium, and resets the program. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MaterialsList_SelectedIndexChanged(object sender, EventArgs e) { _timer.Enabled = false; _startButton.Enabled = true; _pauseButton.Enabled = false; _resumeButton.Enabled = false; _clearButton.Enabled = false; _timeElapsed = 0; _timeShown.Text = _timeElapsed.ToString("E03"); int selected = _materialsList.SelectedIndex; if (selected == 5) { var rng = SmallRng.Create(); selected = _dist.Sample(rng); } if (selected == 0) { _current = _glass; } else if (selected == 1) { _current = _diamond; } else if (selected == 2) { _current = _water; } else if (selected == 3) { _current = _air; } else if (selected == 4) { _current = _brett; } _ycurrent = new YValues(); _ypast = new YValues(); _yfuture = new YValues(); }
public IEnumerable <RoverAction> Simulate(ScratchRover rover) { while (true) { yield return(RoverAction.CollectSample); yield return(RoverAction.ProcessSamples); yield return(RoverAction.Transmit); Int32 num = _directionDist.Sample(_rng); if (num == 0) { yield return(new RoverAction(Direction.Up)); } else if (num == 1) { yield return(new RoverAction(Direction.Right)); } else if (num == 2) { yield return(new RoverAction(Direction.Down)); } else if (num == 3) { yield return(new RoverAction(Direction.Left)); } if (rover.IsHalted) { yield break; } yield return(RoverAction.CollectPower); } }
private static readonly Bernoulli _wallClearChance = Bernoulli.FromRatio(1, 5); // 20% chance of clearing a wall public Level Generate(SimulationParameters parameters, Int32 rngSeed) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } Pcg32 rng = Pcg32.Create((UInt64)rngSeed, 0); Int32 width = parameters.BottomRight.X + 1; Int32 height = parameters.BottomRight.Y + 1; TerrainType[,] terrain = new TerrainType[width, height]; Position start = parameters.InitialPosition; Stack <CoordinatePair> stack = new Stack <CoordinatePair>(); HashSet <CoordinatePair> visited = new HashSet <CoordinatePair>(); stack.Push(start); visited.Add(start); terrain[start.X, start.Y] = TerrainType.Smooth; while (stack.Count > 0) { var current = stack.Peek(); Int32 offset = _directionDist.Sample(rng); Boolean anyNewNeighbors = false; for (Int32 i = 0; i < Direction.DirectionCount; i++) { Direction direction = (Direction)((i + offset) % Direction.DirectionCount); var passage = current + direction; var neighbor = passage + direction; var boundaryCheck = neighbor + direction; if (!parameters.BottomRight.Contains(boundaryCheck) || visited.Contains(neighbor)) { continue; } anyNewNeighbors = true; terrain[passage.X, passage.Y] = TerrainType.Smooth; terrain[neighbor.X, neighbor.Y] = TerrainType.Smooth; visited.Add(neighbor); stack.Push(neighbor); } if (!anyNewNeighbors) { if (_wallClearChance.Sample(rng)) { Direction direction = (Direction)_directionDist.Sample(rng); var passage = current + direction; var boundaryCheck = passage + direction; if (parameters.BottomRight.Contains(boundaryCheck) && terrain[passage.X, passage.Y] == TerrainType.Impassable) { terrain[passage.X, passage.Y] = TerrainType.Rough; } } stack.Pop(); } } return(new Level(terrain, new ProtoLevel(parameters, this, rngSeed))); }
public IEnumerable <RoverAction> Simulate(ScratchRover rover) { while (true) { Direction smoothSquare = Direction.None; SenseAdjacentSquares(rover); for (Int32 i = 0; i < Direction.DirectionCount; i++) { if (adjacentSquares[i] == TerrainType.Smooth) { smoothSquare = (Direction)i; break; } } if (rover.Power < 30 || (smoothSquare == Direction.None && adjacentSquares[4] == TerrainType.Smooth)) { if (rover.Power < 51 * rover.MovesLeft) { yield return(RoverAction.CollectPower); } } if (rover.MovesLeft < 3) { if (rover.MovesLeft == 2) { yield return(RoverAction.ProcessSamples); yield return(RoverAction.Transmit); yield break; } yield return(RoverAction.Transmit); } if (rover.Power < 41) { if (rover.Power > 10) { yield return(RoverAction.CollectPower); } if (rover.Power < 41) { yield return(RoverAction.Transmit); } } if (adjacentSquares[4] == TerrainType.Smooth || adjacentSquares[4] == TerrainType.Rough) { yield return(RoverAction.CollectSample); if (rover.SamplesCollected >= 3) { yield return(RoverAction.ProcessSamples); } } if (adjacentSquares.Contains(TerrainType.Smooth)) { if (adjacentSquares[0] == TerrainType.Smooth) { yield return(new RoverAction(Direction.Up)); } else if (adjacentSquares[1] == TerrainType.Smooth) { yield return(new RoverAction(Direction.Right)); } else if (adjacentSquares[2] == TerrainType.Smooth) { yield return(new RoverAction(Direction.Down)); } else if (adjacentSquares[3] == TerrainType.Smooth) { yield return(new RoverAction(Direction.Left)); } } else { Int32 num = _directionDist.Sample(_rng); if (num == 0) { yield return(new RoverAction(Direction.Up)); } else if (num == 1) { yield return(new RoverAction(Direction.Right)); } else if (num == 2) { yield return(new RoverAction(Direction.Down)); } else if (num == 3) { yield return(new RoverAction(Direction.Left)); } } } }