From d0ed8d2f35c74fe23fa2877320bd30e2842e8b60 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Sun, 7 Mar 2021 18:27:55 +1100 Subject: [PATCH] Reduce precision to float --- 84 Super Star Trek/csharp/Commands/CommandResult.cs | 6 +++--- 84 Super Star Trek/csharp/Game.cs | 8 ++++---- 84 Super Star Trek/csharp/Objects/Enterprise.cs | 12 ++++++------ 84 Super Star Trek/csharp/Objects/Klingon.cs | 6 +++--- 84 Super Star Trek/csharp/Objects/Starbase.cs | 10 +++++----- 84 Super Star Trek/csharp/Random.cs | 8 ++++---- 84 Super Star Trek/csharp/Space/Course.cs | 2 +- 84 Super Star Trek/csharp/Space/QuadrantInfo.cs | 10 +++++----- 84 Super Star Trek/csharp/Systems/ShieldControl.cs | 4 ++-- 84 Super Star Trek/csharp/Systems/Subsystem.cs | 4 ++-- 10 files changed, 35 insertions(+), 35 deletions(-) diff --git a/84 Super Star Trek/csharp/Commands/CommandResult.cs b/84 Super Star Trek/csharp/Commands/CommandResult.cs index d92fba09..1b02780c 100644 --- a/84 Super Star Trek/csharp/Commands/CommandResult.cs +++ b/84 Super Star Trek/csharp/Commands/CommandResult.cs @@ -10,14 +10,14 @@ namespace SuperStarTrek.Commands IsGameOver = isGameOver; } - private CommandResult(double timeElapsed) + private CommandResult(float timeElapsed) { TimeElapsed = timeElapsed; } 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); } } diff --git a/84 Super Star Trek/csharp/Game.cs b/84 Super Star Trek/csharp/Game.cs index 6473877e..0d5539ba 100644 --- a/84 Super Star Trek/csharp/Game.cs +++ b/84 Super Star Trek/csharp/Game.cs @@ -14,7 +14,7 @@ namespace SuperStarTrek private int _initialStardate; private int _finalStarDate; - private double _currentStardate; + private float _currentStardate; private Coordinates _currentQuadrant; private Coordinates _currentSector; private Galaxy _galaxy; @@ -27,7 +27,7 @@ namespace SuperStarTrek _input = new Input(_output); } - public double Stardate => _currentStardate; + public float Stardate => _currentStardate; public void DoIntroduction() { @@ -111,7 +111,7 @@ namespace SuperStarTrek return _enterprise.IsStranded; } - private double GetEfficiency() => - 1000 * Math.Pow(_initialKlingonCount / (_currentStardate - _initialStardate), 2); + private float GetEfficiency() => + 1000 * (float)Math.Pow(_initialKlingonCount / (_currentStardate - _initialStardate), 2); } } diff --git a/84 Super Star Trek/csharp/Objects/Enterprise.cs b/84 Super Star Trek/csharp/Objects/Enterprise.cs index ddaa10a9..ed11bb1e 100644 --- a/84 Super Star Trek/csharp/Objects/Enterprise.cs +++ b/84 Super Star Trek/csharp/Objects/Enterprise.cs @@ -33,8 +33,8 @@ namespace SuperStarTrek.Objects public Coordinates Sector { get; } public string Condition => GetCondition(); public ShieldControl ShieldControl => (ShieldControl)_commandExecutors[Command.SHE]; - public double Energy => TotalEnergy - ShieldControl.ShieldEnergy; - public double TotalEnergy { get; private set; } + public float Energy => TotalEnergy - ShieldControl.ShieldEnergy; + public float TotalEnergy { get; private set; } public int DamagedSystemCount => _systems.Count(s => s.IsDamaged); public IEnumerable Systems => _systems; public int TorpedoCount { get; } @@ -68,7 +68,7 @@ namespace SuperStarTrek.Objects (_quadrant.HasKlingons, Energy / _maxEnergy) switch { (true, _) => "*Red*", - (_, < 0.1) => "Yellow", + (_, < 0.1f) => "Yellow", _ => "Green" }; @@ -112,15 +112,15 @@ namespace SuperStarTrek.Objects return CommandResult.Ok; } - private void TakeDamage(double hitStrength) + private void TakeDamage(float hitStrength) { var hitShieldRatio = hitStrength / ShieldControl.ShieldEnergy; - if (_random.GetDouble() > 0.6 || hitShieldRatio <= 0.02) + if (_random.GetFloat() > 0.6 || hitShieldRatio <= 0.02f) { return; } - _systems[_random.Get1To8Inclusive() - 1].TakeDamage(hitShieldRatio + 0.5 * _random.GetDouble()); + _systems[_random.Get1To8Inclusive() - 1].TakeDamage(hitShieldRatio + 0.5f * _random.GetFloat()); } } } diff --git a/84 Super Star Trek/csharp/Objects/Klingon.cs b/84 Super Star Trek/csharp/Objects/Klingon.cs index 0bbaf984..29584aec 100644 --- a/84 Super Star Trek/csharp/Objects/Klingon.cs +++ b/84 Super Star Trek/csharp/Objects/Klingon.cs @@ -5,7 +5,7 @@ namespace SuperStarTrek.Objects { internal class Klingon { - private double _energy; + private float _energy; private Coordinates _sector; private readonly Random _random; @@ -13,14 +13,14 @@ namespace SuperStarTrek.Objects { _sector = sector; _random = random; - _energy = _random.GetDouble(100, 300); + _energy = _random.GetFloat(100, 300); } public override string ToString() => "+K+"; public CommandResult FireOn(Enterprise enterprise) { - var attackStrength = _random.GetDouble(); + var attackStrength = _random.GetFloat(); var hitStrength = (int)(_energy * (2 + attackStrength) / _sector.GetDistanceTo(enterprise.Sector)); _energy /= 3 + attackStrength; diff --git a/84 Super Star Trek/csharp/Objects/Starbase.cs b/84 Super Star Trek/csharp/Objects/Starbase.cs index b96ffab1..4d854efd 100644 --- a/84 Super Star Trek/csharp/Objects/Starbase.cs +++ b/84 Super Star Trek/csharp/Objects/Starbase.cs @@ -6,21 +6,21 @@ namespace SuperStarTrek.Objects { private readonly Input _input; private readonly Output _output; - private readonly double _repairDelay; + private readonly float _repairDelay; public Starbase(Random random, Input input, Output output) { - _repairDelay = random.GetDouble() * 0.5; + _repairDelay = random.GetFloat() * 0.5f; _input = input; _output = output; } 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; - if (repairTime >= 1) { repairTime = 0.9; } + repairTime = enterprise.DamagedSystemCount * 0.1f + _repairDelay; + if (repairTime >= 1) { repairTime = 0.9f; } _output.Write(Strings.RepairEstimate, repairTime); if (_input.GetYesNo(Strings.RepairPrompt, Input.YesNoMode.TrueOnY)) diff --git a/84 Super Star Trek/csharp/Random.cs b/84 Super Star Trek/csharp/Random.cs index 460c1cef..5ed47805 100644 --- a/84 Super Star Trek/csharp/Random.cs +++ b/84 Super Star Trek/csharp/Random.cs @@ -12,14 +12,14 @@ namespace SuperStarTrek // 475 DEF FNR(R)=INT(RND(R)*7.98+1.01) // Returns a value from 1 to 8, inclusive. // 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) => _random.Next(inclusiveMinValue, exclusiveMaxValue); - public double GetDouble() => _random.NextDouble(); + public float GetFloat() => (float)_random.NextDouble(); - public double GetDouble(double inclusiveMinValue, double exclusiveMaxValue) - => _random.NextDouble() * (exclusiveMaxValue - inclusiveMinValue) + inclusiveMinValue; + public float GetFloat(float inclusiveMinValue, float exclusiveMaxValue) + => GetFloat() * (exclusiveMaxValue - inclusiveMinValue) + inclusiveMinValue; } } diff --git a/84 Super Star Trek/csharp/Space/Course.cs b/84 Super Star Trek/csharp/Space/Course.cs index 99c48c1a..40bf4bb2 100644 --- a/84 Super Star Trek/csharp/Space/Course.cs +++ b/84 Super Star Trek/csharp/Space/Course.cs @@ -50,7 +50,7 @@ namespace SuperStarTrek.Space public IEnumerable GetSectorsFrom(Coordinates start) { - (double x, double y) = start; + (float x, float y) = start; while(true) { diff --git a/84 Super Star Trek/csharp/Space/QuadrantInfo.cs b/84 Super Star Trek/csharp/Space/QuadrantInfo.cs index 284c8c84..04780a27 100644 --- a/84 Super Star Trek/csharp/Space/QuadrantInfo.cs +++ b/84 Super Star Trek/csharp/Space/QuadrantInfo.cs @@ -24,14 +24,14 @@ namespace SuperStarTrek.Space public static QuadrantInfo Create(Coordinates coordinates, string name) { var random = new Random(); - var klingonCount = random.GetDouble() switch + var klingonCount = random.GetFloat() switch { - > 0.98 => 3, - > 0.95 => 2, - > 0.80 => 1, + > 0.98f => 3, + > 0.95f => 2, + > 0.80f => 1, _ => 0 }; - var hasStarbase = random.GetDouble() > 0.96; + var hasStarbase = random.GetFloat() > 0.96f; var starCount = random.Get1To8Inclusive(); return new QuadrantInfo(coordinates, name, klingonCount, starCount, hasStarbase); diff --git a/84 Super Star Trek/csharp/Systems/ShieldControl.cs b/84 Super Star Trek/csharp/Systems/ShieldControl.cs index 48320588..6ceefcf4 100644 --- a/84 Super Star Trek/csharp/Systems/ShieldControl.cs +++ b/84 Super Star Trek/csharp/Systems/ShieldControl.cs @@ -18,7 +18,7 @@ namespace SuperStarTrek.Systems _input = input; } - public double ShieldEnergy { get; private set; } + public float ShieldEnergy { get; private set; } protected override bool CanExecuteCommand() => IsOperational("{name} inoperable"); @@ -39,7 +39,7 @@ namespace SuperStarTrek.Systems return CommandResult.Ok; } - private bool Validate(double requested) + private bool Validate(float requested) { if (requested > _enterprise.TotalEnergy) { diff --git a/84 Super Star Trek/csharp/Systems/Subsystem.cs b/84 Super Star Trek/csharp/Systems/Subsystem.cs index 88924a8c..ab7bef4c 100644 --- a/84 Super Star Trek/csharp/Systems/Subsystem.cs +++ b/84 Super Star Trek/csharp/Systems/Subsystem.cs @@ -17,7 +17,7 @@ namespace SuperStarTrek.Systems } public string Name { get; } - public double Condition { get; private set; } + public float Condition { get; private set; } public bool IsDamaged => Condition < 0; public Command Command { get; } @@ -44,7 +44,7 @@ namespace SuperStarTrek.Systems if (IsDamaged) { Condition = 0; } } - internal void TakeDamage(double damage) + internal void TakeDamage(float damage) { Condition -= damage; _output.WriteLine($"Damage Control reports, '{Name} damaged by the hit.'");