diff --git a/07_Basketball/csharp/Clock.cs b/07_Basketball/csharp/Clock.cs index deb7c549..25033052 100644 --- a/07_Basketball/csharp/Clock.cs +++ b/07_Basketball/csharp/Clock.cs @@ -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; diff --git a/07_Basketball/csharp/Game.cs b/07_Basketball/csharp/Game.cs index 391e70d8..4444fa77 100644 --- a/07_Basketball/csharp/Game.cs +++ b/07_Basketball/csharp/Game.cs @@ -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; + } } diff --git a/07_Basketball/csharp/Plays/HomeTeamPlay.cs b/07_Basketball/csharp/Plays/HomeTeamPlay.cs index f79d6388..fe30ff8f 100644 --- a/07_Basketball/csharp/Plays/HomeTeamPlay.cs +++ b/07_Basketball/csharp/Plays/HomeTeamPlay.cs @@ -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) { diff --git a/07_Basketball/csharp/Plays/VisitingTeamPlay.cs b/07_Basketball/csharp/Plays/VisitingTeamPlay.cs index f9e8e460..ab526562 100644 --- a/07_Basketball/csharp/Plays/VisitingTeamPlay.cs +++ b/07_Basketball/csharp/Plays/VisitingTeamPlay.cs @@ -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,55 +27,55 @@ 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) - .Do(0.5f, () => scoreboard.Turnover("Dartmouth controls the rebound.")) - .Or(() => ResolveVisitorsRebound()); + void ResolveBadShot(Scoreboard scoreboard, string message, float defenseFactor) => + Resolve(message, defenseFactor) + .Do(0.5f, () => scoreboard.Turnover("Dartmouth controls the rebound.")) + .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."); - if (_defense == 6 && _random.NextFloat() <= 0.25f) - { - _io.WriteLine(); - 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; + _io.WriteLine(); + 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; } } diff --git a/07_Basketball/csharp/Scoreboard.cs b/07_Basketball/csharp/Scoreboard.cs index 356e50c1..79068392 100644 --- a/07_Basketball/csharp/Scoreboard.cs +++ b/07_Basketball/csharp/Scoreboard.cs @@ -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); }