private void Apply(MovePerformedGameEvent @event) { if (@event.CardPlayed.IsOneEyedJack()) { _board.RemoveChip(@event.Row, @event.Column); } else { _board.PutChip(@event.Row, @event.Column, @event.Team); } var hand = _hands[@event.UserId]; hand.Remove(@event.CardPlayed); foreach (var deadCard in @event.DeadCards) { hand.Remove(deadCard); } for (var i = 0; i < @event.NumCardsDealt; i++) { if (_deck.Count == 0) { continue; } var card = _deck.Draw(); hand.Add(card); } var currentUserIndex = _users.IndexOf(CurrentUserId); var nextUserIndex = (currentUserIndex + 1) % _users.Count; CurrentUserId = _users[nextUserIndex]; }
public Game PlayCard(Guid userId, int row, int column, Card card) { if (card == null) { throw new ArgumentNullException("card"); } if (Status != GameStatus.Playing) { throw new InvalidOperationException("Game is not under way."); } var user = _users.SingleOrDefault(id => id == userId); if (user == null) { throw new InvalidOperationException("User is not in game."); } if (GameBoard.IsShared(row, column)) { var error = string.Format("{{ Row: {0}, Column: {1} }} is a corner.", row.ToString(), column.ToString()); throw new InvalidOperationException(error); } if (card.IsOneEyedJack()) { if (_board.IsFree(row, column)) { throw new InvalidOperationException("Tile is empty."); } var sequences = _board.GetSequences(); if (sequences.Any(s => s.Contains(row, column))) { var error = string.Format("{{ Row: {0}, Column: {1} }} is part of a sequence.", row, column); throw new InvalidOperationException(error); } } else { if (!Equals(GameBoard.GetCard(row, column), card) && !card.IsTwoEyedJack()) { throw new InvalidOperationException("Card does not match tile."); } } if (_hands[userId].Contains(card) == false) { throw new InvalidOperationException("User does not have that card."); } var index = _users.IndexOf(user); var team = (Team) (index % 2 + 1); var deadCards = _hands[userId].Where(c => _board.IsCardDead(c)); var dealtCards = deadCards.Count() + 1; var e = new MovePerformedGameEvent(Guid.NewGuid(), Id, userId, Version + 1) { CardPlayed = card, NumCardsDealt = dealtCards, DeadCards = deadCards, Column = column, Created = DateTimeOffset.Now, Row = row, Team = team }; RaiseEvent(e); return this; }