public Card DetermineCardToThrow(GameRules gameRules, IList<Card> pile, IEnumerable<Card> handLeft) { ArgumentCheck(pile, handLeft); handLeft = handLeft.ToList(); if(pile.Count == 0) { return StandardFirstCardPlay(handLeft); } int currentPileCount = _scoreCalculator.SumValues(pile); var validPlays = handLeft.Where(c => currentPileCount + _valueStrategy.ValueOf(c) <= GameRules.PlayMaxScore).ToList(); var maxScore = validPlays .Select(card => new CardScore(card, _scoreCalculator.CountThePlay(new List<Card>(pile) { card }))) .MaxBy(cs => cs.Score); if(maxScore.Score == 0) { return validPlays.MaxBy(c => _valueStrategy.ValueOf(c)); } else { return maxScore.Card; } }
public Card DetermineCardToThrow(GameRules gameRules, IList<Card> pile, IEnumerable<Card> handLeft) { ArgumentCheck(pile, handLeft); int currentPileCount = _scoreCalculator.SumValues(pile); var validPlays = handLeft.Where(c => currentPileCount + _valueStrategy.ValueOf(c) <= GameRules.PlayMaxScore).ToList(); var randomIndex = validPlays.Count - 1; return validPlays.ElementAt(randomIndex); }
/// <summary> /// Synchronous Game of Cribbage /// </summary> /// <param name="gameRules">Set of rules the game will abide by.</param> /// <param name="players">2-4 players</param> /// <param name="deck"></param> public CribbageGameRunner(List<Player> players = null, GameRules gameRules = null, Deck deck = null) { _gameRules = gameRules ?? new GameRules(); _players = players ?? new List<Player> { new Player("Player 1"), new Player("Player 2") }; if (_players.Count > 4 || _players.Count < 2) throw new ArgumentOutOfRangeException(nameof(players)); if (_players.Count != _gameRules.PlayerCount) throw new ArgumentOutOfRangeException(nameof(players)); _deck = deck ?? new Deck(); }
public Card DetermineCardToThrow(GameRules gameRules, IList<Card> pile, IEnumerable<Card> handLeft) { ArgumentCheck(pile, handLeft); int currentPileCount = _scoreCalculator.SumValues(pile); var validPlays = handLeft.Where(c => currentPileCount + _valueStrategy.ValueOf(c) <= GameRules.PlayMaxScore); var x = validPlays.Select(card => new {card, theoreticalPlayHand = new List<Card>(pile) {card}}) .Select(a => new {t = a, score = _scoreCalculator.CountThePlay(a.theoreticalPlayHand)}) .Select(anon => new CardScore(anon.t.card, anon.score)) .ToList(); var lowScore = x.Min(cs => cs.Score); return x.First(c => lowScore == c.Score).Card; }
public void Test_LowerCardIsPlayed_EqualValue() { var gameRules = new GameRules(); var pile = new List<Card>(); var handLeft = new List<Card> { new Card(Rank.Two, Suit.Clubs), new Card(Rank.Two, Suit.Diamonds), new Card(Rank.Two, Suit.Hearts), new Card(Rank.Two, Suit.Spades) }; var thrown = _lowestCardPlayStrategy.DetermineCardToThrow(gameRules, pile, handLeft); thrown.Should().Be(new Card(Rank.Two, Suit.Clubs)); }
public Cribbage(IEnumerable<int> players, GameRules rules, IEnumerable<IEventListener> eventListeners = null) { State = new GameState(); var listeners = new List<IEventListener>(eventListeners ?? new List<IEventListener>()) { new GameStateEventListener(State, new GameStateBuilder()) }; Stream = new EventStream(listeners) { new GameStartedEvent { GameId = Guid.NewGuid(), Occurred = DateTimeOffset.Now, Rules = rules, Players = players.ToList() }, new DeckShuffledEvent {Deck = _deck.ToList(), GameId = State.Id} }; }
public Card DetermineCardToThrow(GameRules gameRules, IList<Card> pile, IEnumerable<Card> handLeft) { ArgumentCheck(pile, handLeft); return handLeft.OrderBy(c => _orderStrategy.Order(c)).ThenBy(c => (int) c.Suit).First(); }