RCRPG/Raku: Difference between revisions

m
→‎RCRPG/Raku: Fix syntax highlighting
m (→‎Commands: typo fix)
m (→‎RCRPG/Raku: Fix syntax highlighting)
 
(14 intermediate revisions by 2 users not shown)
Line 1:
{{collection|RCRPG}}
=={{example|task=RCRPG|language=Raku}}==
This [[Perl 6Raku]] version of [[RCRPG]] implements a text interface.
 
==Language Idioms==
This [[Perl 6]] version of [[RCRPG]] implements a text interface.
This version of RCRPG demonstrates the following idioms in Raku.
* A simple [[input loop]]
* Subroutines (Functions)
* Nested, lexically scoped Subroutines.
* Nested data structures using list and hash references
* Function references
* Range constructors
* Whatever codes
* Infinite lazy lists
* Meta Operators
* User output
* [[Linked list|Linked lists]]
 
==Language Idioms==
==Commands==
As with the [[RCRPG/Perl|Perl version]].
Line 10 ⟶ 23:
As with the [[RCRPG/Perl|Perl version]].
==Code==
{{works with|Rakudo|2018.03}}
Note that this contains workarounds for bugs in [[Rakudo]].
<syntaxhighlight lang="raku" line>my %commands;
<lang perl6># TODO: Z~, List.pick, fix xx
my %commands;
my %rooms;
 
Line 18 ⟶ 30:
my $stuff = [];
my $equipped = '';
 
sub zip($fun, \$l1, \$l2) {
my @cl2 = $l2;
map { $fun($_, shift @cl2) }, $l1;
 
sub serial_and(*@list) {
my @delims = map { ", " }, @list;
@delims[*-1] = "" if @delims >= 1;
@delims[*-2] = " and " if @delims >= 2;
 
join "", zip &infix:<~>, @list, @delims;
 
my $WHITE = "\e[0m";
Line 37 ⟶ 36:
 
my @random_items = < sledge ladder gold >;
 
my %dir_str =
up => 'in the ceiling',
down => 'in the floor',
(map { ($_ => "to the $_") }, <north south east west>).Slip;
 
my %dir_vec =
up => [ 0, 0, 1],
Line 48 ⟶ 49:
east => [ 1, 0, 0],
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) > .85 ?? [@random_items.roll(2)] !!
my $next = 0;
$roll > .25 ?? [@random_items.roll] !! [];
has $.stuff = [@random_items[{ $next == $_ && ($next = 0); $next++ }]];
has $.name = "The Unnamed Room";
has $.links = [];
Line 64 ⟶ 66:
say "There is an exit %dir_str{$.links[0]}.";
} else {
say "There are exits: {serial_and(@$.links)}.";join(', ')\
.subst(/','(<-[,]>+)$/, -> $/ {" and$0"})}.";
}
if $.stuff {
say "There is {serial_and(map { "a $_" }, @$.stuff )} here.";
} else {
say $BLUE, "There is nothing useful here you can take.", $WHITE;
Line 76 ⟶ 79:
}
 
%rooms{"0 0 0"} = Room.new(name => 'The Start', stuff => [<sledge>]);
%rooms{"1 1 5"} = Room.new(name => 'The Prize Room', stuff => [<gold> xx 9]);
 
[<gold gold gold gold gold gold gold gold gold>]);
sub serial_and(*@list) {
my @cl2 = $l2%things;
my @delims = map { ", "%things{$_}++ }, @list;
my $items = (map { "{%things{$_} > 1 ?? %things{$_} !! 'a'} {plural($_, %things{$_})}" },
%things.keys).join: ', ';
$items.subst( /','(<-[,]>+)$ /, -> $/ {" and$0"} );
 
sub plural ($item, $count) {
return $count != 1 ?? $item~'s' !! $item
 
sub move($dir) {
Line 84 ⟶ 98:
say "There needs to be a ladder in the room before you can climb.";
} elsif grep $dir, @( %rooms{$room}.links ) {
$room = [ zip &infix:<+>, @$room, Z+ @(%dir_vec{$dir}) ];
} else {
say "Your way is blocked.";
Line 102 ⟶ 116:
}
 
for flat <north south east west up down> -> $dir {
command [$dir, substr($dir,0,1)], '', 0, { move($dir) };
}
Line 109 ⟶ 123:
-> $dir {
if !(%dir_vec{$dir}:exists) {
say "I don't know that direction. Try one of: {sort keys %dir_vec}";
}
elsif $equipped ne 'sledge' {
say "You accomplish nothing.";
}
elsif grep $dir, @( %rooms{$room}.links ) {
say "You swing {$equipped ?? "the $equipped" !! 'your sledgefists'} wildly. at the hole ";
~ 'in the wall. It makes a nice breeze.';
my $next = 0; }
elsif $equipped ne 'sledge' {
say "You swing {$equipped ?? "the $equipped" !! 'your fists'} around purposfully "
~ 'but accomplish nothing. Perhaps you should equip a better tool?';
}
else {
my $r2 = [ zip &infix:<+>, @$room, Z+ @(%dir_vec{$dir}) ];
my $r2o = %rooms{$r2} //= Room.new;
say "'You bash until the surface crumbles, leaving a hole you can crawl through."';
push @( %rooms{$room}.links ), $dir;
push @( $r2o.links ), %dir_rev{$dir};
Line 131 ⟶ 147:
say "You're not carrying anything. Ask again later.";
} else {
say "You have {serial_and(map { "a $_" }, @$stuff )}."
}
};
Line 197 ⟶ 213:
%commands{$c2} = %commands{$c1};
}
};
 
command [<exit quit>], "(leave Muddy)", 0,
{
print 'The Muddy walls dim. ';
say "You glance down as {serial_and( @$stuff )} {plural('clatter', +!(@$stuff == 1))}"
~ " 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 202 ⟶ 227:
%rooms{$room}.greet;
 
forwhile my $*IN.linesin ->= $in*IN.get {
my @args = $in.words;
if @args && %commands{lc @args[0]} -> $fun {
Line 210 ⟶ 235:
}
%rooms{$room}.greet;
}</syntaxhighlight >
}
</lang>
10,327

edits