Skip to content

Instantly share code, notes, and snippets.

@robinmitra
Created July 8, 2019 08:12
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 robinmitra/0480b454d02180083458f24549500d03 to your computer and use it in GitHub Desktop.
Save robinmitra/0480b454d02180083458f24549500d03 to your computer and use it in GitHub Desktop.
Learning Dart
class Deck {
List<Card> cards = [];
Deck() {
var ranks = ['Ace', 'Two', 'Three', 'Four'];
var suits = ['Diamonds', 'Hearts', 'Clubs', 'Spades'];
for (var suit in suits) {
for (var rank in ranks) {
var card = Card(rank: rank, suit: suit);
this.cards.add(card);
}
}
}
shuffle() {
cards.shuffle();
}
getCardsBySuit(String suit) {
return cards.where((card) => card.suit == suit);
}
deal(int handSize) {
var hand = cards.sublist(0, handSize);
cards = cards.sublist(handSize);
return hand;
}
removeCard(String suit, String rank) {
cards.removeWhere((card) => card.suit == suit && card.rank == rank);
}
toString() {
return cards.toString();
}
}
class Card {
String rank;
String suit;
Card({this.rank, this.suit});
toString() {
return '$rank of $suit';
}
}
main() {
var deck = new Deck();
// deck.shuffle();
print(deck);
print(deck.deal(5));
print(deck);
deck.removeCard('Spades', 'Four');
print(deck);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment