Skip to content

Instantly share code, notes, and snippets.

@asonni
Created November 18, 2025 18:28
Show Gist options
  • Select an option

  • Save asonni/5885c2c9967cd4d1c1d8c081994fd387 to your computer and use it in GitHub Desktop.

Select an option

Save asonni/5885c2c9967cd4d1c1d8c081994fd387 to your computer and use it in GitHub Desktop.
Deck of Cards
void main() {
var deck = new Deck();
// print(deck);
// deck.shuffle();
// print('After shuffling:');
// print(deck);
// print('Deck with Spades');
// print(deck.cardsWithSuit('Spades'));
// print('Deck with Hearts');
// print(deck.cardsWithSuit('Hearts'));
print('Full deck: $deck');
// print('Deal hand: ${deck.deal(3)}');
// print('Deck after dealing: $deck');
deck.removeCard(rank: 'Ace', suit: 'Diamonds');
print('Deck after removing Ace of Diamonds: $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, suit);
cards.add(card);
}
}
}
String toString() {
return cards.toString();
}
void shuffle() {
cards.shuffle();
}
List<Card> cardsWithSuit(String suit) {
return cards.where((Card card) {
return card.suit == suit;
}).toList();
}
List<Card> deal(int handSize) {
var hand = cards.sublist(0, handSize);
cards = cards.sublist(handSize);
return hand;
}
void removeCard({required String suit, required String rank}) {
cards.removeWhere((card) => card.suit == suit && card.rank == rank);
}
}
class Card {
String? rank;
String? suit;
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