mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 15:16:33 -08:00
Cleanup Game creation and main loop
This commit is contained in:
@@ -7,58 +7,67 @@ namespace Basketball;
|
|||||||
|
|
||||||
internal class Game
|
internal class Game
|
||||||
{
|
{
|
||||||
|
private readonly Clock _clock;
|
||||||
|
private readonly Scoreboard _scoreboard;
|
||||||
private readonly TextIO _io;
|
private readonly TextIO _io;
|
||||||
private readonly IRandom _random;
|
private readonly IRandom _random;
|
||||||
|
|
||||||
public Game(TextIO io, IRandom random)
|
private Game(Clock clock, Scoreboard scoreboard, TextIO io, IRandom random)
|
||||||
{
|
{
|
||||||
|
_clock = clock;
|
||||||
|
_scoreboard = scoreboard;
|
||||||
_io = io;
|
_io = io;
|
||||||
_random = random;
|
_random = random;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Play()
|
public static Game Create(TextIO io, IRandom random)
|
||||||
{
|
{
|
||||||
_io.Write(Resource.Streams.Introduction);
|
io.Write(Resource.Streams.Introduction);
|
||||||
|
|
||||||
var defense = new Defense(_io.ReadDefense("Your starting defense will be"));
|
var defense = new Defense(io.ReadDefense("Your starting defense will be"));
|
||||||
var clock = new Clock(_io);
|
var clock = new Clock(io);
|
||||||
|
|
||||||
_io.WriteLine();
|
io.WriteLine();
|
||||||
|
|
||||||
var scoreboard = new Scoreboard(
|
var scoreboard = new Scoreboard(
|
||||||
new Team("Dartmouth", new HomeTeamPlay(_io, _random, clock, defense)),
|
new Team("Dartmouth", new HomeTeamPlay(io, random, clock, defense)),
|
||||||
new Team(_io.ReadString("Choose your opponent"), new VisitingTeamPlay(_io, _random, clock, defense)),
|
new Team(io.ReadString("Choose your opponent"), new VisitingTeamPlay(io, random, clock, defense)),
|
||||||
_io);
|
io);
|
||||||
|
|
||||||
|
return new Game(clock, scoreboard, io, random);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Play()
|
||||||
|
{
|
||||||
var ballContest = new BallContest(0.4f, "{0} controls the tap", _io, _random);
|
var ballContest = new BallContest(0.4f, "{0} controls the tap", _io, _random);
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
_io.WriteLine("Center jump");
|
_io.WriteLine("Center jump");
|
||||||
ballContest.Resolve(scoreboard);
|
ballContest.Resolve(_scoreboard);
|
||||||
|
|
||||||
_io.WriteLine();
|
_io.WriteLine();
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var isFullTime = scoreboard.Offense.ResolvePlay(scoreboard);
|
var isFullTime = _scoreboard.Offense.ResolvePlay(_scoreboard);
|
||||||
if (isFullTime && IsGameOver(scoreboard, clock)) { return; }
|
if (isFullTime && IsGameOver()) { return; }
|
||||||
if (clock.IsHalfTime) { break; }
|
if (_clock.IsHalfTime) { break; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsGameOver(Scoreboard scoreboard, Clock clock)
|
private bool IsGameOver()
|
||||||
{
|
{
|
||||||
_io.WriteLine();
|
_io.WriteLine();
|
||||||
if (scoreboard.ScoresAreEqual)
|
if (_scoreboard.ScoresAreEqual)
|
||||||
{
|
{
|
||||||
scoreboard.Display(Resource.Formats.EndOfSecondHalf);
|
_scoreboard.Display(Resource.Formats.EndOfSecondHalf);
|
||||||
clock.StartOvertime();
|
_clock.StartOvertime();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
scoreboard.Display(Resource.Formats.EndOfGame);
|
_scoreboard.Display(Resource.Formats.EndOfGame);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ using Basketball;
|
|||||||
using Games.Common.IO;
|
using Games.Common.IO;
|
||||||
using Games.Common.Randomness;
|
using Games.Common.Randomness;
|
||||||
|
|
||||||
var game = new Game(new ConsoleIO(), new RandomNumberGenerator());
|
var game = Game.Create(new ConsoleIO(), new RandomNumberGenerator());
|
||||||
|
|
||||||
game.Play();
|
game.Play();
|
||||||
Reference in New Issue
Block a user