Last active
February 22, 2020 09:41
-
-
Save digitaljoni/6e6b81c1acff41cef053b40762a41645 to your computer and use it in GitHub Desktop.
Deck 2 - Deck Create
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 Card { | |
| Card(this.suit, this.rank); | |
| final String suit; | |
| final int rank; | |
| final Map<int, String> rankNames = { | |
| 11: 'Jack', | |
| 12: 'Queen', | |
| 13: 'King', | |
| 1: 'Ace', | |
| }; | |
| String get rankName => rankNames[rank] ?? '$rank'; | |
| String get suitSymbol => suitSymbols[suit]; | |
| @override | |
| String toString() { | |
| return '$rankName of $suitSymbol'; | |
| } | |
| } | |
| final Map<String, String> suitSymbols = { | |
| 'd': 'Diamonds', | |
| 'h': 'Hearts', | |
| 's': 'Spades', | |
| 'c': 'Clubs' | |
| }; | |
| class Deck { | |
| List<Card> cards = <Card>[]; | |
| Deck() { | |
| final List<String> suits = suitSymbols.keys.toList(); | |
| suits.forEach((String suit) { | |
| for (int rank = 1; rank <= 13; rank++) { | |
| cards.add(Card(suit, rank)); | |
| } | |
| }); | |
| } | |
| @override | |
| String toString() { | |
| return cards.map((Card card) => card).join(', '); | |
| } | |
| } | |
| void main() { | |
| final Deck newDeck = Deck(); | |
| print(newDeck); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment