RCRPG/Raku: Difference between revisions
m
→RCRPG/Raku: Fix syntax highlighting
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: minor bug fix - needs a positional) |
Thundergnat (talk | contribs) 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 [[
==Language Idioms==
This version of RCRPG demonstrates the following idioms in
* A simple [[input loop]]
* Subroutines (Functions)
Line 23:
As with the [[RCRPG/Perl|Perl version]].
==Code==
{{works with|
<syntaxhighlight lang="raku" line>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
< north south east west up down >;
my class Room {
has $.stuff = (my $roll = 1.rand) > .
$roll > .
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 %
map { %
my $items = (map { "{%
%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
~ " 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 >
▲}
|