Skip to content

Instantly share code, notes, and snippets.

@Quingsley
Last active August 10, 2022 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Quingsley/8e265876bf22e4ef1eb59c40d0909ae3 to your computer and use it in GitHub Desktop.
Save Quingsley/8e265876bf22e4ef1eb59c40d0909ae3 to your computer and use it in GitHub Desktop.
DartFundamentals
void main() {
var deck = Deck();
//deck.shuffle();
// print(deck.cardsWithSuit('Clubs'));
// print(deck);
// print(deck.deal(4));
// print(deck);
print(deck);
deck.removeCard('Diamonds','Ace');
print(deck);
}
class Deck {
List<Card> cards = [];
Deck() {
var ranks = [
'Ace',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Joker',
'Queen',
'King'
];
var suits = ['Diamonds', 'Clubs', 'Spades', 'Hearts'];
for (var suit in suits) {
for (var rank in ranks) {
var card = Card(rank: rank, suit: suit);
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) {
return 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