Skip to content

Instantly share code, notes, and snippets.

@RaneWallin
Created October 30, 2020 18:45
Show Gist options
  • Save RaneWallin/49357f143214cc89d246c2fc9afa8aa0 to your computer and use it in GitHub Desktop.
Save RaneWallin/49357f143214cc89d246c2fc9afa8aa0 to your computer and use it in GitHub Desktop.
Deck of cards in Dart
void main() {
Deck deck = new Deck();
print(deck.length());
deck.removeCard("Jack", "Hearts");
print(deck.length());
print(deck);
}
class Deck {
List<Card> cards = [];
Deck() {
List ranks = [
"Ace",
"King",
"Queen",
"Jack",
"10",
"9",
"8",
"7",
"6",
"5",
"4",
"3",
"2"
];
List suits = ["Hearts", "Spades", "Diamonds", "Clubs"];
for (String suit in suits) {
for (String rank in ranks) {
cards.add(new Card(rank: rank, suit: suit));
}
}
}
String toString() {
return cards.toString();
}
void shuffle() {
cards.shuffle();
}
List<Card> cardsWithSuit(String suit) {
return (cards.where((Card card) => card.suit == suit)).toList();
}
Hand deal(int handSize) {
List<Card> handContents = this.cards.sublist(0, handSize);
this.cards.removeRange(0, handSize);
return new Hand(handContents);
}
void removeCard(String rank, String suit) {
cards.removeWhere((Card card) => (card.suit == suit && card.rank == rank));
}
int length() {
return cards.length;
}
}
class Card {
String rank;
String suit;
Card({this.rank, this.suit});
String toString() {
return "$rank of $suit";
}
}
class Hand {
List<Card> hand = [];
Hand(this.hand);
String toString() {
return hand.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment