diff --git a/84 Super Star Trek/csharp/Command.cs b/84 Super Star Trek/csharp/Commands/Command.cs similarity index 95% rename from 84 Super Star Trek/csharp/Command.cs rename to 84 Super Star Trek/csharp/Commands/Command.cs index 33d9fd01..f0d569f8 100644 --- a/84 Super Star Trek/csharp/Command.cs +++ b/84 Super Star Trek/csharp/Commands/Command.cs @@ -1,6 +1,6 @@ using System.ComponentModel; -namespace SuperStarTrek +namespace SuperStarTrek.Commands { internal enum Command { diff --git a/84 Super Star Trek/csharp/CommandExtensions.cs b/84 Super Star Trek/csharp/Commands/CommandExtensions.cs similarity index 91% rename from 84 Super Star Trek/csharp/CommandExtensions.cs rename to 84 Super Star Trek/csharp/Commands/CommandExtensions.cs index 2b69ea31..f8f7e9da 100644 --- a/84 Super Star Trek/csharp/CommandExtensions.cs +++ b/84 Super Star Trek/csharp/Commands/CommandExtensions.cs @@ -1,7 +1,7 @@ using System.Reflection; using System.ComponentModel; -namespace SuperStarTrek +namespace SuperStarTrek.Commands { internal static class CommandExtensions { diff --git a/84 Super Star Trek/csharp/Commands/CommandResult.cs b/84 Super Star Trek/csharp/Commands/CommandResult.cs new file mode 100644 index 00000000..d92fba09 --- /dev/null +++ b/84 Super Star Trek/csharp/Commands/CommandResult.cs @@ -0,0 +1,23 @@ +namespace SuperStarTrek.Commands +{ + internal class CommandResult + { + public static readonly CommandResult Ok = new(false); + public static readonly CommandResult GameOver = new(true); + + private CommandResult(bool isGameOver) + { + IsGameOver = isGameOver; + } + + private CommandResult(double timeElapsed) + { + TimeElapsed = timeElapsed; + } + + public bool IsGameOver { get; } + public double TimeElapsed { get; } + + public static CommandResult Elapsed(double timeElapsed) => new(timeElapsed); + } +} diff --git a/84 Super Star Trek/csharp/Game.cs b/84 Super Star Trek/csharp/Game.cs index eb9fe03c..c9d9c778 100644 --- a/84 Super Star Trek/csharp/Game.cs +++ b/84 Super Star Trek/csharp/Game.cs @@ -43,9 +43,50 @@ namespace SuperStarTrek public void Play() { - var quadrant = Initialise(); + Initialise(); var gameOver = false; + while (!gameOver) + { + var command = _input.GetCommand(); + + var result = _enterprise.Execute(command); + + gameOver = result.IsGameOver || CheckIfStranded(); + _currentStardate += result.TimeElapsed; + } + + if (_galaxy.KlingonCount > 0) + { + _output.Write(Strings.EndOfMission, _currentStardate, _galaxy.KlingonCount); + } + else + { + _output.Write(Strings.Congratulations, GetEfficiency()); + } + } + + private void Initialise() + { + var random = new Random(); + + _currentStardate = _initialStardate = random.GetInt(20, 40) * 100; + _finalStarDate = _initialStardate + random.GetInt(25, 35); + + _currentQuadrant = random.GetCoordinate(); + _currentSector = random.GetCoordinate(); + + _galaxy = new Galaxy(); + _initialKlingonCount = _galaxy.KlingonCount; + + _enterprise = new Enterprise(3000, random.GetCoordinate(), _output); + _enterprise + .Add(new ShortRangeSensors(_enterprise, _galaxy, this, _output)) + .Add(new ShieldControl(_enterprise, _output, _input)) + .Add(new DamageControl(_enterprise, _output)); + + var quadrant = new Quadrant(_galaxy[_currentQuadrant], _enterprise); + _output.Write(Strings.Enterprise); _output.Write( Strings.Orders, @@ -59,58 +100,14 @@ namespace SuperStarTrek _input.WaitForAnyKeyButEnter("when ready to accept command"); _enterprise.Enter(quadrant, Strings.StartText); - - while (!gameOver) - { - var command = _input.GetCommand(); - - gameOver = command == Command.XXX || _enterprise.Execute(command) || CheckIfStranded(); - } - - if (_galaxy.KlingonCount > 0) - { - _output.Write(Strings.EndOfMission, _currentStardate, _galaxy.KlingonCount); - } - else - { - _output.Write(Strings.Congratulations, GetEfficiency()); - } - } - - private Quadrant Initialise() - { - var random = new Random(); - - _currentStardate = _initialStardate = random.GetInt(20, 40) * 100; - _finalStarDate = _initialStardate + random.GetInt(25, 35); - - _currentQuadrant = random.GetCoordinate(); - _currentSector = random.GetCoordinate(); - - _galaxy = new Galaxy(); - _initialKlingonCount = _galaxy.KlingonCount; - - _enterprise = new Enterprise(3000, random.GetCoordinate()); - _enterprise - .Add(new ShortRangeSensors(_enterprise, _galaxy, this, _output)) - .Add(new ShieldControl(_enterprise, _output, _input)) - .Add(new DamageControl(_enterprise, _output)); - - return new Quadrant(_galaxy[_currentQuadrant], _enterprise); } public bool Replay() => _galaxy.StarbaseCount > 0 && _input.GetString(Strings.ReplayPrompt, "Aye"); private bool CheckIfStranded() { - if (_enterprise.TotalEnergy < 10 || - _enterprise.Energy < 10 && _enterprise.Shields.IsDamaged) - { - _output.Write(Strings.Stranded); - return true; - } - - return false; + if (_enterprise.IsStranded) { _output.Write(Strings.Stranded); } + return _enterprise.IsStranded; } private double GetEfficiency() => diff --git a/84 Super Star Trek/csharp/Input.cs b/84 Super Star Trek/csharp/Input.cs index 3e2d7972..d86b7baf 100644 --- a/84 Super Star Trek/csharp/Input.cs +++ b/84 Super Star Trek/csharp/Input.cs @@ -1,5 +1,7 @@ using System; using System.Linq; +using SuperStarTrek.Commands; + using static System.StringComparison; namespace SuperStarTrek diff --git a/84 Super Star Trek/csharp/Objects/Enterprise.cs b/84 Super Star Trek/csharp/Objects/Enterprise.cs index e8447fca..97e5e3cd 100644 --- a/84 Super Star Trek/csharp/Objects/Enterprise.cs +++ b/84 Super Star Trek/csharp/Objects/Enterprise.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using SuperStarTrek.Commands; using SuperStarTrek.Resources; using SuperStarTrek.Space; using SuperStarTrek.Systems; @@ -11,29 +12,32 @@ namespace SuperStarTrek.Objects internal class Enterprise { private readonly int _maxEnergy; + private readonly Output _output; private readonly List _systems; private readonly Dictionary _commandExecutors; private Quadrant _quadrant; - public Enterprise(int maxEnergy, Coordinates sector) + public Enterprise(int maxEnergy, Coordinates sector, Output output) { Sector = sector; TotalEnergy = _maxEnergy = maxEnergy; _systems = new List(); _commandExecutors = new Dictionary(); + _output = output; } public Coordinates Quadrant => _quadrant.Coordinates; public Coordinates Sector { get; } public string Condition => GetCondition(); - public ShieldControl Shields => (ShieldControl)_commandExecutors[Command.SHE]; - public double Energy => TotalEnergy - Shields.Energy; + public ShieldControl ShieldControl => (ShieldControl)_commandExecutors[Command.SHE]; + public double Energy => TotalEnergy - ShieldControl.ShieldEnergy; public double TotalEnergy { get; private set; } public int DamagedSystemCount => _systems.Count(s => s.IsDamaged); public IEnumerable Systems => _systems; public int TorpedoCount { get; } - public bool IsDocked { get; private set; } + public bool IsDocked => _quadrant.EnterpriseIsNextToStarbase; + public bool IsStranded => TotalEnergy < 10 || Energy < 10 && ShieldControl.IsDamaged; public Enterprise Add(Subsystem system) { @@ -47,17 +51,14 @@ namespace SuperStarTrek.Objects { _quadrant = quadrant; - var _output = new Output(); _output.Write(entryTextFormat, quadrant); if (quadrant.HasKlingons) { _output.Write(Strings.CombatArea); - if (Shields.Energy <= 200) { _output.Write(Strings.LowShields); } + if (ShieldControl.ShieldEnergy <= 200) { _output.Write(Strings.LowShields); } } - IsDocked = quadrant.EnterpriseIsNextToStarbase; - Execute(Command.SRS); } @@ -69,10 +70,11 @@ namespace SuperStarTrek.Objects _ => "Green" }; - public bool Execute(Command command) + public CommandResult Execute(Command command) { - _commandExecutors[command].ExecuteCommand(_quadrant); - return false; + if (command == Command.XXX) { return CommandResult.GameOver; } + + return _commandExecutors[command].ExecuteCommand(_quadrant); } internal bool Recognises(string command) diff --git a/84 Super Star Trek/csharp/Systems/DamageControl.cs b/84 Super Star Trek/csharp/Systems/DamageControl.cs index 5cfbda7e..b4587add 100644 --- a/84 Super Star Trek/csharp/Systems/DamageControl.cs +++ b/84 Super Star Trek/csharp/Systems/DamageControl.cs @@ -1,3 +1,4 @@ +using SuperStarTrek.Commands; using SuperStarTrek.Objects; using SuperStarTrek.Space; @@ -15,7 +16,7 @@ namespace SuperStarTrek.Systems _output = output; } - public override void ExecuteCommand(Quadrant quadrant) + public override CommandResult ExecuteCommand(Quadrant quadrant) { if (IsDamaged) { @@ -31,8 +32,11 @@ namespace SuperStarTrek.Systems if (quadrant.Starbase.TryRepair(_enterprise, out var repairTime)) { WriteDamageReport(); + return CommandResult.Elapsed(repairTime); } } + + return CommandResult.Ok; } public void WriteDamageReport() diff --git a/84 Super Star Trek/csharp/Systems/ShieldControl.cs b/84 Super Star Trek/csharp/Systems/ShieldControl.cs index e7aca413..9c4a3c16 100644 --- a/84 Super Star Trek/csharp/Systems/ShieldControl.cs +++ b/84 Super Star Trek/csharp/Systems/ShieldControl.cs @@ -1,3 +1,4 @@ +using SuperStarTrek.Commands; using SuperStarTrek.Objects; using SuperStarTrek.Space; @@ -17,26 +18,35 @@ namespace SuperStarTrek.Systems _input = input; } - public double Energy { get; private set; } + public double ShieldEnergy { get; private set; } - public override void ExecuteCommand(Quadrant quadrant) + public override CommandResult ExecuteCommand(Quadrant quadrant) { if (Condition < 0) { _output.WriteLine("Shield Control inoperable"); - return; + } + else + { + UpdateShields(); } + return CommandResult.Ok; + } + + private void UpdateShields() + { _output.WriteLine($"Energy available = {_enterprise.TotalEnergy}"); var requested = _input.GetNumber($"Number of units to shields"); if (Validate(requested)) { - Energy = requested; - return; + ShieldEnergy = requested; + } + else + { + _output.WriteLine(""); } - - _output.WriteLine(""); } private bool Validate(double requested) @@ -47,7 +57,7 @@ namespace SuperStarTrek.Systems return false; } - return requested >= 0 && requested != Energy; + return requested >= 0 && requested != ShieldEnergy; } } } diff --git a/84 Super Star Trek/csharp/Systems/ShortRangeSensors.cs b/84 Super Star Trek/csharp/Systems/ShortRangeSensors.cs index 6632705c..953ec66e 100644 --- a/84 Super Star Trek/csharp/Systems/ShortRangeSensors.cs +++ b/84 Super Star Trek/csharp/Systems/ShortRangeSensors.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using SuperStarTrek.Commands; using SuperStarTrek.Objects; using SuperStarTrek.Resources; using SuperStarTrek.Space; @@ -24,7 +25,7 @@ namespace SuperStarTrek.Systems _output = output; } - public override void ExecuteCommand(Quadrant quadrant) + public override CommandResult ExecuteCommand(Quadrant quadrant) { if (_enterprise.IsDocked) { @@ -42,6 +43,8 @@ namespace SuperStarTrek.Systems .ToList() .ForEach(l => _output.WriteLine(l)); _output.WriteLine("---------------------------------"); + + return CommandResult.Ok; } public IEnumerable GetStatusLines() @@ -52,7 +55,7 @@ namespace SuperStarTrek.Systems yield return $"Sector {_enterprise.Sector}"; yield return $"Photon torpedoes {_enterprise.TorpedoCount}"; yield return $"Total energy {Math.Ceiling(_enterprise.TotalEnergy)}"; - yield return $"Shields {(int)_enterprise.Shields.Energy}"; + yield return $"Shields {(int)_enterprise.ShieldControl.ShieldEnergy}"; yield return $"Klingons remaining {_galaxy.KlingonCount}"; } } diff --git a/84 Super Star Trek/csharp/Systems/Subsystem.cs b/84 Super Star Trek/csharp/Systems/Subsystem.cs index 9cb461ce..53398b41 100644 --- a/84 Super Star Trek/csharp/Systems/Subsystem.cs +++ b/84 Super Star Trek/csharp/Systems/Subsystem.cs @@ -1,3 +1,4 @@ +using SuperStarTrek.Commands; using SuperStarTrek.Space; namespace SuperStarTrek.Systems @@ -16,7 +17,7 @@ namespace SuperStarTrek.Systems public bool IsDamaged => Condition < 0; public Command Command { get; } - public abstract void ExecuteCommand(Quadrant quadrant); + public abstract CommandResult ExecuteCommand(Quadrant quadrant); public void Repair() { if (Condition < 0) { Condition = 0; }