mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 15:16:33 -08:00
Move full time messages to game loop
This commit is contained in:
@@ -17,15 +17,8 @@ internal class Clock
|
|||||||
public void Increment(Scoreboard scoreboard)
|
public void Increment(Scoreboard scoreboard)
|
||||||
{
|
{
|
||||||
time += 1;
|
time += 1;
|
||||||
if (IsHalfTime)
|
if (IsHalfTime) { scoreboard.Display(Resource.Formats.EndOfFirstHalf); }
|
||||||
{
|
if (TwoMinutesLeft) { _io.Write(Resource.Streams.TwoMinutesLeft); }
|
||||||
scoreboard.Display(Resource.Formats.EndOfFirstHalf);
|
|
||||||
// Loop back to center jump;
|
|
||||||
}
|
|
||||||
if (TwoMinutesLeft)
|
|
||||||
{
|
|
||||||
_io.Write(Resource.Streams.TwoMinutesLeft);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartOvertime() => time = 93;
|
public void StartOvertime() => time = 93;
|
||||||
|
|||||||
@@ -39,12 +39,26 @@ internal class Game
|
|||||||
|
|
||||||
_io.WriteLine();
|
_io.WriteLine();
|
||||||
|
|
||||||
while (scoreboard.Offense is not null)
|
while (true)
|
||||||
{
|
{
|
||||||
var gameOver = scoreboard.Offense.ResolvePlay(scoreboard);
|
var isFullTime = scoreboard.Offense.ResolvePlay(scoreboard);
|
||||||
if (gameOver) { return; }
|
if (isFullTime && IsGameOver(scoreboard, clock)) { return; }
|
||||||
if (clock.IsHalfTime) { scoreboard.StartPeriod(); }
|
if (clock.IsHalfTime) { break; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool IsGameOver(Scoreboard scoreboard, Clock clock)
|
||||||
|
{
|
||||||
|
_io.WriteLine();
|
||||||
|
if (scoreboard.ScoresAreEqual)
|
||||||
|
{
|
||||||
|
scoreboard.Display(Resource.Formats.EndOfSecondHalf);
|
||||||
|
clock.StartOvertime();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
scoreboard.Display(Resource.Formats.EndOfGame);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,20 +24,7 @@ internal class HomeTeamPlay : Play
|
|||||||
{
|
{
|
||||||
var shot = _io.ReadShot("Your shot");
|
var shot = _io.ReadShot("Your shot");
|
||||||
|
|
||||||
if (_random.NextFloat() >= 0.5f && _clock.IsFullTime)
|
if (_random.NextFloat() >= 0.5f && _clock.IsFullTime) { return true; }
|
||||||
{
|
|
||||||
_io.WriteLine();
|
|
||||||
if (!scoreboard.ScoresAreEqual)
|
|
||||||
{
|
|
||||||
scoreboard.Display(Resource.Formats.EndOfGame);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
scoreboard.Display(Resource.Formats.EndOfSecondHalf);
|
|
||||||
_clock.StartOvertime();
|
|
||||||
scoreboard.StartPeriod();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (shot == 0)
|
if (shot == 0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ internal class VisitingTeamPlay : Play
|
|||||||
private readonly IRandom _random;
|
private readonly IRandom _random;
|
||||||
private readonly Clock _clock;
|
private readonly Clock _clock;
|
||||||
private readonly Defense _defense;
|
private readonly Defense _defense;
|
||||||
|
private bool _playContinues;
|
||||||
|
|
||||||
public VisitingTeamPlay(TextIO io, IRandom random, Clock clock, Defense defense)
|
public VisitingTeamPlay(TextIO io, IRandom random, Clock clock, Defense defense)
|
||||||
: base(io, random)
|
: base(io, random)
|
||||||
@@ -26,55 +27,55 @@ internal class VisitingTeamPlay : Play
|
|||||||
|
|
||||||
_io.WriteLine();
|
_io.WriteLine();
|
||||||
var shot = _random.NextFloat(1, 3.5f);
|
var shot = _random.NextFloat(1, 3.5f);
|
||||||
var playContinues = shot > 2;
|
_playContinues = shot > 2;
|
||||||
|
|
||||||
if (shot <= 2)
|
if (shot <= 2)
|
||||||
{
|
{
|
||||||
Resolve("Jump shot.", _defense / 8)
|
Resolve("Jump shot.", _defense / 8)
|
||||||
.Do(0.35f, () => scoreboard.AddBasket("Shot is good."))
|
.Do(0.35f, () => scoreboard.AddBasket("Shot is good."))
|
||||||
.Or(0.75f, () => ResolveBadShot("Shot is off the rim.", _defense * 6))
|
.Or(0.75f, () => ResolveBadShot(scoreboard, "Shot is off the rim.", _defense * 6))
|
||||||
.Or(0.9f, () => ResolveFreeThrows(scoreboard, "Player fouled. Two shots."))
|
.Or(0.9f, () => ResolveFreeThrows(scoreboard, "Player fouled. Two shots."))
|
||||||
.Or(() => _io.WriteLine("Offensive foul. Dartmouth's ball."));
|
.Or(() => _io.WriteLine("Offensive foul. Dartmouth's ball."));
|
||||||
|
|
||||||
_io.WriteLine();
|
_io.WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (playContinues)
|
while (_playContinues)
|
||||||
{
|
{
|
||||||
playContinues = false;
|
_playContinues = false;
|
||||||
|
|
||||||
Resolve(shot > 3 ? "Set shot." : "Lay up.", _defense / 7)
|
Resolve(shot > 3 ? "Set shot." : "Lay up.", _defense / 7)
|
||||||
.Do(0.413f, () => scoreboard.AddBasket("Shot is good."))
|
.Do(0.413f, () => scoreboard.AddBasket("Shot is good."))
|
||||||
.Or(() => ResolveBadShot("Shot is missed.", 6 / _defense));
|
.Or(() => ResolveBadShot(scoreboard, "Shot is missed.", 6 / _defense));
|
||||||
_io.WriteLine();
|
_io.WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void ResolveBadShot(string message, float defenseFactor) =>
|
void ResolveBadShot(Scoreboard scoreboard, string message, float defenseFactor) =>
|
||||||
Resolve("Shot is off the rim.", _defense * 6)
|
Resolve(message, defenseFactor)
|
||||||
.Do(0.5f, () => scoreboard.Turnover("Dartmouth controls the rebound."))
|
.Do(0.5f, () => scoreboard.Turnover("Dartmouth controls the rebound."))
|
||||||
.Or(() => ResolveVisitorsRebound());
|
.Or(() => ResolveVisitorsRebound(scoreboard));
|
||||||
|
|
||||||
void ResolveVisitorsRebound()
|
void ResolveVisitorsRebound(Scoreboard scoreboard)
|
||||||
|
{
|
||||||
|
_io.Write($"{scoreboard.Visitors} controls the rebound.");
|
||||||
|
if (_defense == 6 && _random.NextFloat() <= 0.25f)
|
||||||
{
|
{
|
||||||
_io.Write($"{scoreboard.Visitors} controls the rebound.");
|
_io.WriteLine();
|
||||||
if (_defense == 6 && _random.NextFloat() <= 0.25f)
|
scoreboard.Turnover();
|
||||||
{
|
scoreboard.AddBasket("Ball stolen. Easy lay up for Dartmouth.");
|
||||||
_io.WriteLine();
|
return;
|
||||||
scoreboard.Turnover();
|
|
||||||
scoreboard.AddBasket("Ball stolen. Easy lay up for Dartmouth.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_random.NextFloat() <= 0.5f)
|
|
||||||
{
|
|
||||||
_io.WriteLine();
|
|
||||||
_io.Write($"Pass back to {scoreboard.Visitors} guard.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
playContinues = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_random.NextFloat() <= 0.5f)
|
||||||
|
{
|
||||||
|
_io.WriteLine();
|
||||||
|
_io.Write($"Pass back to {scoreboard.Visitors} guard.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_playContinues = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,12 @@ internal class Scoreboard
|
|||||||
_scores = new() { [home] = 0, [visitors] = 0 };
|
_scores = new() { [home] = 0, [visitors] = 0 };
|
||||||
Home = home;
|
Home = home;
|
||||||
Visitors = visitors;
|
Visitors = visitors;
|
||||||
|
Offense = home; // temporary value till first center jump
|
||||||
_io = io;
|
_io = io;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ScoresAreEqual => _scores[Home] == _scores[Visitors];
|
public bool ScoresAreEqual => _scores[Home] == _scores[Visitors];
|
||||||
public Team? Offense { get; set; }
|
public Team Offense { get; set; }
|
||||||
public Team Home { get; }
|
public Team Home { get; }
|
||||||
public Team Visitors { get; }
|
public Team Visitors { get; }
|
||||||
|
|
||||||
@@ -35,8 +36,6 @@ internal class Scoreboard
|
|||||||
Display();
|
Display();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void StartPeriod() => Offense = null;
|
|
||||||
|
|
||||||
public void Turnover(string? message = null)
|
public void Turnover(string? message = null)
|
||||||
{
|
{
|
||||||
if (message is not null) { _io.WriteLine(message); }
|
if (message is not null) { _io.WriteLine(message); }
|
||||||
|
|||||||
Reference in New Issue
Block a user