mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-29 14:15:08 -08:00
Add CommandResult
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace SuperStarTrek
|
||||
namespace SuperStarTrek.Commands
|
||||
{
|
||||
internal enum Command
|
||||
{
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.Reflection;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace SuperStarTrek
|
||||
namespace SuperStarTrek.Commands
|
||||
{
|
||||
internal static class CommandExtensions
|
||||
{
|
||||
23
84 Super Star Trek/csharp/Commands/CommandResult.cs
Normal file
23
84 Super Star Trek/csharp/Commands/CommandResult.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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() =>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using SuperStarTrek.Commands;
|
||||
|
||||
using static System.StringComparison;
|
||||
|
||||
namespace SuperStarTrek
|
||||
|
||||
@@ -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<Subsystem> _systems;
|
||||
private readonly Dictionary<Command, Subsystem> _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<Subsystem>();
|
||||
_commandExecutors = new Dictionary<Command, Subsystem>();
|
||||
_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<Subsystem> 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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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("<SHIELDS UNCHANGED>");
|
||||
}
|
||||
|
||||
_output.WriteLine("<SHIELDS UNCHANGED>");
|
||||
}
|
||||
|
||||
private bool Validate(double requested)
|
||||
@@ -47,7 +57,7 @@ namespace SuperStarTrek.Systems
|
||||
return false;
|
||||
}
|
||||
|
||||
return requested >= 0 && requested != Energy;
|
||||
return requested >= 0 && requested != ShieldEnergy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<string> 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}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
|
||||
Reference in New Issue
Block a user