Created
April 25, 2021 09:26
-
-
Save voratham/a2f896300703aa0ee7ea908677ccf383 to your computer and use it in GitHub Desktop.
Dart101
This file contains hidden or 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
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