Created
September 9, 2019 14:25
-
-
Save MaxySpark/1f1c95e88a8703ec90ec867c80824a3e to your computer and use it in GitHub Desktop.
Dart Learn
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.cardsWithSuits('Diamonds')); | |
deck.removeCard('Diamonds', 'Ace'); | |
print(deck); | |
// deck.shuffle(); | |
// print(deck.deal(5)); | |
// print(deck); | |
} | |
class Deck { | |
List<Card> cards = []; | |
Deck() { | |
var ranks = ['Ace', 'Two', 'Three', 'Four', 'Five']; | |
var suits = ['Diamonds', 'Hearts', 'Clubs', 'Spades']; | |
for(var suit in suits) { | |
for(var rank in ranks) { | |
var card = new Card( | |
rank: rank, | |
suit: suit | |
); | |
cards.add(card); | |
} | |
} | |
} | |
toString() { | |
return cards.toString(); | |
} | |
shuffle() { | |
cards.shuffle(); | |
} | |
cardsWithSuits(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); | |
} | |
} | |
class Card { | |
String suit; | |
String rank; | |
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