From a1b257d5ae42ab95888955ccd732f23a811dd39e Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Wed, 3 Mar 2021 23:22:16 +1100 Subject: [PATCH] Add Long Range Sensors --- 84 Super Star Trek/csharp/Game.cs | 1 + 84 Super Star Trek/csharp/Random.cs | 2 +- 84 Super Star Trek/csharp/Space/Coordinate.cs | 10 ++--- 84 Super Star Trek/csharp/Space/Galaxy.cs | 17 +++++-- 84 Super Star Trek/csharp/Space/Quadrant.cs | 4 +- .../csharp/Space/QuadrantInfo.cs | 10 +++++ .../csharp/Systems/LongRangeSensors.cs | 44 +++++++++++++++++++ 7 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 84 Super Star Trek/csharp/Systems/LongRangeSensors.cs diff --git a/84 Super Star Trek/csharp/Game.cs b/84 Super Star Trek/csharp/Game.cs index c9d9c778..0547103b 100644 --- a/84 Super Star Trek/csharp/Game.cs +++ b/84 Super Star Trek/csharp/Game.cs @@ -82,6 +82,7 @@ namespace SuperStarTrek _enterprise = new Enterprise(3000, random.GetCoordinate(), _output); _enterprise .Add(new ShortRangeSensors(_enterprise, _galaxy, this, _output)) + .Add(new LongRangeSensors(_galaxy, _output)) .Add(new ShieldControl(_enterprise, _output, _input)) .Add(new DamageControl(_enterprise, _output)); diff --git a/84 Super Star Trek/csharp/Random.cs b/84 Super Star Trek/csharp/Random.cs index 9b7e1bff..460c1cef 100644 --- a/84 Super Star Trek/csharp/Random.cs +++ b/84 Super Star Trek/csharp/Random.cs @@ -6,7 +6,7 @@ namespace SuperStarTrek { private static readonly System.Random _random = new(); - public Coordinates GetCoordinate() => new Coordinates(Get1To8Inclusive(), Get1To8Inclusive()); + public Coordinates GetCoordinate() => new Coordinates(Get1To8Inclusive() - 1, Get1To8Inclusive() - 1); // Duplicates the algorithm used in the original code to get an integer value from 1 to 8, inclusive: // 475 DEF FNR(R)=INT(RND(R)*7.98+1.01) diff --git a/84 Super Star Trek/csharp/Space/Coordinate.cs b/84 Super Star Trek/csharp/Space/Coordinate.cs index 87fe6e89..aa5b5285 100644 --- a/84 Super Star Trek/csharp/Space/Coordinate.cs +++ b/84 Super Star Trek/csharp/Space/Coordinate.cs @@ -14,16 +14,16 @@ namespace SuperStarTrek.Space public int X { get; } public int Y { get; } - public int RegionIndex => ((X - 1) << 1) + ((Y - 1) >> 2); - public int SubRegionIndex => (Y - 1) % 4; + public int RegionIndex => (X << 1) + (Y >> 2); + public int SubRegionIndex => Y % 4; private int Validated(int value, string argumentName) { - if (value >= 1 && value <= 8) { return value; } + if (value >= 0 && value <= 7) { return value; } - throw new ArgumentOutOfRangeException(argumentName, value, "Must be 1 to 8 inclusive"); + throw new ArgumentOutOfRangeException(argumentName, value, "Must be 0 to 7 inclusive"); } - public override string ToString() => $"{X} , {Y}"; + public override string ToString() => $"{X+1} , {Y+1}"; } } diff --git a/84 Super Star Trek/csharp/Space/Galaxy.cs b/84 Super Star Trek/csharp/Space/Galaxy.cs index 88361645..0392b335 100644 --- a/84 Super Star Trek/csharp/Space/Galaxy.cs +++ b/84 Super Star Trek/csharp/Space/Galaxy.cs @@ -1,3 +1,5 @@ +using System.Collections; +using System.Collections.Generic; using System.Linq; using SuperStarTrek.Resources; @@ -22,9 +24,9 @@ namespace SuperStarTrek.Space var random = new Random(); _quadrants = Enumerable - .Range(1, 8) + .Range(0, 8) .Select(x => Enumerable - .Range(1, 8) + .Range(0, 8) .Select(y => new Coordinates(x, y)) .Select(c => QuadrantInfo.Create(c, GetQuadrantName(c))) .ToArray()) @@ -42,12 +44,21 @@ namespace SuperStarTrek.Space } } - public QuadrantInfo this[Coordinates coordinate] => _quadrants[coordinate.X - 1][coordinate.Y - 1]; + public QuadrantInfo this[Coordinates coordinate] => _quadrants[coordinate.X][coordinate.Y]; public int KlingonCount => _quadrants.SelectMany(q => q).Sum(q => q.KlingonCount); public int StarbaseCount => _quadrants.SelectMany(q => q).Count(q => q.HasStarbase); private static string GetQuadrantName(Coordinates coordinates) => $"{_regionNames[coordinates.RegionIndex]} {_subRegionIdentifiers[coordinates.SubRegionIndex]}"; + + public IEnumerable> GetNeighborhood(Quadrant quadrant) => + Enumerable.Range(-1, 3) + .Select(dx => dx + quadrant.Coordinates.X) + .Select(x => GetNeighborhoodRow(quadrant, x)); + private IEnumerable GetNeighborhoodRow(Quadrant quadrant, int x) => + Enumerable.Range(-1, 3) + .Select(dy => dy + quadrant.Coordinates.Y) + .Select(y => y < 0 || y > 7 || x < 0 || x > 7 ? null : _quadrants[x][y]); } } diff --git a/84 Super Star Trek/csharp/Space/Quadrant.cs b/84 Super Star Trek/csharp/Space/Quadrant.cs index f08fa18b..e483604f 100644 --- a/84 Super Star Trek/csharp/Space/Quadrant.cs +++ b/84 Super Star Trek/csharp/Space/Quadrant.cs @@ -66,13 +66,13 @@ namespace SuperStarTrek.Space } } - public IEnumerable GetDisplayLines() => Enumerable.Range(1, 8).Select(x => GetDisplayLine(x)); + public IEnumerable GetDisplayLines() => Enumerable.Range(0, 8).Select(x => GetDisplayLine(x)); private string GetDisplayLine(int x) => string.Join( " ", Enumerable - .Range(1, 8) + .Range(0, 8) .Select(y => new Coordinates(x, y)) .Select(c => _sectors.GetValueOrDefault(c)) .Select(o => o?.ToString() ?? " ")); diff --git a/84 Super Star Trek/csharp/Space/QuadrantInfo.cs b/84 Super Star Trek/csharp/Space/QuadrantInfo.cs index ed9ed3fa..20f21ee2 100644 --- a/84 Super Star Trek/csharp/Space/QuadrantInfo.cs +++ b/84 Super Star Trek/csharp/Space/QuadrantInfo.cs @@ -2,6 +2,8 @@ namespace SuperStarTrek.Space { internal class QuadrantInfo { + private bool _isKnown; + private QuadrantInfo(Coordinates coordinates, string name, int klingonCount, int starCount, bool hasStarbase) { Coordinates = coordinates; @@ -36,5 +38,13 @@ namespace SuperStarTrek.Space internal void AddKlingon() => KlingonCount += 1; internal void AddStarbase() => HasStarbase = true; + + public string Scan() + { + _isKnown = true; + return ToString(); + } + + public override string ToString() => _isKnown ? $"{KlingonCount}{(HasStarbase ? 1 : 0)}{StarCount}" : "***"; } } diff --git a/84 Super Star Trek/csharp/Systems/LongRangeSensors.cs b/84 Super Star Trek/csharp/Systems/LongRangeSensors.cs new file mode 100644 index 00000000..233af4a6 --- /dev/null +++ b/84 Super Star Trek/csharp/Systems/LongRangeSensors.cs @@ -0,0 +1,44 @@ + +using System; +using System.Collections.Generic; +using System.Linq; +using SuperStarTrek.Commands; +using SuperStarTrek.Objects; +using SuperStarTrek.Resources; +using SuperStarTrek.Space; + +namespace SuperStarTrek.Systems +{ + internal class LongRangeSensors : Subsystem + { + private readonly Galaxy _galaxy; + private readonly Output _output; + + public LongRangeSensors(Galaxy galaxy, Output output) + : base("Long Range Sensors", Command.LRS) + { + _galaxy = galaxy; + _output = output; + } + + public override CommandResult ExecuteCommand(Quadrant quadrant) + { + if (Condition < 0) + { + _output.WriteLine("Long Range Sensors are inoperable"); + } + else + { + _output.WriteLine($"Long range scan for quadrant {quadrant.Coordinates}"); + _output.WriteLine("-------------------"); + foreach (var quadrants in _galaxy.GetNeighborhood(quadrant)) + { + _output.WriteLine(": " + string.Join(" : ", quadrants.Select(q => q?.Scan() ?? "***")) + " :"); + _output.WriteLine("-------------------"); + } + } + + return CommandResult.Ok; + } + } +}