RCRPG/Raku: Difference between revisions
Content deleted Content added
→Code: Note that it hasn't yet been added to RCRPG. |
Thundergnat (talk | contribs) m →RCRPG/Raku: Fix syntax highlighting |
||
(11 intermediate revisions by 2 users not shown) | |||
Line 1:
{{collection|RCRPG}}
=={{example|task=RCRPG|language=Raku}}==
This [[
==Language Idioms==
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]]
==Commands==
Line 12 ⟶ 23:
As with the [[RCRPG/Perl|Perl version]].
==Code==
{{works with|Rakudo|2018.03}}
<syntaxhighlight lang="raku" line>my %commands;
my %rooms;
Line 20 ⟶ 30:
my $stuff = [];
my $equipped = '';
my @cl2 = $l2;▼
}▼
sub serial_and(*@list) {▼
my @delims = map { ", " }, @list;▼
}▼
my $WHITE = "\e[0m";
Line 39 ⟶ 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 50 ⟶ 49:
east => [ 1, 0, 0],
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) > .85 ?? [@random_items.roll(2)] !!
my $next = 0;▼
$roll > .25 ?? [@random_items.roll] !! [];
has $.name = "The Unnamed Room";
has $.links = [];
Line 66:
say "There is an exit %dir_str{$.links[0]}.";
} else {
say "There are exits: {
.subst(/','(<-[,]>+)$/, -> $/ {" and$0"})}.";
}
if $.stuff {
say "There is {serial_and(
} else {
say $BLUE, "There is nothing useful here you can take.", $WHITE;
Line 78 ⟶ 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]);
▲sub serial_and(*@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 86 ⟶ 98:
say "There needs to be a ladder in the room before you can climb.";
} elsif grep $dir, @( %rooms{$room}.links ) {
$room = [
} else {
say "Your way is blocked.";
Line 104 ⟶ 116:
}
for flat <north south east west up down> -> $dir {
command [$dir, substr($dir,0,1)], '', 0, { move($dir) };
}
Line 111 ⟶ 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' {▼
}
elsif grep $dir, @( %rooms{$room}.links ) {
say "You swing {$equipped ?? "the $equipped" !! 'your
~ 'in the wall. It makes a nice breeze.';
▲ 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 = [
my $r2o = %rooms{$r2} //= Room.new;
say
push @( %rooms{$room}.links ), $dir;
push @( $r2o.links ), %dir_rev{$dir};
Line 133 ⟶ 147:
say "You're not carrying anything. Ask again later.";
} else {
say "You have {serial_and(
}
};
Line 199 ⟶ 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...';
};
Line 204 ⟶ 227:
%rooms{$room}.greet;
my @args = $in.words;
if @args && %commands{lc @args[0]} -> $fun {
Line 212 ⟶ 235:
}
%rooms{$room}.greet;
}</syntaxhighlight >
|