Created
February 15, 2020 06:36
-
-
Save digitaljoni/87dc6fbd61dce6e870d63cd7a1c30e75 to your computer and use it in GitHub Desktop.
Sample Dart Code
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
| void main() { | |
| final CardDeck newDeck = CardDeck.create(); | |
| print(newDeck); | |
| } | |
| enum Suit { | |
| diamond, | |
| heart, | |
| spade, | |
| club, | |
| } | |
| class Card { | |
| Card(this.suit, this.rank); | |
| final Suit suit; | |
| final int rank; | |
| Map<Suit, String> suitSymbols = { | |
| Suit.diamond: '♦️', | |
| Suit.heart: '♥️', | |
| Suit.spade: '♠️', | |
| Suit.club: '♣️' | |
| }; | |
| Map<int, String> rankNames = { | |
| 11 : 'Jack', | |
| 12 : 'Queen', | |
| 13 : 'King', | |
| 14 : 'Ace', | |
| }; | |
| String get rankName => rankNames[rank] ?? '$rank'; | |
| @override | |
| String toString() { | |
| return '$rankName of ${suitSymbols[suit]}'; | |
| } | |
| } | |
| class CardDeck { | |
| CardDeck(this.cards); | |
| CardDeck.create() : cards = _create(); | |
| final List<Card> cards; | |
| static List<Card> _create() { | |
| List<Card> newCards = <Card>[]; | |
| Suit.values.forEach((Suit suit) { | |
| for (int rank = 2; rank <= 14; rank++) { | |
| newCards.add(Card(suit, rank)); | |
| } | |
| }); | |
| return newCards; | |
| } | |
| @override | |
| String toString() { | |
| return cards.map((Card card) => card).join(', '); | |
| } | |
| } | |
| class Scoreboard { | |
| Scoreboard(this.playerScore, this.computerScore); | |
| final int playerScore; | |
| final int computerScore; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment