Reduce precision to float

This commit is contained in:
Andrew Cooper
2021-03-07 18:27:55 +11:00
parent 345545a27d
commit d0ed8d2f35
10 changed files with 35 additions and 35 deletions

View File

@@ -10,14 +10,14 @@ namespace SuperStarTrek.Commands
IsGameOver = isGameOver; IsGameOver = isGameOver;
} }
private CommandResult(double timeElapsed) private CommandResult(float timeElapsed)
{ {
TimeElapsed = timeElapsed; TimeElapsed = timeElapsed;
} }
public bool IsGameOver { get; } public bool IsGameOver { get; }
public double TimeElapsed { get; } public float TimeElapsed { get; }
public static CommandResult Elapsed(double timeElapsed) => new(timeElapsed); public static CommandResult Elapsed(float timeElapsed) => new(timeElapsed);
} }
} }

View File

@@ -14,7 +14,7 @@ namespace SuperStarTrek
private int _initialStardate; private int _initialStardate;
private int _finalStarDate; private int _finalStarDate;
private double _currentStardate; private float _currentStardate;
private Coordinates _currentQuadrant; private Coordinates _currentQuadrant;
private Coordinates _currentSector; private Coordinates _currentSector;
private Galaxy _galaxy; private Galaxy _galaxy;
@@ -27,7 +27,7 @@ namespace SuperStarTrek
_input = new Input(_output); _input = new Input(_output);
} }
public double Stardate => _currentStardate; public float Stardate => _currentStardate;
public void DoIntroduction() public void DoIntroduction()
{ {
@@ -111,7 +111,7 @@ namespace SuperStarTrek
return _enterprise.IsStranded; return _enterprise.IsStranded;
} }
private double GetEfficiency() => private float GetEfficiency() =>
1000 * Math.Pow(_initialKlingonCount / (_currentStardate - _initialStardate), 2); 1000 * (float)Math.Pow(_initialKlingonCount / (_currentStardate - _initialStardate), 2);
} }
} }

View File

@@ -33,8 +33,8 @@ namespace SuperStarTrek.Objects
public Coordinates Sector { get; } public Coordinates Sector { get; }
public string Condition => GetCondition(); public string Condition => GetCondition();
public ShieldControl ShieldControl => (ShieldControl)_commandExecutors[Command.SHE]; public ShieldControl ShieldControl => (ShieldControl)_commandExecutors[Command.SHE];
public double Energy => TotalEnergy - ShieldControl.ShieldEnergy; public float Energy => TotalEnergy - ShieldControl.ShieldEnergy;
public double TotalEnergy { get; private set; } public float TotalEnergy { get; private set; }
public int DamagedSystemCount => _systems.Count(s => s.IsDamaged); public int DamagedSystemCount => _systems.Count(s => s.IsDamaged);
public IEnumerable<Subsystem> Systems => _systems; public IEnumerable<Subsystem> Systems => _systems;
public int TorpedoCount { get; } public int TorpedoCount { get; }
@@ -68,7 +68,7 @@ namespace SuperStarTrek.Objects
(_quadrant.HasKlingons, Energy / _maxEnergy) switch (_quadrant.HasKlingons, Energy / _maxEnergy) switch
{ {
(true, _) => "*Red*", (true, _) => "*Red*",
(_, < 0.1) => "Yellow", (_, < 0.1f) => "Yellow",
_ => "Green" _ => "Green"
}; };
@@ -112,15 +112,15 @@ namespace SuperStarTrek.Objects
return CommandResult.Ok; return CommandResult.Ok;
} }
private void TakeDamage(double hitStrength) private void TakeDamage(float hitStrength)
{ {
var hitShieldRatio = hitStrength / ShieldControl.ShieldEnergy; var hitShieldRatio = hitStrength / ShieldControl.ShieldEnergy;
if (_random.GetDouble() > 0.6 || hitShieldRatio <= 0.02) if (_random.GetFloat() > 0.6 || hitShieldRatio <= 0.02f)
{ {
return; return;
} }
_systems[_random.Get1To8Inclusive() - 1].TakeDamage(hitShieldRatio + 0.5 * _random.GetDouble()); _systems[_random.Get1To8Inclusive() - 1].TakeDamage(hitShieldRatio + 0.5f * _random.GetFloat());
} }
} }
} }

View File

@@ -5,7 +5,7 @@ namespace SuperStarTrek.Objects
{ {
internal class Klingon internal class Klingon
{ {
private double _energy; private float _energy;
private Coordinates _sector; private Coordinates _sector;
private readonly Random _random; private readonly Random _random;
@@ -13,14 +13,14 @@ namespace SuperStarTrek.Objects
{ {
_sector = sector; _sector = sector;
_random = random; _random = random;
_energy = _random.GetDouble(100, 300); _energy = _random.GetFloat(100, 300);
} }
public override string ToString() => "+K+"; public override string ToString() => "+K+";
public CommandResult FireOn(Enterprise enterprise) public CommandResult FireOn(Enterprise enterprise)
{ {
var attackStrength = _random.GetDouble(); var attackStrength = _random.GetFloat();
var hitStrength = (int)(_energy * (2 + attackStrength) / _sector.GetDistanceTo(enterprise.Sector)); var hitStrength = (int)(_energy * (2 + attackStrength) / _sector.GetDistanceTo(enterprise.Sector));
_energy /= 3 + attackStrength; _energy /= 3 + attackStrength;

View File

@@ -6,21 +6,21 @@ namespace SuperStarTrek.Objects
{ {
private readonly Input _input; private readonly Input _input;
private readonly Output _output; private readonly Output _output;
private readonly double _repairDelay; private readonly float _repairDelay;
public Starbase(Random random, Input input, Output output) public Starbase(Random random, Input input, Output output)
{ {
_repairDelay = random.GetDouble() * 0.5; _repairDelay = random.GetFloat() * 0.5f;
_input = input; _input = input;
_output = output; _output = output;
} }
public override string ToString() => ">!<"; public override string ToString() => ">!<";
internal bool TryRepair(Enterprise enterprise, out double repairTime) internal bool TryRepair(Enterprise enterprise, out float repairTime)
{ {
repairTime = enterprise.DamagedSystemCount * 0.1 + _repairDelay; repairTime = enterprise.DamagedSystemCount * 0.1f + _repairDelay;
if (repairTime >= 1) { repairTime = 0.9; } if (repairTime >= 1) { repairTime = 0.9f; }
_output.Write(Strings.RepairEstimate, repairTime); _output.Write(Strings.RepairEstimate, repairTime);
if (_input.GetYesNo(Strings.RepairPrompt, Input.YesNoMode.TrueOnY)) if (_input.GetYesNo(Strings.RepairPrompt, Input.YesNoMode.TrueOnY))

View File

@@ -12,14 +12,14 @@ namespace SuperStarTrek
// 475 DEF FNR(R)=INT(RND(R)*7.98+1.01) // 475 DEF FNR(R)=INT(RND(R)*7.98+1.01)
// Returns a value from 1 to 8, inclusive. // Returns a value from 1 to 8, inclusive.
// Note there's a slight bias away from the extreme values, 1 and 8. // Note there's a slight bias away from the extreme values, 1 and 8.
public int Get1To8Inclusive() => (int)(_random.NextDouble() * 7.98 + 1.01); public int Get1To8Inclusive() => (int)(GetFloat() * 7.98 + 1.01);
public int GetInt(int inclusiveMinValue, int exclusiveMaxValue) => public int GetInt(int inclusiveMinValue, int exclusiveMaxValue) =>
_random.Next(inclusiveMinValue, exclusiveMaxValue); _random.Next(inclusiveMinValue, exclusiveMaxValue);
public double GetDouble() => _random.NextDouble(); public float GetFloat() => (float)_random.NextDouble();
public double GetDouble(double inclusiveMinValue, double exclusiveMaxValue) public float GetFloat(float inclusiveMinValue, float exclusiveMaxValue)
=> _random.NextDouble() * (exclusiveMaxValue - inclusiveMinValue) + inclusiveMinValue; => GetFloat() * (exclusiveMaxValue - inclusiveMinValue) + inclusiveMinValue;
} }
} }

View File

@@ -50,7 +50,7 @@ namespace SuperStarTrek.Space
public IEnumerable<Coordinates> GetSectorsFrom(Coordinates start) public IEnumerable<Coordinates> GetSectorsFrom(Coordinates start)
{ {
(double x, double y) = start; (float x, float y) = start;
while(true) while(true)
{ {

View File

@@ -24,14 +24,14 @@ namespace SuperStarTrek.Space
public static QuadrantInfo Create(Coordinates coordinates, string name) public static QuadrantInfo Create(Coordinates coordinates, string name)
{ {
var random = new Random(); var random = new Random();
var klingonCount = random.GetDouble() switch var klingonCount = random.GetFloat() switch
{ {
> 0.98 => 3, > 0.98f => 3,
> 0.95 => 2, > 0.95f => 2,
> 0.80 => 1, > 0.80f => 1,
_ => 0 _ => 0
}; };
var hasStarbase = random.GetDouble() > 0.96; var hasStarbase = random.GetFloat() > 0.96f;
var starCount = random.Get1To8Inclusive(); var starCount = random.Get1To8Inclusive();
return new QuadrantInfo(coordinates, name, klingonCount, starCount, hasStarbase); return new QuadrantInfo(coordinates, name, klingonCount, starCount, hasStarbase);

View File

@@ -18,7 +18,7 @@ namespace SuperStarTrek.Systems
_input = input; _input = input;
} }
public double ShieldEnergy { get; private set; } public float ShieldEnergy { get; private set; }
protected override bool CanExecuteCommand() => IsOperational("{name} inoperable"); protected override bool CanExecuteCommand() => IsOperational("{name} inoperable");
@@ -39,7 +39,7 @@ namespace SuperStarTrek.Systems
return CommandResult.Ok; return CommandResult.Ok;
} }
private bool Validate(double requested) private bool Validate(float requested)
{ {
if (requested > _enterprise.TotalEnergy) if (requested > _enterprise.TotalEnergy)
{ {

View File

@@ -17,7 +17,7 @@ namespace SuperStarTrek.Systems
} }
public string Name { get; } public string Name { get; }
public double Condition { get; private set; } public float Condition { get; private set; }
public bool IsDamaged => Condition < 0; public bool IsDamaged => Condition < 0;
public Command Command { get; } public Command Command { get; }
@@ -44,7 +44,7 @@ namespace SuperStarTrek.Systems
if (IsDamaged) { Condition = 0; } if (IsDamaged) { Condition = 0; }
} }
internal void TakeDamage(double damage) internal void TakeDamage(float damage)
{ {
Condition -= damage; Condition -= damage;
_output.WriteLine($"Damage Control reports, '{Name} damaged by the hit.'"); _output.WriteLine($"Damage Control reports, '{Name} damaged by the hit.'");