use v6; use strict; use Blackjack::Card; class Blackjack::Hand { has @.cards; has $.name; method draw( Blackjack::Card $card ) { push self.cards, $card; } method highval() { my $vals = grep { $_ <= 21 }, self.values; return "BUSTED" if !$vals; return $vals; } method values() { my $total = 0; for self.cards -> $card { $total += $card.val; } return $total; } method is_busted() { return !( self.values <= 21 ); } method Str() { my $str = ""; for self.cards -> $card { $str ~= "-" if $str.chars; $str ~= "$card"; } return "{self.name}: " ~ "$str ({self.highval})"; } method is_blackjack() { return self.cards.elems == 2 && self.values == 21; } method score( Blackjack::Hand $other ) { return -1 if self.is_busted; return 1 if $other.is_busted; return 0 if self.is_blackjack and $other.is_blackjack; return 1.5 if self.is_blackjack; return -1 if $other.is_blackjack; self.highval > $other.highval ?? return 1 !! return 0; } }