diff --git a/77_Salvo/csharp/Game.cs b/77_Salvo/csharp/Game.cs index 978701cb..33d750df 100644 --- a/77_Salvo/csharp/Game.cs +++ b/77_Salvo/csharp/Game.cs @@ -17,15 +17,9 @@ internal class Game { _io.Write(Streams.Title); - var hitTurnRecord = new int[13]; + var hitRecords = new List<(int Turn, Ship Ship)>(); var temp = new Position[13]; - var hitShipValue = new float[13]; - for (var i = 1; i <= 12; i++) - { - hitTurnRecord[i] = -1; - hitShipValue[i] = -1; - } var computerGrid = new Grid(_random); var humanGrid = new Grid(_io); var humanShotSelector = new HumanShotSelector(humanGrid, computerGrid, _io); @@ -74,11 +68,7 @@ L2880: if (numberOfShots != 0) { goto L2960; } L2890: _io.Write(Streams.YouWon); L2900: return; -L2960: for (var i = 1; i <= 12; i++) - { - // if damaged ships -L2970: if (hitShipValue[i]>0) { goto L3800; } - } +L2960: if (humanGrid.Ships.Any(s => s.IsDamaged)) { goto L3800; } temp = computerShotSelector.GetShots().ToArray(); // display shots L3380: if (seeShotsResponse == "YES") @@ -95,33 +85,20 @@ L3380: if (seeShotsResponse == "YES") continue; } _io.Write(Strings.IHit(ship.Name)); - for (var j = 1; j <= 12; j++) - { - if (hitTurnRecord[j] == -1) - { - hitTurnRecord[j]=10+turnNumber; - hitShipValue[j]=ship.Value; - break; - } - } if (ship.IsDestroyed) { - for (var k = 1; k <= 12; k++) - { - if (hitShipValue[k] == ship.Value) - { - hitShipValue[k] = hitTurnRecord[k] = -1; - } - } + hitRecords = hitRecords.Where(hr => hr.Ship != ship).ToList(); + } + else + { + hitRecords.Add((turnNumber, ship)); } - humanGrid[shot]=10+turnNumber; } goto L1950; L3800: //REM************************USINGEARRAY var tempGrid = Position.All.ToDictionary(x => x, _ => 0); -L3860: for (var i = 1; i <= 12; i++) +L3860: foreach (var (hitTurn, ship) in hitRecords) { - if (hitTurnRecord[i]<10) { continue; } foreach (var position in Position.All) { if (humanGrid.WasTargetedAt(position, out _)) @@ -132,9 +109,9 @@ L3860: for (var i = 1; i <= 12; i++) foreach (var neighbour in position.Neighbours) { - if (humanGrid.WasTargetedAt(neighbour, out var turn) && turn == (hitTurnRecord[i] - 10)) + if (humanGrid.WasTargetedAt(neighbour, out var turn) && turn == hitTurn) { - tempGrid[position] += hitTurnRecord[i]-position.Y*(int)(hitShipValue[i]+.5F); + tempGrid[position] += hitTurn + 10 - position.Y * ship.Shots; } } } diff --git a/77_Salvo/csharp/Grid.cs b/77_Salvo/csharp/Grid.cs index 43dd61b4..0c84d688 100644 --- a/77_Salvo/csharp/Grid.cs +++ b/77_Salvo/csharp/Grid.cs @@ -57,18 +57,6 @@ internal class Grid } } - public float this[Position position] - { - get => _shots.TryGetValue(position, out var value) - ? value + 10 - : _ships.FirstOrDefault(s => s.Positions.Contains(position))?.Value ?? 0; - set - { - _ = _ships.FirstOrDefault(s => s.IsHit(position)); - _shots[position] = (int)value - 10; - } - } - internal int UntriedSquareCount => 100 - _shots.Count; internal IEnumerable Ships => _ships.AsEnumerable(); diff --git a/77_Salvo/csharp/Ships/Battleship.cs b/77_Salvo/csharp/Ships/Battleship.cs index 1e57e59d..4f864eb6 100644 --- a/77_Salvo/csharp/Ships/Battleship.cs +++ b/77_Salvo/csharp/Ships/Battleship.cs @@ -14,5 +14,4 @@ internal sealed class Battleship : Ship internal override int Shots => 3; internal override int Size => 5; - internal override float Value => 3; } diff --git a/77_Salvo/csharp/Ships/Cruiser.cs b/77_Salvo/csharp/Ships/Cruiser.cs index 33f30e38..d004f24c 100644 --- a/77_Salvo/csharp/Ships/Cruiser.cs +++ b/77_Salvo/csharp/Ships/Cruiser.cs @@ -14,5 +14,4 @@ internal sealed class Cruiser : Ship internal override int Shots => 2; internal override int Size => 3; - internal override float Value => 2; } diff --git a/77_Salvo/csharp/Ships/Destroyer.cs b/77_Salvo/csharp/Ships/Destroyer.cs index 0e2a023b..6523395d 100644 --- a/77_Salvo/csharp/Ships/Destroyer.cs +++ b/77_Salvo/csharp/Ships/Destroyer.cs @@ -14,5 +14,4 @@ internal sealed class Destroyer : Ship internal override int Shots => 1; internal override int Size => 2; - internal override float Value => Name.EndsWith("") ? 1 : 0.5F; } diff --git a/77_Salvo/csharp/Ships/Ship.cs b/77_Salvo/csharp/Ships/Ship.cs index 58f311c2..ee204d3f 100644 --- a/77_Salvo/csharp/Ships/Ship.cs +++ b/77_Salvo/csharp/Ships/Ship.cs @@ -24,8 +24,7 @@ internal abstract class Ship internal string Name { get; } internal abstract int Shots { get; } internal abstract int Size { get; } - internal abstract float Value { get; } - internal IEnumerable Positions => _positions; + internal bool IsDamaged => _positions.Count > 0 && _positions.Count < Size; internal bool IsDestroyed => _positions.Count == 0; internal bool IsHit(Position position) => _positions.Remove(position);