RCRPG/Raku: Difference between revisions

m
→‎RCRPG/Raku: Fix syntax highlighting
m (→‎{{header|Perl 6}}: minor bug fix - needs a positional)
m (→‎RCRPG/Raku: Fix syntax highlighting)
 
(6 intermediate revisions by the same user not shown)
Line 1:
{{collection|RCRPG}}
=={{example|task=RCRPG|language=Raku}}==
 
This [[Perl 6Raku]] version of [[RCRPG]] implements a text interface.
 
==Language Idioms==
This version of RCRPG demonstrates the following idioms in Perl 6Raku.
* A simple [[input loop]]
* Subroutines (Functions)
Line 23:
As with the [[RCRPG/Perl|Perl version]].
==Code==
{{works with|nieczaRakudo|v242018.03}}
<syntaxhighlight lang="raku" line>my %commands;
{{works with|Rakudo|2013.05}}
<lang perl6>
my %commands;
my %rooms;
 
Line 42 ⟶ 40:
up => 'in the ceiling',
down => 'in the floor',
(map { ($_ => "to the $_") }, <north south east west>).Slip;
 
my %dir_vec =
Line 52 ⟶ 50:
west => [-1, 0, 0];
 
my %dir_rev = map { ($^a => $^b), $^b => $^a).Slip },
< north south east west up down >;
 
my class Room {
has $.stuff = (my $roll = 1.rand) > .2585 ?? [@random_items.roll(2)] !!
$roll > .8525 ?? [@random_items.roll(2)] !! [];
has $.name = "The Unnamed Room";
has $.links = [];
Line 69 ⟶ 67:
} else {
say "There are exits: {@$.links.join(', ')\
.subst(/','(<-[,]>+)$/, -> $/ {" and$0"})}.";
}
if $.stuff {
Line 85 ⟶ 83:
 
sub serial_and(*@list) {
my %stuffthings;
map { %stuffthings{$_}++ }, @list;
my $items = (map { "{%stuffthings{$_} > 1 ?? %stuffthings{$_} !! 'a'} {plural($_" }, %stuff.keysthings{$_}).join:}" '}, ';
%things.keys).join: ', ';
$items.subst( /','(<-[,]>+)$ /, -> $/ {" and$0"} );
 
sub plural ($item, $count) {
return $count != 1 ?? $item~'s' !! $item
}
 
Line 113 ⟶ 116:
}
 
for flat <north south east west up down> -> $dir {
command [$dir, substr($dir,0,1)], '', 0, { move($dir) };
}
Line 120 ⟶ 123:
-> $dir {
if !(%dir_vec{$dir}:exists) {
say "I don't know that direction. Try one of: {sort keys %dir_vec}";
}
elsif grep $dir, @( %rooms{$room}.links ) {
Line 215 ⟶ 218:
{
print 'The Muddy walls dim. ';
say "You glance down as {serial_and( @$stuff )} {plural('clatter{', +!(@$stuff >== 1 ?? 's' !! ''))} to the floor then fades away." if @$stuff;
~ " to the floor and {plural('fade', +!(@$stuff == 1))} away." if @$stuff;
say 'It is as if you are awakening from a strange (and extremly low resolution) dream...';
exit;
Line 224 ⟶ 228:
 
while my $in = $*IN.get {
my @args = $in.words;
if @args && %commands{lc @args[0]} -> $fun {
$fun(|@args);
Line 231 ⟶ 235:
}
%rooms{$room}.greet;
}</syntaxhighlight >
</lang>
10,327

edits