-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cards_game.dart
114 lines (100 loc) · 2.53 KB
/
Cards_game.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import 'dart:io';
// Define the main function
void main() {
// Create a new deck of cards
var deck = new Deck();
print("Choose a number between 1 and 4");
print("1. Print the deck of cards");
print("2. Shuffle the deck of cards");
print("3. Get all cards with suit 'Diamonds'");
print("4. Deal 3 cards from the deck");
print("5. Exit");
// Get the user's choice of action using switch case
var choice = int.parse(stdin.readLineSync()!);
switch (choice) {
case 1:
print(deck);
break;
case 2:
deck.Shuffle();
print(deck);
break;
case 3:
print(deck.cardsWithSuit('Diamonds'));
break;
case 4:
deck.Shuffle();
print(deck.Deal(3));
break;
case 5:
break;
default:
print("Invalid choice");
}
}
// Define the Deck class
class Deck {
// Define a list of Card objects
List<Card> cards = [];
// Define the constructor for the Deck class
Deck() {
// Define the possible ranks and suits for the cards
var ranks = [
'Ace',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Jack',
'Queen',
'King'
];
var suits = ['Diamonds', 'Hearts', 'Clubs', 'Spades'];
// Create a new Card object for each possible combination of rank and suit
for (var suit in suits) {
for (var rank in ranks) {
var card = new Card(rank, suit);
cards.add(card);
}
}
}
// Define a method to return a string representation of the deck of cards
toString() {
return cards.toString();
}
// Define a method to shuffle the deck of cards
Shuffle() {
cards.shuffle();
}
// Define a method to get all cards with a certain suit
cardsWithSuit(String suit) {
return cards.where((card) => card.suit == suit);
}
// Define a method to deal a certain number of cards from the deck
Deal(int handSize) {
var hand = cards.sublist(0, handSize);
cards = cards.sublist(handSize);
return hand;
}
// Define a method to remove a specific card from the deck
removeCard(String suit, String rank) {
cards.removeWhere((card) => (card.suit == suit) && (card.rank == rank));
}
}
// Define the Card class
class Card {
// Define properties for the suit and rank of the card
String suit = '';
String rank = '';
// Define the constructor for the Card class
Card(this.rank, this.suit);
// Define a method to return a string representation of the card
toString() {
return '$rank of $suit';
}
}