Go Fish/Raku: Difference between revisions
→{{header|Perl 6}}: Update to work with modern Rakudo
m (remove unneeded parens and 'do') |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Update to work with modern Rakudo) |
||
Line 1:
{{collection|Go Fish}}
{{works with|Rakudo|
<lang perl6>constant BOOKSIZE = 4;
constant HANDSIZE = 9;
constant
# The elements of @pips are only names. Pips are represented internally
# as indices of this array.
constant
constant
constant
constant $foe_nominative_pronoun = pick 1, <he she it e xe>;
Line 21:
sub find ($x, *@a) {
for @a.kv -> $k, $v {
$v
}
fail 'Not found';
Line 28:
sub maxes (&f, *@a) {
my $x = [max] map &f, @a;
return @a.grep: { f($^e) eqv $x }
}
Line 43:
loop {
print 'For what do you ask? (', join(', ', @shortpips[@choices]), '): ';
my $in = substr
my $pip = find $in, @shortpips;
if defined $pip {
Line 75 ⟶ 76:
class Player {
has
# @h[$n] is number of cards of pip $n in this player's hand.
has $.deck;
Line 83 ⟶ 84:
has Knowledge @.know;
method new ( $cpu, @deck
my
++@h[$_] for @deck[^HANDSIZE];
@deck = @deck[HANDSIZE ..^ @deck];
Player.bless(
h => @h, cpu => $cpu,
deck =>
know => ($cpu ?? map { Knowledge.new() }, @pips !! ())
);
Line 111 ⟶ 112:
}
method getcards (Int $quantity,
@!h[$pip] += $quantity;
@.h[$pip] == BOOKSIZE or return;
Line 123 ⟶ 124:
}
method losecards (
@.h[$pip] = 0;
while none @.h and $.deck.elems {
Line 133 ⟶ 134:
}
method learn (
method notice_draw () { .incr for @.know }
method choose_request ()
#self.showhand;
#say 'Know: ', join ', ', map
Line 143 ⟶ 144:
# @.know;
my @ps = map { .key }, grep { .value }, pairs @.h;
return
# Most of all we should ask for cards we know the
# user has.
@ps.grep
# Then try asking for one we haven't requested
# before.
@ps.grep
# Then try asking for one we least recently
# asked about.
maxes { @.know[$^p].n }, @ps ).roll;
}
}
Line 158 ⟶ 160:
sub play () {
my
# 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 =
until none(map { count $^x, @deck[^HANDSIZE] }, ^@pips) >= BOOKSIZE and
none(map { count $^x, @deck[HANDSIZE ..^ 2*HANDSIZE] }, ^@pips) >= BOOKSIZE;
Line 168 ⟶ 170:
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 {
|