Created
July 8, 2019 08:12
-
-
Save robinmitra/0480b454d02180083458f24549500d03 to your computer and use it in GitHub Desktop.
Learning Dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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