Created
December 12, 2018 05:42
-
-
Save aadeshere1/31367f3538e509487bb84fd40218b05c to your computer and use it in GitHub Desktop.
This file contains 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
void main() { | |
var deck = new Deck(); | |
print(deck); | |
deck.removeCard('Diamonds', 'Ace'); | |
print(deck); | |
} | |
class Card { | |
String rank; | |
String suit; | |
Card({this.suit, this.rank}); | |
toString() { | |
return '$rank of $suit'; | |
} | |
} | |
class Deck { | |
List<Card> cards = []; | |
Deck() { | |
var ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8','9', 'J', 'Q', 'K']; | |
var suits = ['Diamonds', 'Heards', 'Clubs', 'Spades']; | |
for (var suit in suits){ | |
for (var rank in ranks){ | |
var card = new Card( | |
suit: suit, | |
rank: rank | |
); | |
cards.add(card); | |
} | |
} | |
} | |
toString() { | |
return cards.toString(); | |
} | |
shuffle() { | |
cards.shuffle(); | |
} | |
cardsWithSuit(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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment