using System.Collections.Generic;
public static void Main()
using (var game = new PickupGame(maxPlayers: 4, output: Console.Out))
game.Add("Rachel Riley");
using (var game = new PickupGame(maxPlayers: 2, output: Console.Out))
public class PickupGame : IDisposable
private List<string> _players;
private TextWriter _stream;
public PickupGame(int maxPlayers, string gameName = "Pickup Game", TextWriter output = null)
_players = new List<string>();
_stream = output ?? Console.Out;
SetMaxPlayers(maxPlayers);
public void Add(string name)
if (_players.Contains(name))
_stream.WriteLine(string.Format("{0} tried to join a game they are already in.", name));
else if (_players.Count < MaxPlayers)
_stream.WriteLine(string.Format("{0} was added to the game.", name));
public void CheckPlayerCount()
if (_players.Count == MaxPlayers)
_stream.WriteLine(string.Format("{0} should come server.", GetNames()));
public void SetMaxPlayers(int maxPlayers)
throw new ArgumentOutOfRangeException(nameof(maxPlayers));
_stream.WriteLine("{0} for {1} players was created.", GameName, MaxPlayers);
var names = string.Join(", ", _players.Take(_players.Count - 1));
return string.Format("{0} and {1}", names, _players.Last());
var names = _players.Any() ? string.Format(" with {0}", GetNames()) : null;
_stream.WriteLine(string.Format("{0} was cancelled because nobody wants to play{1}.", GameName, names));
public override string ToString()
return string.Format("{0} between {1}.", GameName, GetNames());
return string.Format("{0} with no players.", GameName);