mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-15 14:34:06 -08:00
Improve computer shot tracking/selection
This commit is contained in:
@@ -17,15 +17,9 @@ internal class Game
|
|||||||
{
|
{
|
||||||
_io.Write(Streams.Title);
|
_io.Write(Streams.Title);
|
||||||
|
|
||||||
var hitTurnRecord = new int[13];
|
var hitRecords = new List<(int Turn, Ship Ship)>();
|
||||||
var temp = new Position[13];
|
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 computerGrid = new Grid(_random);
|
||||||
var humanGrid = new Grid(_io);
|
var humanGrid = new Grid(_io);
|
||||||
var humanShotSelector = new HumanShotSelector(humanGrid, computerGrid, _io);
|
var humanShotSelector = new HumanShotSelector(humanGrid, computerGrid, _io);
|
||||||
@@ -74,11 +68,7 @@ L2880: if (numberOfShots != 0) { goto L2960; }
|
|||||||
L2890: _io.Write(Streams.YouWon);
|
L2890: _io.Write(Streams.YouWon);
|
||||||
L2900: return;
|
L2900: return;
|
||||||
|
|
||||||
L2960: for (var i = 1; i <= 12; i++)
|
L2960: if (humanGrid.Ships.Any(s => s.IsDamaged)) { goto L3800; }
|
||||||
{
|
|
||||||
// if damaged ships
|
|
||||||
L2970: if (hitShipValue[i]>0) { goto L3800; }
|
|
||||||
}
|
|
||||||
temp = computerShotSelector.GetShots().ToArray();
|
temp = computerShotSelector.GetShots().ToArray();
|
||||||
// display shots
|
// display shots
|
||||||
L3380: if (seeShotsResponse == "YES")
|
L3380: if (seeShotsResponse == "YES")
|
||||||
@@ -95,33 +85,20 @@ L3380: if (seeShotsResponse == "YES")
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
_io.Write(Strings.IHit(ship.Name));
|
_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)
|
if (ship.IsDestroyed)
|
||||||
{
|
{
|
||||||
for (var k = 1; k <= 12; k++)
|
hitRecords = hitRecords.Where(hr => hr.Ship != ship).ToList();
|
||||||
{
|
}
|
||||||
if (hitShipValue[k] == ship.Value)
|
else
|
||||||
{
|
{
|
||||||
hitShipValue[k] = hitTurnRecord[k] = -1;
|
hitRecords.Add((turnNumber, ship));
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
humanGrid[shot]=10+turnNumber;
|
|
||||||
}
|
}
|
||||||
goto L1950;
|
goto L1950;
|
||||||
L3800: //REM************************USINGEARRAY
|
L3800: //REM************************USINGEARRAY
|
||||||
var tempGrid = Position.All.ToDictionary(x => x, _ => 0);
|
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)
|
foreach (var position in Position.All)
|
||||||
{
|
{
|
||||||
if (humanGrid.WasTargetedAt(position, out _))
|
if (humanGrid.WasTargetedAt(position, out _))
|
||||||
@@ -132,9 +109,9 @@ L3860: for (var i = 1; i <= 12; i++)
|
|||||||
|
|
||||||
foreach (var neighbour in position.Neighbours)
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 int UntriedSquareCount => 100 - _shots.Count;
|
||||||
internal IEnumerable<Ship> Ships => _ships.AsEnumerable();
|
internal IEnumerable<Ship> Ships => _ships.AsEnumerable();
|
||||||
|
|
||||||
|
|||||||
@@ -14,5 +14,4 @@ internal sealed class Battleship : Ship
|
|||||||
|
|
||||||
internal override int Shots => 3;
|
internal override int Shots => 3;
|
||||||
internal override int Size => 5;
|
internal override int Size => 5;
|
||||||
internal override float Value => 3;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,5 +14,4 @@ internal sealed class Cruiser : Ship
|
|||||||
|
|
||||||
internal override int Shots => 2;
|
internal override int Shots => 2;
|
||||||
internal override int Size => 3;
|
internal override int Size => 3;
|
||||||
internal override float Value => 2;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,5 +14,4 @@ internal sealed class Destroyer : Ship
|
|||||||
|
|
||||||
internal override int Shots => 1;
|
internal override int Shots => 1;
|
||||||
internal override int Size => 2;
|
internal override int Size => 2;
|
||||||
internal override float Value => Name.EndsWith("<A>") ? 1 : 0.5F;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,7 @@ internal abstract class Ship
|
|||||||
internal string Name { get; }
|
internal string Name { get; }
|
||||||
internal abstract int Shots { get; }
|
internal abstract int Shots { get; }
|
||||||
internal abstract int Size { get; }
|
internal abstract int Size { get; }
|
||||||
internal abstract float Value { get; }
|
internal bool IsDamaged => _positions.Count > 0 && _positions.Count < Size;
|
||||||
internal IEnumerable<Position> Positions => _positions;
|
|
||||||
internal bool IsDestroyed => _positions.Count == 0;
|
internal bool IsDestroyed => _positions.Count == 0;
|
||||||
|
|
||||||
internal bool IsHit(Position position) => _positions.Remove(position);
|
internal bool IsHit(Position position) => _positions.Remove(position);
|
||||||
|
|||||||
Reference in New Issue
Block a user