Created
November 20, 2019 16:18
-
-
Save cptiwari20/9f08494eeb5057248239566cc3d2c2fb to your computer and use it in GitHub Desktop.
Deck Game
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() { | |
| var deck = new Deck(); | |
| // deck.shuffle(); //shuffle the list of cards inside deck | |
| print(deck.cardsWithSuit('Hearts')); | |
| deck.removeCard('Diamonds', 'Ace'); | |
| // print(deck); | |
| print(deck.deal(10)); | |
| print(deck); | |
| } | |
| class Deck { | |
| // creating a list | |
| List<Card> cards = []; | |
| Deck() { | |
| var suits = ['Diamonds', 'Spades', 'Hearts', 'Clubs']; | |
| var ranks = ['Ace', 'one', 'two', 'three']; | |
| for (var suit in suits) { | |
| for (var rank in ranks) { | |
| var card = new Card(suit: suit, rank: rank); //named parameter | |
| cards.add(card); | |
| } | |
| } | |
| } | |
| toString() { | |
| return cards.toString(); // it will find toString method to cards lists | |
| } | |
| cardsWithSuit(String suit) { | |
| return cards.where((card) => card.suit == suit); // filter the list | |
| } | |
| removeCard(suit, rank){ | |
| return cards.removeWhere((card) => (card.suit == suit) && (card.rank == rank)); | |
| } | |
| deal(int handCard){ | |
| var hand = cards.sublist(0, handCard); // cut all the cards between two indexes | |
| cards = cards.sublist(handCard); // remove all the cards after this index | |
| return hand; | |
| } | |
| shuffle() { | |
| return cards.shuffle(); // shuffles the list | |
| } | |
| } | |
| class Card { | |
| // creating static variables | |
| String suit; | |
| String rank; | |
| Card({this.rank, this.suit}); // setting the named parameter with constructor function | |
| toString() { | |
| return '$rank of $suit'; | |
| } | |
| } | |
| // 2019-08-16T16:47 | |
| // 2019-08-16 16:47 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment