Go Fish/Raku: Difference between revisions

m
(Created page with '{{collection|Go Fish}}Category:Perl 6 {{works with|Rakudo|#23 "Lisbon"}} <lang perl6>constant BOOKSIZE = 4; constant HANDSIZE = 9; constant Str @pips = <two three four five…')
 
 
(6 intermediate revisions by 4 users not shown)
Line 1:
{{collection|Go Fish}}[[Category:Perl 6]]
=={{example|task=Go Fish|language=Raku}}==
 
{{works with|Rakudo|#23 "Lisbon"2019.03.1}}
 
<syntaxhighlight lang="raku" perl6line>constant BOOKSIZE = 4;
constant HANDSIZE = 9;
constant Str @pips = <two three four five six seven eight nine ten jack queen king ace>;
# The elements of @pips are only names. Pips are represented internally
# as indices of this array.
constant Str @piparticles = <a a a a a a an a a a a a an>;
constant Str @ppips = <deuces threes fours fives sixes sevens eights nines tens jacks queens kings aces>;
constant Str @shortpips = <2 3 4 5 6 7 8 9 T J Q K A>;
constant $foe_nominative_pronoun = pick 1, <he she it e xe>;
 
Line 21 ⟶ 22:
sub find ($x, *@a) {
for @a.kv -> $k, $v {
$v eqveq $x and return $k;
}
fail 'Not found';
Line 27 ⟶ 28:
 
sub maxes (&f, *@a) {
my $x = [max] map &f, @a;
return @a.grep: { f($^e) eqv $x }, @a;
}
 
Line 37 ⟶ 38:
sub readpip (@user_hand) {
my @choices = grep { @user_hand[$^p] }, ^@pips;
if (@choices == 1) {
say "You're obliged to ask for { @ppips[@choices[0]] }.";
return @choices[0];
Line 43 ⟶ 44:
loop {
print 'For what do you ask? (', join(', ', @shortpips[@choices]), '): ';
my $in = substr uc($*IN.get.uc or next), 0, 1;
 
my $pip = find $in, @shortpips;
if defined $pip {
Line 75 ⟶ 77:
 
class Player {
has Int @.h;
# @h[$n] is number of cards of pip $n in this player's hand.
has $.deck;
Line 83 ⟶ 85:
has Knowledge @.know;
 
method new ( $cpu, @deck is rw) {
my Int @h = 0 xx @pips;
++@h[$_] for @deck[^HANDSIZE];
@deck = @deck[HANDSIZE ..^ @deck];
Player.bless(*,
h => @h, cpu => $cpu,
deck => \@deck,
know => ($cpu ?? map { Knowledge.new() }, @pips !! ())
);
Line 111 ⟶ 113:
}
 
method getcards (Int $quantity, Int $pip) {
@!h[$pip] += $quantity;
@.h[$pip] == BOOKSIZE or return;
Line 123 ⟶ 125:
}
 
method losecards (Int $pip) {
@.h[$pip] = 0;
while none @.h and $.deck.elems {
say do $.cpu
?? "The dealer's hand is empty, so $foe_nominative_pronoun draws a new card."
!! "Your hand's empty, so you draw a new card.";
Line 133 ⟶ 135:
}
 
method learn (Int $pip, Maybe $m) { @.know[$pip].set($m) }
 
method notice_draw () { .incr for @.know }
 
method choose_request () returns Int {
#self.showhand;
#say 'Know: ', join ', ', map
Line 143 ⟶ 145:
# @.know;
my @ps = map { .key }, grep { .value }, pairs @.h;
 
return pick 1,( maxes { @.h[$^p] }, do
# Most of all we should ask for cards we know the
# user has.
@ps.grep ({ @.know[$^p].maybe ~~ Yes }, @ps).flat or||
# Then try asking for one we haven't requested
# before.
@ps.grep ({ @.know[$^p].maybe ~~ Dunno }, @ps).flat or||
# Then try asking for one we least recently
# asked about.
maxes { @.know[$^p].n }, @ps ).roll;
}
}
Line 158 ⟶ 161:
sub play () {
 
my Int @deck;
# Shuffle the deck until the first two hands contain no books.
# (If BOOKSIZE is greater than 2 and HANDSIZE is reasonably
# small, this'll probably take only one shuffle.)
repeat { @deck = pick *,(flat ^@pips xx BOOKSIZE).pick(*) }
until none(map { count $^x, @deck[^HANDSIZE] }, ^@pips) >= BOOKSIZE and
none(map { count $^x, @deck[HANDSIZE ..^ 2*HANDSIZE] }, ^@pips) >= BOOKSIZE;
Line 168 ⟶ 171:
my Player $user .= new(False, @deck);
my Player $foe .= new(True, @deck);
 
while any |$user.h or any |$foe.h {
 
# The user goes first.
while any |$user.h {
Line 223 ⟶ 226:
}
 
sub MAIN () { play }</langsyntaxhighlight>
10,327

edits