Last active
February 22, 2019 13:38
-
-
Save zanuka/7adee8b8fab2ef0daccbe7e31f7a0e94 to your computer and use it in GitHub Desktop.
deal cards in Dart
This file contains 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
<h2>Dart Cards</h2> | |
<p>card dealing experiments based on Stephen Grider's <a href="https://github.com/StephenGrider/FlutterCasts">Fluttercasts</a></p> | |
<div> | |
<h3>Deal Two Cards<h3> | |
<button id="deal_two_button">DEAL</button><br/> | |
<label id="deal_two_result"></label> | |
</div> | |
<br/><br/> | |
<div> | |
<h3>Deal Five Cards<h3> | |
<button id="deal_five_button">DEAL</button><br/> | |
<label id="deal_five_result"></label> | |
</div> |
This file contains 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
import 'dart:html'; | |
main() { | |
var deck = Deck(); | |
// deck.removeCard('Diamonds', 'Ace'); // detect a missing card... | |
var length = deck.getLength(); | |
var diamonds = deck.cardsWithSuit('Diamonds'); | |
var hearts = deck.cardsWithSuit('Hearts'); | |
var clubs = deck.cardsWithSuit('Clubs'); | |
var spades = deck.cardsWithSuit('Spades'); | |
deck.shuffle(); | |
print(deck); | |
print(length); | |
print('Diamonds = $diamonds'); | |
print('Hearts = $hearts'); | |
print('Clubs = $clubs'); | |
print('Spades = $spades'); | |
} | |
class Deck { | |
List<Card> cards = []; | |
Deck() { | |
// html element behavior | |
querySelector("#deal_two_button").onClick.listen((e) { | |
querySelector('#deal_two_result').text = deal(2).toString(); | |
}); | |
querySelector("#deal_five_button").onClick.listen((e) { | |
querySelector('#deal_five_result').text = deal(5).toString(); | |
}); | |
var ranks = [ | |
'Ace', | |
'Two', | |
'Three', | |
'Four', | |
'Five', | |
'Six', | |
'Seven', | |
'Eight', | |
'Nine', | |
'Ten', | |
'Jack', | |
'Queen', | |
'King', | |
]; | |
var suits = ['Diamonds', 'Hearts', 'Clubs', 'Spades']; | |
for (var suit in suits) { | |
for (var rank in ranks) { | |
var card = new Card(rank, suit); | |
cards.add(card); | |
} | |
} | |
} | |
toString() { | |
return cards.toString(); | |
} | |
shuffle() { | |
cards.shuffle(); | |
} | |
getLength() { | |
return cards.length; | |
} | |
cardsWithSuit(String suit) { | |
return cards.where((card) => card.suit == suit); | |
} | |
deal(int handSize) { | |
var hand = cards.sublist(0, handSize); | |
cards = cards.sublist(handSize); | |
return hand; | |
} | |
removeCard(String suit, String rank) { | |
cards.removeWhere((card) => (card.suit == suit) && (card.rank == rank)); | |
} | |
} | |
class Card { | |
String suit; | |
String rank; | |
Card(this.rank, this.suit); | |
toString() { | |
return '$rank of $suit'; | |
} | |
} | |
This file contains 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
h2 { | |
margin-bottom: 0; | |
text-align: center; | |
} | |
div { | |
text-align: left; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment