Deck constructor
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<Card> cards,
}) {
// At least 50 cards.
if (cards.length < 50) {
throw ArgumentError.value(
cards.length,
'cards.length',
'must contain at least 50 cards',
);
}
// Ensure that there are no bases or leaders in the deck and <= 3 copies.
final count = <Card, int>{};
for (final card in cards) {
switch (card) {
case BaseCard():
throw ArgumentError.value(
card,
'cards',
'cannot contain a base card',
);
case UnitCard() when card.isLeader:
throw ArgumentError.value(
card,
'cards',
'cannot contain a leader card',
);
case _:
final total = count.update(
card,
(value) => value + 1,
ifAbsent: () => 1,
);
if (total > 3) {
throw ArgumentError.value(
card,
'cards',
'cannot contain more than 3 copies of the same card',
);
}
}
}
return Deck._(
leader: leader,
base: base,
cards: List.unmodifiable(cards),
);
}