public bool TakeTurn(ICentralPile centralPile, ICardTransport transport) { if (Hand.IsEmpty) { return(false); } transport.Transfer(Hand, centralPile); return(true); }
public static async Task <int> Main() { Console.OutputEncoding = Encoding.UTF8; var container = GetConfiguredContainer(); using (var scope = container.BeginLifetimeScope()) { var game = new SnapGame(scope); IEnumerable <IPlayer> players = CreatePlayers(scope); ICardDealer dealer = CreateDealer(scope); var deck = CreateDeck(scope); ICardTransport transport = CreateTransport(scope); ICentralPile centralPile = CreateCentralPile(scope); await game.Play(players, dealer, deck, transport, centralPile); Console.WriteLine($"Winner: {(game.HasWinner ? game.Winner.Name : "The Game!!!")}"); Console.ReadKey(); return(game.HasWinner ? 1 : 0); } }
public SnapCardDealer(ICardTransport cardTransport) { this.cardTransport = cardTransport; }
private async Task ProcessPlayerSnaps(IEnumerable <IPlayer> players, ICentralPile centralPile, ICardTransport transport) { var results = (from p in players select p) .ToDictionary(k => k, v => v.CheckForMatchAsync(centralPile)); var checkTask = await Task.WhenAny(Timeout(), Task.WhenAny(results.Values)); var snappers = results.Where(o => o.Value.IsCompletedSuccessfully && o.Value.Result); if (snappers.Any()) { var player = snappers.First().Key; transport.Transfer(centralPile, player.Hand); DumpState($"Snap ({player.Name})", players, centralPile.GetAll()); } async Task <bool> Timeout() { await Task.Delay(3000); return(false); } }
public async Task Play(IEnumerable <IPlayer> players, ICardDealer dealer, ICardDeck deck, ICardTransport transport, ICentralPile centralPile) { deck.Shuffle(); DumpState("Deck", players, centralPile.GetAll()); dealer.Deal(deck, players); while (!HasWinner && players.Any(p => !p.Hand.IsEmpty)) { foreach (var player in players) { player.TakeTurn(centralPile, transport); } DumpState("Play", players, centralPile.GetAll()); await ProcessPlayerSnaps(players, centralPile, transport); if (players.Where(o => o.Hand.IsEmpty).Count() == (players.Count() - 1)) { var winner = (from p in players orderby p.Hand.Count descending select p) .First(); Winner = winner; } } }