mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 07:40:50 -08:00
Add TurnHandler
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
using Salvo.Targetting;
|
||||
|
||||
namespace Salvo;
|
||||
|
||||
internal class Game
|
||||
@@ -17,95 +15,15 @@ internal class Game
|
||||
{
|
||||
_io.Write(Streams.Title);
|
||||
|
||||
var computerFleet = new Fleet(_random);
|
||||
var humanFleet = new Fleet(_io);
|
||||
var humanStarts = AskWhoStarts(computerFleet);
|
||||
L1890: var turnNumber=0;
|
||||
var humanShotSelector = new HumanShotSelector(humanFleet, _io);
|
||||
var computerShotSelector = new ComputerShotSelector(computerFleet, _random, _io);
|
||||
L1920: _io.WriteLine();
|
||||
L1930: if (!humanStarts) { goto L2620; }
|
||||
L1950: if (humanStarts)
|
||||
{
|
||||
L1960: turnNumber++;
|
||||
L1980: _io.Write(Strings.Turn(turnNumber));
|
||||
}
|
||||
L1990: var numberOfShots = humanShotSelector.NumberOfShots;
|
||||
L2220: _io.Write(Strings.YouHaveShots(numberOfShots));
|
||||
if (numberOfShots == 0) { goto L2270; }
|
||||
L2230: if (humanShotSelector.CanTargetAllRemainingSquares)
|
||||
{
|
||||
_io.WriteLine(Streams.YouHaveMoreShotsThanSquares);
|
||||
L2250: goto L2890;
|
||||
}
|
||||
computerFleet.ReceiveShots(
|
||||
humanShotSelector.GetShots(turnNumber),
|
||||
ship => _io.Write(Strings.YouHit(ship.Name)));
|
||||
L2620: if (!humanStarts)
|
||||
{
|
||||
L2640: turnNumber++;
|
||||
L2660: _io.Write(Strings.Turn(turnNumber));
|
||||
}
|
||||
L2670: numberOfShots = computerShotSelector.NumberOfShots;
|
||||
L2840: _io.Write(Strings.IHaveShots(numberOfShots));
|
||||
L2850: if (!computerShotSelector.CanTargetAllRemainingSquares) { goto L2880; }
|
||||
L2860: _io.Write(Streams.IHaveMoreShotsThanSquares);
|
||||
L2270: _io.Write(Streams.IWon);
|
||||
return;
|
||||
L2880: if (numberOfShots > 0) { goto L2960; }
|
||||
L2890: _io.Write(Streams.YouWon);
|
||||
L2900: return;
|
||||
var turnHandler = new TurnHandler(_io, _random);
|
||||
_io.WriteLine();
|
||||
|
||||
L2960: humanFleet.ReceiveShots(
|
||||
computerShotSelector.GetShots(turnNumber),
|
||||
ship =>
|
||||
{
|
||||
_io.Write(Strings.IHit(ship.Name));
|
||||
computerShotSelector.RecordHit(ship, turnNumber);
|
||||
});
|
||||
goto L1950;
|
||||
}
|
||||
|
||||
private bool AskWhoStarts(Fleet computerFleet)
|
||||
{
|
||||
while (true)
|
||||
Winner? winner;
|
||||
do
|
||||
{
|
||||
var startResponse = _io.ReadString(Prompts.Start);
|
||||
if (startResponse.Equals(Strings.WhereAreYourShips, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
foreach (var ship in computerFleet.Ships)
|
||||
{
|
||||
_io.WriteLine(ship);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return startResponse.Equals("yes", StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
}
|
||||
winner = turnHandler.PlayTurn();
|
||||
} while (winner == null);
|
||||
|
||||
_io.Write(winner == Winner.Computer ? Streams.IWon : Streams.YouWon);
|
||||
}
|
||||
}
|
||||
|
||||
internal enum Winner
|
||||
{
|
||||
None,
|
||||
Human,
|
||||
Computer
|
||||
}
|
||||
|
||||
internal class DataRandom : IRandom
|
||||
{
|
||||
private readonly Queue<float> _values =
|
||||
new(File.ReadAllLines("data.txt").Select(l => float.Parse(l) / 1000000));
|
||||
private float _previous;
|
||||
|
||||
public float NextFloat() => _previous = _values.Dequeue();
|
||||
|
||||
public float PreviousFloat() => _previous;
|
||||
|
||||
public void Reseed(int seed)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
92
77_Salvo/csharp/TurnHandler.cs
Normal file
92
77_Salvo/csharp/TurnHandler.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using Salvo.Targetting;
|
||||
|
||||
namespace Salvo;
|
||||
|
||||
internal class TurnHandler
|
||||
{
|
||||
private readonly IReadWrite _io;
|
||||
private readonly Fleet _humanFleet;
|
||||
private readonly Fleet _computerFleet;
|
||||
private readonly bool _humanStarts;
|
||||
private readonly HumanShotSelector _humanShotSelector;
|
||||
private readonly ComputerShotSelector _computerShotSelector;
|
||||
private readonly Func<Winner?> _turnAction;
|
||||
private int _turnNumber;
|
||||
|
||||
public TurnHandler(IReadWrite io, IRandom random)
|
||||
{
|
||||
_io = io;
|
||||
_computerFleet = new Fleet(random);
|
||||
_humanFleet = new Fleet(io);
|
||||
_turnAction = AskWhoStarts()
|
||||
? () => PlayHumanTurn() ?? PlayComputerTurn()
|
||||
: () => PlayComputerTurn() ?? PlayHumanTurn();
|
||||
_humanShotSelector = new HumanShotSelector(_humanFleet, io);
|
||||
_computerShotSelector = new ComputerShotSelector(_computerFleet, random, io);
|
||||
}
|
||||
|
||||
public Winner? PlayTurn()
|
||||
{
|
||||
_io.Write(Strings.Turn(++_turnNumber));
|
||||
return _turnAction.Invoke();
|
||||
}
|
||||
|
||||
private bool AskWhoStarts()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var startResponse = _io.ReadString(Prompts.Start);
|
||||
if (startResponse.Equals(Strings.WhereAreYourShips, StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
foreach (var ship in _computerFleet.Ships)
|
||||
{
|
||||
_io.WriteLine(ship);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return startResponse.Equals("yes", StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Winner? PlayComputerTurn()
|
||||
{
|
||||
var numberOfShots = _computerShotSelector.NumberOfShots;
|
||||
_io.Write(Strings.IHaveShots(numberOfShots));
|
||||
if (numberOfShots == 0) { return Winner.Human; }
|
||||
if (_computerShotSelector.CanTargetAllRemainingSquares)
|
||||
{
|
||||
_io.Write(Streams.IHaveMoreShotsThanSquares);
|
||||
return Winner.Computer;
|
||||
}
|
||||
|
||||
_humanFleet.ReceiveShots(
|
||||
_computerShotSelector.GetShots(_turnNumber),
|
||||
ship =>
|
||||
{
|
||||
_io.Write(Strings.IHit(ship.Name));
|
||||
_computerShotSelector.RecordHit(ship, _turnNumber);
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private Winner? PlayHumanTurn()
|
||||
{
|
||||
var numberOfShots = _humanShotSelector.NumberOfShots;
|
||||
_io.Write(Strings.YouHaveShots(numberOfShots));
|
||||
if (numberOfShots == 0) { return Winner.Computer; }
|
||||
if (_humanShotSelector.CanTargetAllRemainingSquares)
|
||||
{
|
||||
_io.WriteLine(Streams.YouHaveMoreShotsThanSquares);
|
||||
return Winner.Human;
|
||||
}
|
||||
|
||||
_computerFleet.ReceiveShots(
|
||||
_humanShotSelector.GetShots(_turnNumber),
|
||||
ship => _io.Write(Strings.YouHit(ship.Name)));
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
7
77_Salvo/csharp/Winner.cs
Normal file
7
77_Salvo/csharp/Winner.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Salvo;
|
||||
|
||||
internal enum Winner
|
||||
{
|
||||
Human,
|
||||
Computer
|
||||
}
|
||||
Reference in New Issue
Block a user