Add Long Range Sensors

This commit is contained in:
Andrew Cooper
2021-03-03 23:22:16 +11:00
parent cbcfebfeb3
commit a1b257d5ae
7 changed files with 77 additions and 11 deletions

View File

@@ -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));

View File

@@ -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)

View File

@@ -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}";
}
}

View File

@@ -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<IEnumerable<QuadrantInfo>> GetNeighborhood(Quadrant quadrant) =>
Enumerable.Range(-1, 3)
.Select(dx => dx + quadrant.Coordinates.X)
.Select(x => GetNeighborhoodRow(quadrant, x));
private IEnumerable<QuadrantInfo> 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]);
}
}

View File

@@ -66,13 +66,13 @@ namespace SuperStarTrek.Space
}
}
public IEnumerable<string> GetDisplayLines() => Enumerable.Range(1, 8).Select(x => GetDisplayLine(x));
public IEnumerable<string> 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() ?? " "));

View File

@@ -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}" : "***";
}
}

View File

@@ -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;
}
}
}