RCRPG/Raku: Difference between revisions

→‎{{header|Perl 6}}: Updated to work with the latest versions of Niecza and Rakudo. Various tweaks and enhancements
(→‎Code: Correction)
(→‎{{header|Perl 6}}: Updated to work with the latest versions of Niecza and Rakudo. Various tweaks and enhancements)
Line 12:
As with the [[RCRPG/Perl|Perl version]].
==Code==
{{works with|niecza|v24}}
Note that this contains workarounds for bugs in [[niecza]] which aren't present in [[rakudo]]. (It hasn't been added to [[RCRPG]] yet, for this reason)
{{works with|nieczaRakudo|2013.05}}
<lang perl6># TODO: Z~, List.pick, fix xx
my %commands;
my %rooms;
Line 21:
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 40 ⟶ 27:
 
my @random_items = < sledge ladder gold >;
 
my %dir_str =
up => 'in the ceiling',
down => 'in the floor',
map { ($_ => "to the $_") }, <north south east west>;
 
my %dir_vec =
up => [ 0, 0, 1],
Line 51 ⟶ 40:
east => [ 1, 0, 0],
west => [-1, 0, 0];
 
my %dir_rev = map { ($^a => $^b), $^b => $^a },
< north south east west up down >;
 
my class Room {
has $.stuff = (my $roll = 1.rand) > .25 ?? [@random_items.roll] !!
my $next = 0;
$roll > .85 ?? [@random_items.roll(2)] !! Nil;
has $.stuff = [@random_items[{ $next == $_ && ($next = 0); $next++ }]];
has $.name = "The Unnamed Room";
has $.links = [];
Line 67 ⟶ 57:
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 79 ⟶ 70:
}
 
%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%stuff;
map { $fun(%stuff{$_, shift @cl2)}++ }, $l1@list;
my $items = (map { "{%stuff{$_} > 1 ?? %stuff{$_} !! 'a'} $_" }, %stuff.keys).join: ', ';
$items.subst( /','(<-[,]>+)$ /, -> $/ {" and$0"} );
 
sub move($dir) {
Line 87 ⟶ 84:
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 113 ⟶ 110:
if !(%dir_vec{$dir}:exists) {
say "I don't know that direction. Try: {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 134 ⟶ 133:
say "You're not carrying anything. Ask again later.";
} else {
say "You have {serial_and(map { "a $_" }, @$stuff )}."
}
};
Line 200 ⟶ 199:
%commands{$c2} = %commands{$c1};
}
};
 
command [<exit quit>], "(leave Muddy)", 0,
{
print 'The Muddy walls dim. ';
say "You glance down as {serial_and( @$stuff )} clatter{@$stuff > 1 ?? 's' !! ''} to the floor then fades away." if @$stuff;
say 'It is as if you are awakening from a strange (and extremly low resolution) dream...';
}exit;
};
 
Line 205 ⟶ 212:
%rooms{$room}.greet;
 
forwhile my $*IN.linesin ->= $in*IN.get {
my @args = $in.words;
if @args && %commands{lc @args[0]} -> $fun {
$fun(|@args);
10,333

edits