Convert Hexapawn to common library

This commit is contained in:
Andrew Cooper
2022-03-18 07:05:27 +11:00
parent 455fea9609
commit 1a8ea5aabd
18 changed files with 527 additions and 603 deletions

View File

@@ -1,27 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Games.Common.IO;
using Games.Common.Randomness;
using Hexapawn.Resources;
namespace Hexapawn
namespace Hexapawn;
// Runs series of games between the computer and the human player
internal class GameSeries
{
// Runs series of games between the computer and the human player
internal class GameSeries
private readonly TextIO _io;
private readonly Computer _computer;
private readonly Human _human;
private readonly Dictionary<object, int> _wins;
public GameSeries(TextIO io, IRandom random)
{
private readonly Computer _computer = new();
private readonly Human _human = new();
_io = io;
_computer = new(io, random);
_human = new(io);
_wins = new() { [_computer] = 0, [_human] = 0 };
}
public void Play()
public void Play()
{
_io.Write(Resource.Streams.Title);
if (_io.GetYesNo("Instructions") == 'Y')
{
while (true)
{
var game = new Game(_human, _computer);
_io.Write(Resource.Streams.Instructions);
}
var winner = game.Play();
winner.AddWin();
Console.WriteLine(winner == _computer ? "I win." : "You win.");
while (true)
{
var game = new Game(_io);
Console.Write($"I have won {_computer.Wins} and you {_human.Wins}");
Console.WriteLine($" out of {_computer.Wins + _human.Wins} games.");
Console.WriteLine();
}
var winner = game.Play(_human, _computer);
_wins[winner]++;
_io.WriteLine(winner == _computer ? "I win." : "You win.");
_io.Write($"I have won {_wins[_computer]} and you {_wins[_human]}");
_io.WriteLine($" out of {_wins.Values.Sum()} games.");
_io.WriteLine();
}
}
}