-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck_of_cards.dart
77 lines (64 loc) · 1.29 KB
/
deck_of_cards.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Deck of Cards
import 'dart:io';
void main() {
var deck = new Deck();
// print all cards
deck.printCards();
// print cards with suit
print("\n\n\nCards with suit");
print("Enter suit: ");
String? suit = stdin.readLineSync();
print(deck.cardsWithSuits(suit!));
// shuffle cards
// print("\n\n\nAfter Shuffling:");
// deck.shuffle();
// deck.printCards();
}
class Deck {
List<Card> cards = [];
Deck() {
var suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'];
var ranks = [
'Ace',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Jack',
'Queen',
'King'
];
for (var suit in suits) {
for (var rank in ranks) {
var card = new Card(rank, suit);
cards.add(card);
}
}
}
cardsWithSuits(String suit) {
return cards.where((card) => card.suit == suit);
}
// shuffle cards
shuffle() {
cards.shuffle();
}
// print all the cards in this deck
printCards() {
for (var card in cards) {
print(card);
}
}
}
class Card {
String suit;
String rank;
Card(this.rank, this.suit);
toString() {
return '$rank of $suit';
}
}