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)
|
||||
{
|
||||
time += 1;
|
||||
if (IsHalfTime)
|
||||
{
|
||||
scoreboard.Display(Resource.Formats.EndOfFirstHalf);
|
||||
// Loop back to center jump;
|
||||
}
|
||||
if (TwoMinutesLeft)
|
||||
{
|
||||
_io.Write(Resource.Streams.TwoMinutesLeft);
|
||||
}
|
||||
if (IsHalfTime) { scoreboard.Display(Resource.Formats.EndOfFirstHalf); }
|
||||
if (TwoMinutesLeft) { _io.Write(Resource.Streams.TwoMinutesLeft); }
|
||||
}
|
||||
|
||||
public void StartOvertime() => time = 93;
|
||||
|
||||
@@ -39,12 +39,26 @@ internal class Game
|
||||
|
||||
_io.WriteLine();
|
||||
|
||||
while (scoreboard.Offense is not null)
|
||||
while (true)
|
||||
{
|
||||
var gameOver = scoreboard.Offense.ResolvePlay(scoreboard);
|
||||
if (gameOver) { return; }
|
||||
if (clock.IsHalfTime) { scoreboard.StartPeriod(); }
|
||||
var isFullTime = scoreboard.Offense.ResolvePlay(scoreboard);
|
||||
if (isFullTime && IsGameOver(scoreboard, clock)) { return; }
|
||||
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");
|
||||
|
||||
if (_random.NextFloat() >= 0.5f && _clock.IsFullTime)
|
||||
{
|
||||
_io.WriteLine();
|
||||
if (!scoreboard.ScoresAreEqual)
|
||||
{
|
||||
scoreboard.Display(Resource.Formats.EndOfGame);
|
||||
return true;
|
||||
}
|
||||
|
||||
scoreboard.Display(Resource.Formats.EndOfSecondHalf);
|
||||
_clock.StartOvertime();
|
||||
scoreboard.StartPeriod();
|
||||
return false;
|
||||
}
|
||||
if (_random.NextFloat() >= 0.5f && _clock.IsFullTime) { return true; }
|
||||
|
||||
if (shot == 0)
|
||||
{
|
||||
|
||||
@@ -9,6 +9,7 @@ internal class VisitingTeamPlay : Play
|
||||
private readonly IRandom _random;
|
||||
private readonly Clock _clock;
|
||||
private readonly Defense _defense;
|
||||
private bool _playContinues;
|
||||
|
||||
public VisitingTeamPlay(TextIO io, IRandom random, Clock clock, Defense defense)
|
||||
: base(io, random)
|
||||
@@ -26,37 +27,38 @@ internal class VisitingTeamPlay : Play
|
||||
|
||||
_io.WriteLine();
|
||||
var shot = _random.NextFloat(1, 3.5f);
|
||||
var playContinues = shot > 2;
|
||||
_playContinues = shot > 2;
|
||||
|
||||
if (shot <= 2)
|
||||
{
|
||||
Resolve("Jump shot.", _defense / 8)
|
||||
.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(() => _io.WriteLine("Offensive foul. Dartmouth's ball."));
|
||||
|
||||
_io.WriteLine();
|
||||
}
|
||||
|
||||
while (playContinues)
|
||||
while (_playContinues)
|
||||
{
|
||||
playContinues = false;
|
||||
_playContinues = false;
|
||||
|
||||
Resolve(shot > 3 ? "Set shot." : "Lay up.", _defense / 7)
|
||||
.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();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ResolveBadShot(string message, float defenseFactor) =>
|
||||
Resolve("Shot is off the rim.", _defense * 6)
|
||||
void ResolveBadShot(Scoreboard scoreboard, string message, float defenseFactor) =>
|
||||
Resolve(message, defenseFactor)
|
||||
.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)
|
||||
@@ -74,7 +76,6 @@ internal class VisitingTeamPlay : Play
|
||||
return;
|
||||
}
|
||||
|
||||
playContinues = true;
|
||||
}
|
||||
_playContinues = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,11 +13,12 @@ internal class Scoreboard
|
||||
_scores = new() { [home] = 0, [visitors] = 0 };
|
||||
Home = home;
|
||||
Visitors = visitors;
|
||||
Offense = home; // temporary value till first center jump
|
||||
_io = io;
|
||||
}
|
||||
|
||||
public bool ScoresAreEqual => _scores[Home] == _scores[Visitors];
|
||||
public Team? Offense { get; set; }
|
||||
public Team Offense { get; set; }
|
||||
public Team Home { get; }
|
||||
public Team Visitors { get; }
|
||||
|
||||
@@ -35,8 +36,6 @@ internal class Scoreboard
|
||||
Display();
|
||||
}
|
||||
|
||||
public void StartPeriod() => Offense = null;
|
||||
|
||||
public void Turnover(string? message = null)
|
||||
{
|
||||
if (message is not null) { _io.WriteLine(message); }
|
||||
|
||||
Reference in New Issue
Block a user