mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-06 10:05:45 -08:00
Main game loop with Short Range Scan and Shield Control
This commit is contained in:
53
84 Super Star Trek/csharp/Systems/ShieldControl.cs
Normal file
53
84 Super Star Trek/csharp/Systems/ShieldControl.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using SuperStarTrek.Objects;
|
||||
using SuperStarTrek.Space;
|
||||
|
||||
namespace SuperStarTrek.Systems
|
||||
{
|
||||
internal class ShieldControl : Subsystem
|
||||
{
|
||||
private readonly Enterprise _enterprise;
|
||||
private readonly Output _output;
|
||||
private readonly Input _input;
|
||||
|
||||
public ShieldControl(Enterprise enterprise, Output output, Input input)
|
||||
: base("Shield Control", Command.SHE)
|
||||
{
|
||||
_enterprise = enterprise;
|
||||
_output = output;
|
||||
_input = input;
|
||||
}
|
||||
|
||||
public double Energy { get; private set; }
|
||||
|
||||
public override void ExecuteCommand(Quadrant quadrant)
|
||||
{
|
||||
if (Condition < 0)
|
||||
{
|
||||
_output.WriteLine("Shield Control inoperable");
|
||||
return;
|
||||
}
|
||||
|
||||
_output.WriteLine($"Energy available = {_enterprise.TotalEnergy}");
|
||||
var requested = _input.GetNumber($"Number of units to shields");
|
||||
|
||||
if (Validate(requested))
|
||||
{
|
||||
Energy = requested;
|
||||
return;
|
||||
}
|
||||
|
||||
_output.WriteLine("<SHIELDS UNCHANGED>");
|
||||
}
|
||||
|
||||
private bool Validate(double requested)
|
||||
{
|
||||
if (requested > _enterprise.TotalEnergy)
|
||||
{
|
||||
_output.WriteLine("Shield Control reports, 'This is not the Federation Treasury.'");
|
||||
return false;
|
||||
}
|
||||
|
||||
return requested >= 0 && requested != Energy;
|
||||
}
|
||||
}
|
||||
}
|
||||
59
84 Super Star Trek/csharp/Systems/ShortRangeSensors.cs
Normal file
59
84 Super Star Trek/csharp/Systems/ShortRangeSensors.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using SuperStarTrek.Objects;
|
||||
using SuperStarTrek.Resources;
|
||||
using SuperStarTrek.Space;
|
||||
|
||||
namespace SuperStarTrek.Systems
|
||||
{
|
||||
internal class ShortRangeSensors : Subsystem
|
||||
{
|
||||
private readonly Enterprise _enterprise;
|
||||
private readonly Galaxy _galaxy;
|
||||
private readonly Game _game;
|
||||
private readonly Output _output;
|
||||
|
||||
public ShortRangeSensors(Enterprise enterprise, Galaxy galaxy, Game game, Output output)
|
||||
: base("Short Range Sensors", Command.SRS)
|
||||
{
|
||||
_enterprise = enterprise;
|
||||
_galaxy = galaxy;
|
||||
_game = game;
|
||||
_output = output;
|
||||
}
|
||||
|
||||
public override void ExecuteCommand(Quadrant quadrant)
|
||||
{
|
||||
if (_enterprise.IsDocked)
|
||||
{
|
||||
_output.WriteLine(Strings.ShieldsDropped);
|
||||
}
|
||||
|
||||
if (Condition < 0)
|
||||
{
|
||||
_output.WriteLine(Strings.ShortRangeSensorsOut);
|
||||
}
|
||||
|
||||
_output.WriteLine("---------------------------------");
|
||||
quadrant.GetDisplayLines()
|
||||
.Zip(GetStatusLines(), (sectors, status) => $" {sectors} {status}")
|
||||
.ToList()
|
||||
.ForEach(l => _output.WriteLine(l));
|
||||
_output.WriteLine("---------------------------------");
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetStatusLines()
|
||||
{
|
||||
yield return $"Stardate {_game.Stardate}";
|
||||
yield return $"Condition {_enterprise.Condition}";
|
||||
yield return $"Quadrant {_enterprise.Quadrant}";
|
||||
yield return $"Sector {_enterprise.Sector}";
|
||||
yield return $"Photon torpedoes {_enterprise.TorpedoCount}";
|
||||
yield return $"Total energy {Math.Ceiling(_enterprise.Energy)}";
|
||||
yield return $"Shields {(int)_enterprise.Shields}";
|
||||
yield return $"Klingons remaining {_galaxy.KlingonCount}";
|
||||
}
|
||||
}
|
||||
}
|
||||
20
84 Super Star Trek/csharp/Systems/Subsystem.cs
Normal file
20
84 Super Star Trek/csharp/Systems/Subsystem.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using SuperStarTrek.Space;
|
||||
|
||||
namespace SuperStarTrek.Systems
|
||||
{
|
||||
internal abstract class Subsystem
|
||||
{
|
||||
protected Subsystem(string name, Command command)
|
||||
{
|
||||
Name = name;
|
||||
Command = command;
|
||||
Condition = 0;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public double Condition { get; }
|
||||
public Command Command { get; }
|
||||
|
||||
public abstract void ExecuteCommand(Quadrant quadrant);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user