mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-06 18:14:27 -08:00
Convert Hexapawn to common library
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user