Skip to content

Instantly share code, notes, and snippets.

@voratham
Created April 25, 2021 09:26
Show Gist options
  • Save voratham/a2f896300703aa0ee7ea908677ccf383 to your computer and use it in GitHub Desktop.
Save voratham/a2f896300703aa0ee7ea908677ccf383 to your computer and use it in GitHub Desktop.
Dart101
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(suit: suit, rank : rank);
cards.add(card);
}
}
}
shuffer() {
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);
}
toString() {
return cards.toString();
}
}
class Card {
String suit;
String rank;
Card({this.suit, this.rank});
toString() {
return "$rank of $suit";
}
}
void main() {
var deck = new Deck();
deck.removeCard("Diamonds", "Ace");
print(deck);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment