Simplify messaging

This commit is contained in:
Andrew Cooper
2022-03-30 11:04:38 +11:00
parent 74698c41c4
commit 741b135d99
2 changed files with 58 additions and 60 deletions

View File

@@ -26,23 +26,19 @@ internal class Game
_io.WriteLine(); _io.WriteLine();
var visitingTeam = new Team(_io.ReadString("Choose your opponent")); var visitingTeam = new Team(_io.ReadString("Choose your opponent"));
_io.WriteLine("Center jump");
var offense = _random.NextFloat() > 0.6 ? homeTeam : visitingTeam;
_io.WriteLine($"{offense} controls the tap.");
var time = 0; var time = 0;
var scoreboard = new Scoreboard(homeTeam, visitingTeam, _io); var scoreboard = new Scoreboard(homeTeam, visitingTeam, _io);
scoreboard.Offense = offense;
_io.WriteLine("Center jump");
scoreboard.Offense = ContestBall(0.6f, visitingTeam, homeTeam, "{0} controls the tap");
_io.WriteLine(); _io.WriteLine();
if (offense == homeTeam) if (scoreboard.Offense == homeTeam)
{ {
var shot = _io.ReadShot("Your shot"); var shot = _io.ReadShot("Your shot");
if (_random.NextFloat() >= 0.5 && time >= 100) if (_random.NextFloat() >= 0.5f && time >= 100)
{ {
_io.WriteLine(); _io.WriteLine();
if (scoreboard.ScoresAreEqual) if (scoreboard.ScoresAreEqual)
@@ -78,24 +74,23 @@ internal class Game
_io.Write(Resource.Streams.TwoMinutesLeft); _io.Write(Resource.Streams.TwoMinutesLeft);
} }
_io.WriteLine("Jump shot"); _io.WriteLine("Jump shot");
if (_random.NextFloat() <= 0.341 * defense / 8) if (_random.NextFloat() <= 0.341f * defense / 8)
{ {
scoreboard.AddBasket("Shot is good"); scoreboard.AddBasket("Shot is good");
// over to opponent // over to opponent
} }
else if (_random.NextFloat() <= 0.682 * defense / 8) else if (_random.NextFloat() <= 0.682f * defense / 8)
{ {
_io.WriteLine("Shot is off target"); _io.WriteLine("Shot is off target");
if (defense / 6 * _random.NextFloat() > 0.45) if (defense / 6 * _random.NextFloat() > 0.45f)
{ {
_io.WriteLine($"Rebound to {visitingTeam}"); scoreboard.Turnover($"Rebound to {visitingTeam}");
scoreboard.Turnover();
// over to opponent // over to opponent
} }
else else
{ {
_io.WriteLine("Dartmouth controls the rebound."); _io.WriteLine("Dartmouth controls the rebound.");
if (_random.NextFloat() <= 0.4) if (_random.NextFloat() <= 0.4f)
{ {
// fall through to 1300 // fall through to 1300
} }
@@ -103,7 +98,7 @@ internal class Game
{ {
if (defense == 6) if (defense == 6)
{ {
if (_random.NextFloat() > 0.6) if (_random.NextFloat() > 0.6f)
{ {
scoreboard.Turnover(); scoreboard.Turnover();
scoreboard.AddBasket($"Pass stolen by {visitingTeam} easy layup."); scoreboard.AddBasket($"Pass stolen by {visitingTeam} easy layup.");
@@ -115,23 +110,20 @@ internal class Game
} }
} }
} }
else if (_random.NextFloat() <= 0.782 * defense / 8) else if (_random.NextFloat() <= 0.782f * defense / 8)
{ {
offense = _random.NextFloat() <= 0.5 ? homeTeam : visitingTeam; scoreboard.Offense =
scoreboard.Offense = offense; ContestBall(0.5f, homeTeam, visitingTeam, "Shot is blocked. Ball controlled by {0}.");
_io.WriteLine($"Shot is blocked. Ball controlled by {offense}.");
// go to next shot // go to next shot
} }
else if (_random.NextFloat() <= 0.843 * defense / 8) else if (_random.NextFloat() <= 0.843f * defense / 8)
{ {
_io.WriteLine("Shooter is fouled. Two shots."); FreeThrows("Shooter is fouled. Two shots.");
FreeThrows();
// over to opponent // over to opponent
} }
else else
{ {
_io.WriteLine("Charging foul. Dartmouth loses ball."); scoreboard.Turnover("Charging foul. Dartmouth loses ball.");
scoreboard.Turnover();
// over to opponent // over to opponent
} }
} }
@@ -149,24 +141,23 @@ internal class Game
_io.WriteLine(shot == 3 ? "Lay up." : "Set shot."); _io.WriteLine(shot == 3 ? "Lay up." : "Set shot.");
if (7 / defense * _random.NextFloat() <= 0.4) if (_random.NextFloat() <= 0.4f * defense / 7)
{ {
scoreboard.AddBasket("Shot is good. Two points."); scoreboard.AddBasket("Shot is good. Two points.");
// over to opponent // over to opponent
} }
else if (7 / defense * _random.NextFloat() <= 0.7) else if (_random.NextFloat() <= 0.7f * defense / 7)
{ {
_io.WriteLine("Shot is off the rim."); _io.WriteLine("Shot is off the rim.");
if (_random.NextFloat() <= 2 / 3f) if (_random.NextFloat() <= 2 / 3f)
{ {
_io.WriteLine($"{visitingTeam} controls the rebound."); scoreboard.Turnover($"{visitingTeam} controls the rebound.");
scoreboard.Turnover();
// over to opponent // over to opponent
} }
else else
{ {
_io.WriteLine("Dartmouth controls the rebound"); _io.WriteLine("Dartmouth controls the rebound");
if (_random.NextFloat() <= 0.4) if (_random.NextFloat() <= 0.4f)
{ {
// goto 1300 // goto 1300
} }
@@ -177,22 +168,19 @@ internal class Game
} }
} }
} }
else if (7 / defense * _random.NextFloat() <= 0.875) else if (_random.NextFloat() <= 0.875f * defense / 7)
{ {
_io.WriteLine("Shooter fouled. Two shots."); FreeThrows("Shooter fouled. Two shots.");
FreeThrows();
// over to opponent // over to opponent
} }
else if (7 / defense * _random.NextFloat() <= 0.925) else if (_random.NextFloat() <= 0.925f * defense / 7)
{ {
_io.WriteLine($"Shot blocked. {visitingTeam}'s ball."); scoreboard.Turnover($"Shot blocked. {visitingTeam}'s ball.");
scoreboard.Turnover();
// over to opponent // over to opponent
} }
else else
{ {
_io.WriteLine("Charging foul. Dartmouth loses ball."); scoreboard.Turnover("Charging foul. Dartmouth loses ball.");
scoreboard.Turnover();
// over to opponent // over to opponent
} }
} }
@@ -216,18 +204,17 @@ internal class Game
{ {
_io.WriteLine("Jump shot."); _io.WriteLine("Jump shot.");
if (8 / defense * _random.NextFloat() <= 0.35) if (_random.NextFloat() <= 0.35f * defense / 8)
{ {
scoreboard.AddBasket("Shot is good."); scoreboard.AddBasket("Shot is good.");
// over to Dartmouth // over to Dartmouth
} }
else if (8 / defense * _random.NextFloat() <= 0.75) else if (_random.NextFloat() <= 0.75f * defense / 8)
{ {
_io.WriteLine("Shot is off the rim."); _io.WriteLine("Shot is off the rim.");
if (defense / 6 * _random.NextFloat() <= 0.5) if (_random.NextFloat() <= 0.5f / defense * 6)
{ {
_io.WriteLine("Dartmouth controls the rebound."); scoreboard.Turnover("Dartmouth controls the rebound.");
scoreboard.Turnover();
// over to Dartmouth // over to Dartmouth
} }
else else
@@ -235,7 +222,7 @@ internal class Game
_io.WriteLine($"{visitingTeam} controls the rebound."); _io.WriteLine($"{visitingTeam} controls the rebound.");
if (defense == 6) if (defense == 6)
{ {
if (_random.NextFloat() > 0.75) if (_random.NextFloat() <= 0.25f)
{ {
scoreboard.Turnover(); scoreboard.Turnover();
scoreboard.AddBasket("Ball stolen. Easy lay up for Dartmouth."); scoreboard.AddBasket("Ball stolen. Easy lay up for Dartmouth.");
@@ -243,7 +230,7 @@ internal class Game
// next opponent shot // next opponent shot
} }
} }
if (_random.NextFloat() <= 0.5) if (_random.NextFloat() <= 0.5f)
{ {
_io.WriteLine($"Pass back to {visitingTeam} guard."); _io.WriteLine($"Pass back to {visitingTeam} guard.");
// next opponent shot // next opponent shot
@@ -251,10 +238,9 @@ internal class Game
// goto 3500 // goto 3500
} }
} }
else if (8 / defense * _random.NextFloat() <= 0.9) else if (_random.NextFloat() <= 0.9f * defense / 8)
{ {
_io.WriteLine("Player fouled. Two shots."); FreeThrows("Player fouled. Two shots.");
FreeThrows();
// next Dartmouth shot // next Dartmouth shot
} }
else else
@@ -267,7 +253,7 @@ internal class Game
// 3500 // 3500
_io.WriteLine(shot > 3 ? "Set shot." : "Lay up."); _io.WriteLine(shot > 3 ? "Set shot." : "Lay up.");
if (7 / defense * _random.NextFloat() <= 0.413) if (_random.NextFloat() <= 0.413f * defense / 7)
{ {
scoreboard.AddBasket("Shot is good."); scoreboard.AddBasket("Shot is good.");
// over to Dartmouth // over to Dartmouth
@@ -275,10 +261,9 @@ internal class Game
else else
{ {
_io.WriteLine("Shot is missed."); _io.WriteLine("Shot is missed.");
if (defense / 6 * _random.NextFloat() <= 0.5) if (_random.NextFloat() <= 0.5f * 6 / defense)
{ {
_io.WriteLine("Dartmouth controls the rebound."); scoreboard.Turnover("Dartmouth controls the rebound.");
scoreboard.Turnover();
// over to Dartmouth // over to Dartmouth
} }
else else
@@ -286,7 +271,7 @@ internal class Game
_io.WriteLine($"{visitingTeam} controls the rebound."); _io.WriteLine($"{visitingTeam} controls the rebound.");
if (defense == 6) if (defense == 6)
{ {
if (_random.NextFloat() > 0.75) if (_random.NextFloat() <= 0.25f)
{ {
scoreboard.Turnover(); scoreboard.Turnover();
scoreboard.AddBasket("Ball stolen. Easy lay up for Dartmouth."); scoreboard.AddBasket("Ball stolen. Easy lay up for Dartmouth.");
@@ -294,7 +279,7 @@ internal class Game
// next opponent shot // next opponent shot
} }
} }
if (_random.NextFloat() <= 0.5) if (_random.NextFloat() <= 0.5f)
{ {
_io.WriteLine($"Pass back to {visitingTeam} guard."); _io.WriteLine($"Pass back to {visitingTeam} guard.");
// next opponent shot // next opponent shot
@@ -304,8 +289,17 @@ internal class Game
} }
} }
void FreeThrows() Team ContestBall(float probability, Team a, Team b, string messageFormat)
{ {
var winner = _random.NextFloat() <= probability ? a : b;
_io.WriteLine(messageFormat, winner);
return winner;
}
void FreeThrows(string message)
{
_io.WriteLine(message);
if (_random.NextFloat() <= 0.49) if (_random.NextFloat() <= 0.49)
{ {
scoreboard.AddFreeThrows(2, "Shooter makes both shots."); scoreboard.AddFreeThrows(2, "Shooter makes both shots.");

View File

@@ -10,19 +10,18 @@ internal class Scoreboard
private Team _home; private Team _home;
private Team _visitors; private Team _visitors;
private Team _offense;
public Scoreboard(Team home, Team visitors, IReadWrite io) public Scoreboard(Team home, Team visitors, IReadWrite io)
{ {
_scores = new() { [home] = 0, [visitors] = 0 }; _scores = new() { [home] = 0, [visitors] = 0 };
_home = home; _home = home;
_visitors = visitors; _visitors = visitors;
_offense = home; Offense = home;
_io = io; _io = io;
} }
public bool ScoresAreEqual => _scores[_home] == _scores[_visitors]; public bool ScoresAreEqual => _scores[_home] == _scores[_visitors];
public Team Offense { set => _offense = value; } public Team Offense { get; set; }
public void AddBasket(string message) => AddScore(2, message); public void AddBasket(string message) => AddScore(2, message);
@@ -31,12 +30,17 @@ internal class Scoreboard
private void AddScore(uint score, string message) private void AddScore(uint score, string message)
{ {
_io.WriteLine(message); _io.WriteLine(message);
_scores[_offense] += score; _scores[Offense] += score;
Turnover(); Turnover();
Display(); Display();
} }
public void Turnover() => _offense = _offense == _home ? _visitors : _home; public void Turnover(string? message = null)
{
if (message is not null) { _io.WriteLine(message); }
Offense = Offense == _home ? _visitors : _home;
}
public void Display(string? format = null) => public void Display(string? format = null) =>
_io.WriteLine(format ?? Resource.Formats.Score, _home, _scores[_home], _visitors, _scores[_visitors]); _io.WriteLine(format ?? Resource.Formats.Score, _home, _scores[_home], _visitors, _scores[_visitors]);