Skip to content

Instantly share code, notes, and snippets.

@aelshamy
Created August 30, 2018 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aelshamy/c36bc2138ccf0e33364b8826ef3e02ba to your computer and use it in GitHub Desktop.
Save aelshamy/c36bc2138ccf0e33364b8826ef3e02ba to your computer and use it in GitHub Desktop.
void main() {
var deck = new Deck();
deck.shuffle();
deck.deal(5);
print(deck.deal(5));
}
class Deck {
List<Card> cards = [];
final ranks = [
'Ace',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Jack',
'Queen',
'King'
];
final suits = ['Diamonds', 'Hearts', 'Clubs', 'Spades'];
Deck() {
for (var suit in suits) {
for (var rank in ranks) {
var card = new Card(rank: rank, suit: suit);
cards.add(card);
}
}
}
shuffle() {
cards.shuffle();
}
Iterable<Card> cardWithSuit(String suit) {
return cards.where((card) => card.suit == suit);
}
List<Card> deal(int handSize) {
var hand = cards.sublist(0, handSize);
cards = cards.sublist(handSize);
return hand;
}
toString() {
return cards.toString();
}
}
class Card {
String rank;
String suit;
Card({this.rank, this.suit});
toString() {
return '$rank of $suit';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment