Advertisement
jraavis

Random Deck Generator - Dart

Sep 8th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.18 KB | None | 0 0
  1. void main() {
  2.   var deck = new Deck();
  3.   deck.shuffle();
  4.   print(deck.deal(50));
  5.   print(deck);
  6.   deck.removeCard('Ace', 'Spades');
  7.   print(deck);
  8. }
  9. class Deck {
  10.   List<Card> cards = [];
  11.  
  12.   Deck() {
  13.     List<String> suits = ['Diamonds', 'Hearts', 'Clubs', 'Spades'];
  14.     List<String> ranks = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six'];
  15.     for (var suit in suits) {
  16.       for (var rank in ranks) {
  17.         var card = new Card(suit, rank);
  18.         cards.add(card);
  19.       }
  20.     }
  21.   }
  22.  
  23.   shuffle() {
  24.     cards.shuffle();
  25.   }
  26.  
  27.   cardsWithSuit(String suit) {
  28.     return cards.where((card) => card.suit == suit);
  29.   }
  30.  
  31.   deal(int handSize) {
  32.     if(handSize<=cards.length) {
  33.       List<Card> hand = cards.sublist(0, handSize);
  34.       cards = cards.sublist(handSize);
  35.       return hand;
  36.     } else {
  37.       return 'Invalid Hand Size';
  38.     }
  39.   }
  40.  
  41.   removeCard(String rank, String suit) {
  42.     cards.removeWhere((card) => card.suit==suit && card.rank == rank);
  43.     return cards;
  44.   }
  45.  
  46.   toString() {
  47.     return cards.toString();
  48.   }
  49. }
  50.  
  51. class Card {
  52.   String suit, rank;
  53.   Card(this.suit, this.rank);
  54.   toString() {
  55.     return '$rank of $suit';
  56.   }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement