Deck constructor

Deck({
  1. required UnitCard leader,
  2. required BaseCard base,
  3. required List<PlayableCard> cards,
})

Creates a new deck with the given leader, base, and other cards.

Errors

This class is not intended to be used with user-provided input, and as such does not provide any error handling. If any of the fields are invalid, an error will be thrown.

Implementation

factory Deck({
  required UnitCard leader,
  required BaseCard base,
  required List<PlayableCard> cards,
}) {
  // At least 48 cards.
  if (cards.length < 48) {
    throw ArgumentError.value(
      cards.length,
      'cards.length',
      'must contain at least 48 cards (i.e. 50 including leader and base)',
    );
  }

  // Ensure that there are no leaders in the deck and <= 3 copies.
  final count = <Card, int>{};
  for (final card in cards) {
    switch (card) {
      case TokenCard():
        throw ArgumentError.value(
          card.name,
          'cards',
          'cannot contain token cards',
        );
      case UnitCard() when card is LeaderCard:
        throw ArgumentError.value(
          card.name,
          'cards',
          'cannot contain a leader card',
        );
      case _:
        final total = count.update(
          card,
          (value) => value + 1,
          ifAbsent: () => 1,
        );
        if (total > 3) {
          throw ArgumentError.value(
            card.name,
            'cards',
            'cannot contain more than 3 copies of the same card',
          );
        }
    }
  }

  return Deck._(
    leader: leader,
    base: base,
    cards: List.unmodifiable(cards),
  );
}