Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e0c0275032
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1138 lines (956 sloc) 30.556 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
## Predeclarations

role Thing { ... }

role Container { ... }
role Darkness { ... }
role Heavy { ... }
role Implicit { ... }
role Openable { ... }
role Platform { ... }
role Readable { ... }
role Showable { ... }
role Takable { ... }

class Basket { ... }
class Brook { ... }
class Bushes { ... }
class Butterfly { ... }
class Car { ... }
class Disk { ... }
class Doom { ... }
class Door { ... }
class Fire { ... }
class Flashlight { ... }
class Grass { ... }
class Helmet { ... }
class Inventory { ... }
class Leaves { ... }
class Pedestal { ... }
class Rope { ... }
class Sarcophagi { ... }
class Sign { ... }
class Trees { ... }
class Walls { ... }
class Water { ... }

role Room { ... }

class Cave { ... }
class Crypt { ... }
class Hall { ... }
class Hill { ... }

## Global variables

my %descriptions;
for slurp("descriptions").split(/\n\n/) {
    /^^ '== ' (\N+) \n (.*)/
        or die "Could not parse 'descriptions' file: $_";
    %descriptions{$0} = ~$1;
}

my $room;
my $inventory = Inventory.new();
my $used_abbreviated_syntax = False;

my %things =
    car => Car.new(:name<car>, :contents<flashlight rope>,
                          :herephrase("Your %s is parked here.")),
    flashlight => Flashlight.new(:name<flashlight>),
    rope => Rope.new(:name<rope>),
    grass => Grass.new(:name<grass>),
    bushes => Bushes.new(:name<bushes>),
    door => Door.new(:name<door>),
    trees => Trees.new(:name<trees>),
    leaves => Leaves.new(:name<leaves>,
                    :containphrase("69,105 %s.")),
    brook => Brook.new(:name<brook>,
                    :herephrase("A small brook runs through the forest.")),
    water => Water.new(:name<water>, :containphrase("Some %s.")),
    sign => Sign.new(:name<sign>),
    basket => Basket.new(:name<basket>),
    "tiny disk" => Disk.new(:name("tiny disk")),
    "small disk" => Disk.new(:name("small disk")),
    "medium disk" => Disk.new(:name("medium disk")),
    "large disk" => Disk.new(:name("large disk")),
    "huge disk" => Disk.new(:name("huge disk")),
    fire => Fire.new(:name<fire>),
    helmet => Helmet.new(:name<helmet>),
    pedestal => Pedestal.new(:name<pedestal>, :supports<butterfly>),
    butterfly => Butterfly.new(:name<butterfly>),
    doom => Doom.new(),
    sarcophagi => Sarcophagi.new(:name<sarcophagi>),
    walls => Walls.new(:name<walls>),
;

my %rooms =
    clearing => Room.new( :name<clearing>, :contents<car> ),
    hill => Hill.new( :name<hill>,
                          :contents<door trees leaves grass bushes brook
                                    water>,
                          :in<south> ),
    chamber => Room.new( :name(<chamber>), :contents<sign basket walls>,
                          :out<north> ),
    hall => Hall.new( :name(<hall>),
                          :contents(<helmet walls>, map { "$_ disk" },
                                    <tiny small medium large huge>)),
    cave => Cave.new( :name(<cave>), :contents<fire walls> ),
    crypt => Crypt.new( :name(<crypt>), :contents<pedestal walls> ),
;
%things.push(%rooms);

%rooms<clearing>.connect('east', %rooms<hill>);
%rooms<cave>.connect('northwest', %rooms<crypt>);

my @base_verbs = <examine open close take drop read go use put>;
my %verb_synonyms =
    "x" => "examine",
    "look" => "examine",
    "pick" => "take",
    "pick up" => "take",
    "get" => "take",
    "retrieve" => "take",
    "retreive" => "take", # might as well
    "turn on" => "use",
    "switch on" => "use",
;
my @verbs = @base_verbs, %verb_synonyms.keys;

## Utility subroutines

sub exclude(@l, $e) { grep { $_ !=== $e }, @l }

sub inverse_index(@array, $value) {
    my $index = @array.keys.first({ @array[$_] eq $value });
    return $index;
}

sub current_container_of(Str $name) {
    return $room if $name eq $room.name.lc;
    return $room if $name eq any $room.contents;
    return $inventory if $name eq any $inventory.contents;
    for %things{$room.contents, $inventory.contents} -> $thing {
        return $thing if $name eq any $thing.?contents;
        return $thing if $name eq any $thing.?supports;
    }
    return Nil;
}

sub room_contains(Str $name) {
    return current_container_of($name).?name eq any $room.name, $room.contents;
}

sub inventory_contains(Str $name) {
    return True if $name eq any $inventory.contents;
    return True if $name eq any map { .contents.flat },
                                grep { player_can_see_inside($_) },
                                %things{$inventory.contents};
    return False;
}

sub player_can_see(Thing $thing) {
    my $thing_is_visible = $thing !~~ Showable || $thing.is_visible;

    return False unless $thing_is_visible;
    return False unless room_contains($thing.name)
                        || inventory_contains($thing.name);

    return True;
}

sub player_can_see_inside(Thing $thing) {
    my $thing_is_open = $thing ~~ Container
                        && ($thing !~~ Openable || $thing.is_open);

    return False unless $thing_is_open;
    return False unless player_can_see($thing);

    return True;
}

sub there_is_light() {
    my $there_is_sun = $room !~~ Darkness;
    return True if $there_is_sun;

    my $flashlight = %things<flashlight>;
    my $flashlight_is_here = player_can_see($flashlight);
    return True if $flashlight_is_here && $flashlight.is_on;

    my $fire = %things<fire>;
    my $fire_is_here = player_can_see($fire);
    return True if $fire_is_here;

    return False;
}

## Roles for things and rooms

role Thing {
    has Str $.name;
    has Str $!description = %descriptions{$.name};
    has Str $.herephrase;
    has Str $.containphrase;

    method examine {
        if there_is_light() {
            say $!description;
            self.?on_examine;
        }
        else {
            say "You can't see anything, because it's pitch black.";
        }
    }
}

role Container does Thing {
    has Thing @.contents is rw;

    method add(Str $name) {
        @.contents.push($name);
    }

    method remove(Str $name) {
        @.contents = exclude(@.contents, $name);
    }

    method list_contents($herephrase) {
        for %things{@.contents} -> Thing $thing {
            if player_can_see($thing) {
                next if $thing ~~ Implicit;
                say sprintf $thing.herephrase // $herephrase, $thing.name;
                if player_can_see_inside($thing) && $thing.contents {
                    say "The $thing.name() contains:";
                    $thing.list_container_contents("A %s.");
                }
                if $thing ~~ Platform && $thing.supports {
                    say "On the $thing.name() you see:";
                    $thing.list_platform_supports("A %s.");
                }
            }
        }
    }

    method list_container_contents($containphrase, $indent = 1) {
        for %things{@.contents} -> Thing $thing {
            say ' ' x $indent,
                sprintf $thing.containphrase // $containphrase, $thing.name;
            if player_can_see_inside($thing) && $thing.contents {
                say ' ' x $indent, "The $thing.name() contains:";
                $thing.list_container_contents("A %s.", $indent + 1);
            }
        }
    }

    method on_open {
        if @.contents {
            say "Opening the $.name reveals a {join " and a ", @.contents}.";
        }
    }
}

role Darkness does Room {
}

role Heavy does Thing {
    method on_remove_from($_) {
        when Platform {
            unless grep Heavy, %things<pedestal>.supports {
                say "An alarm starts sounding in the whole cavern.";
                %things<doom>.activate;
            }
        }
    }

    method on_put($_) {
        when Platform {
            say "The alarm stops.";
            %things<doom>.inactivate;
        }
    }
}

role Implicit does Thing {
}

role Openable does Thing {
    has Bool $.is_open;

    method open {
        if $.is_open {
            say "The $.name is open.";
            return;
        }
        say "You open the $.name.";
        $!is_open = True;
        self.?on_open;
    }

    method close {
        unless $.is_open {
            say "The $.name is closed.";
            return;
        }
        say "You close the $.name.";
        $!is_open = False;
        self.?on_close;
    }
}

role Platform does Thing {
    has Thing @.supports is rw;

    method add(Str $name) {
        @.supports.push($name);
    }

    method remove(Str $name) {
        @.supports = exclude(@.supports, $name);
    }

    method list_platform_supports($containphrase, $indent = 1) {
        for %things{@.supports} -> Thing $thing {
            say ' ' x $indent,
                sprintf $thing.containphrase // $containphrase, $thing.name;
            if player_can_see_inside($thing) && $thing.contents {
                say ' ' x $indent, "The $thing.name() contains:";
                $thing.list_container_contents("A %s.", $indent + 1);
            }
        }
    }
}

role Readable does Thing {
    method read {
        self.examine;
    }
}

role Showable does Thing {
    has Bool $.is_visible = False;

    method show {
        unless $.is_visible {
            $!is_visible = True;
            self.?on_show;
        }
    }
}

role Takable does Thing {
    method put($new_receiver) {
        my $old_receiver = current_container_of($.name);
        $old_receiver.remove($.name);
        self.?on_remove_from($old_receiver);

        $new_receiver.add($.name);
        self.?on_put($new_receiver);
    }

    method take {
        if inventory_contains($.name) {
            say "You are already holding the $.name";
            return;
        }
        say "You take the $.name.";
        self.put($inventory);
    }

    method drop {
        unless inventory_contains($.name) {
            say "You are not holding the $.name";
            return;
        }
        say "You drop the $.name on the ground.";
        self.put($room);
    }
}

## Things

class Basket does Container {
}

class Brook does Container {
}

class Bushes does Implicit {
    method on_examine {
        %things<door>.show;
    }
}

class Butterfly does Takable does Heavy {
}

class Car does Openable does Container {
    method go {
        say "You get in the car, but then remember that you haven't found";
        say "the treasure yet, so you get out again.";
    }
}

class Disk does Takable does Heavy {
}

class Doom {
    has Bool $.activated = False;
    has Int $!time_left;

    method activate {
        $!activated = True;
        $!time_left = 4;
    }

    method inactivate {
        $!activated = False;
    }

    method tick {
        if $!activated {
            $!time_left--;
            unless $!time_left {
                say "The alarm starts sounding louder.";
                say "The whole cavern shakes, and falls in on itself.";
                say "You die.";
                last;
            }
        }
    }
}

class Door does Showable does Openable {
    method on_show {
        say "You discover a door in the hill, under the thick grass!";
    }

    method on_open {
        say "You can see into the hill now!";
        %rooms<hill>.connect('south', %rooms<chamber>);
    }

    method on_close {
        %rooms<hill>.disconnect('south');
    }
}

class Fire does Container {
}

class Flashlight does Takable {
    has Bool $.is_on = False;

    method use {
        if $.is_on {
            say "It's already switched on.";
        }
        my $was_dark = !there_is_light;
        $!is_on = True;
        say "You switch on the flashlight.";
        if $was_dark {
            say "";
            $room.look;
        }
    }

    method examine {
        self.Thing::examine;
        say "";
        say "The $.name is switched {$.is_on ?? "on" !! "off"}.";
    }
}

class Grass does Implicit {
    method on_examine {
        %things<door>.show;
    }
}

class Helmet does Container does Takable {
    method on_remove_from(Container $_) {
        when Brook {
            %things<water>.put(self);
        }
    }
}

class Inventory does Container {
}

class Leaves does Implicit does Takable {
    method on_put(Container $_) {
        when Car {
            say "Great. Now your car is full of leaves.";
        }
        when Basket {
            say "The ground rumbles and shakes a bit.";
            say "A passageway opens up to the south, into the caverns.";
            %rooms<chamber>.connect('south', %rooms<hall>);
        }
        when Fire {
            say "The leaves burn up within seconds.";
        }
    }
}

class Pedestal does Platform {
}

class Rope does Takable {
}

class Sarcophagi does Implicit {
}

class Sign does Readable {
}

class Trees does Implicit {
}

class Walls does Implicit does Readable {
    method examine {
        say %descriptions{"walls:$room.name()"}.lines.pick;
        self.?on_examine;
    }
}

class Water does Implicit does Takable {
    method on_put($_) {
        when Inventory {
            say "Your bare hands aren't very good at carrying water.";
            self.drop;
        }
        when Fire {
            say "The fire wanes and dies.";
            $room.remove("fire");
        }
    }
}

## Directions

my @directions = <
    north south east west
    northeast northwest southeast southwest
    up down in out
>;

my %abbr_directions = <
    n north
    s south
    e east
    w west
    ne northeast
    nw northwest
    se southeast
    sw southwest
    u up
    d down
>;

# RAKUDO: Have to repeat the list here because of a scoping bug [perl #95500]
subset Direction of Str where any(<
    north south east west
    northeast northwest southeast southwest
    up down in out
>);

sub opposite_direction(Direction $d) {
    my %opposites =
        'north' => 'south',
        'east' => 'west',
        'northeast' => 'southwest',
        'northwest' => 'southeast',
        'up' => 'down',
        'in' => 'out',
    ;

    %opposites.push( %opposites.invert );

    %opposites{$d};
}

## Rooms

role Room does Container {
    has Direction %.exits is rw;
    has Direction $.in;
    has Direction $.out;
    has Bool $!visited = False;

    method connect(Direction $direction, Room $other_room) {
        my $opposite = opposite_direction($direction);
        self.exits{$direction} = $other_room;
        $other_room.exits{$opposite} = self;
    }

    method disconnect(Direction $direction) {
        my $opposite = opposite_direction($direction);
        my $other_room = self.exits.delete($direction);
        if $other_room {
            $other_room.exits.delete($opposite);
        }
    }

    method list_exits {
        given %.exits {
            when 0 {
                say "There are no obvious exits from here.";
            }
            when 1 {
                say "You can go {.keys}.";
            }
            when 2 {
                say "You can go {.keys.join(" and ")}.";
            }
            default {
                say "You can go {.keys[0..*-2].join(", ")} and {.keys[*-1]}.";
            }
        }
    }

    method look {
        if there_is_light() {
            self.examine;
            self.list_contents("There is a %s here.");
            self.list_exits;
        }
        else {
            say "It is pitch black.";
        }
    }

    method enter {
        say $!name.ucfirst;
        $room = self;

        unless $!visited {
            say "";
            self.look;
        }

        $!visited = True;
        self.?on_enter;
        if %things<doom>.activated {
            say "An alarm is sounding.";
        }
    }
}

class Hill does Room {
    method on_enter {
        if inventory_contains 'butterfly' {
            say "Congratulations! You found the treasure and got out with it ",
                "alive!";
            last;
        }
    }
}

class Cave does Room does Darkness {
    method on_try_exit($direction) {
        if $direction eq "northwest" && player_can_see(%things<fire>) {
            say "You try to walk past the fire, but it's too hot!";
            return False;
        }
        return True;
    }
}

class Crypt does Room does Darkness {
}

class Hall does Room does Darkness {
    has @!disks =
        [5, 4, 3, 2, 1],
        [],
        [],
    ;
    has $!moves_made = 0;
    my @sizes = <. tiny small medium large huge>;
    my @rods = <left middle right>;

    method list_contents($herephrase) {
        say "There are rods stuck in the floor with disks on them, ",
            "like this:";
        self.show_disks;
        say "";
        for %things{@.contents} -> Thing $thing {
            next if $thing ~~ Disk;
            next if $thing ~~ Implicit;
            if player_can_see($thing) {
                say sprintf $thing.herephrase // $herephrase, $thing.name;
            }
        }
    }

    method show_disks {
        say "";
        my $indent = " ";
        my @c =
            " | ",
            " === ",
            " ===== ",
            " ======= ",
            " ========= ",
            "===========",
        ;
        for reverse 0..5 -> $row {
            say $indent, join " ", map { @c[@!disks[$_][$row] // 0] }, 0..2;
        }
        say $indent, "-" x 37;
        say $indent, join " ", map { " $_ " }, <A B C>;
    }

    method take_disk(Str $adjective) {
        my $size = inverse_index(@sizes, $adjective);
        my $old_rod = first { $size == any @!disks[$_].list }, 0..2;

        if defined $old_rod {
            if @!disks[$old_rod][*-1] != $size {
                say "You can't take the $adjective disk, because it is under ",
                    (join " and ", map { "the @sizes[$_] disk" },
                     grep { $_ < $size }, @!disks[$old_rod].list), ".";
                return;
            }
        }
        unless $adjective eq "tiny" {
            say "The $adjective disk is too heavy to carry!";
            return;
        }

        %things{"$adjective disk"}.take;

        if defined $old_rod {
            pop @!disks[$old_rod];
            self.?on_move_disk($old_rod);
        }
    }

    method move_disk_to_rod(Str $adjective, Str $position) {
        my $size = inverse_index(@sizes, $adjective);
        my $old_rod = first { $size == any @!disks[$_].list }, 0..2;

        if defined $old_rod && @!disks[$old_rod][*-1] != $size {
            say "You can't take the $adjective disk, because it is under ",
                (join " and ", map { "the @sizes[$_] disk" },
                 grep { $_ < $size }, @!disks[$old_rod].list), ".";
            return;
        }

        my $new_rod = inverse_index(@rods, $position);
        if @!disks[$new_rod] {
            if @!disks[$new_rod][*-1] == $size {
                say "The $adjective disk is already on the $position rod.";
                return;
            }
            elsif @!disks[$new_rod][*-1] < $size {
                say "A sense of dread fills you as you attempt to put a ",
                    "bigger disk on a smaller one.";
                return;
            }
        }

        if defined $old_rod {
            pop @!disks[$old_rod];
        }

        %things{"$adjective disk"}.put($room);
        push @!disks[$new_rod], $size;

        say "You put the $adjective disk on the $position rod.";
        self.show_disks;

        if defined $old_rod {
            $!moves_made++;
            self.suggest_short_syntax($old_rod, $new_rod);
        }

        self.?on_move_disk($old_rod);
    }

    method move_rod_to_rod(Int $old_rod, Int $new_rod) {
        unless @!disks[$old_rod] {
            say "The {@rods[$old_rod]} rod is empty.";
            return;
        }

        my $size = @!disks[$old_rod][*-1];
        if @!disks[$new_rod] {
            if @!disks[$new_rod][*-1] < $size {
                say "A sense of dread fills you as you attempt to put a ",
                    "bigger disk on a smaller one.";
                return;
            }
        }

        pop @!disks[$old_rod];
        push @!disks[$new_rod], $size;

        my $adjective = @sizes[$size];
        my $position = @rods[$new_rod];

        say "You put the $adjective disk on the $position rod.";
        self.show_disks;

        $!moves_made++;
        self.suggest_short_syntax($old_rod, $new_rod);
        self.?on_move_disk($old_rod);
    }

    method suggest_short_syntax($old_rod, $new_rod) {
        if 3 <= $!moves_made < 5 && !$used_abbreviated_syntax {
            my $abbr = chr(ord("A") + $old_rod) ~ chr(ord("A") + $new_rod);
            say "(You can also write this move as $abbr)";
        }
    }

    method on_move_disk($old_rod) {
        sub hole_is_revealed { %rooms<hall>.exits.exists("down") }

        if @!disks[2] == 5 {
            say "The whole floor tips, and reveals a hole beneath the wall.";
            %rooms<hall>.connect('down', %rooms<cave>);
        }

        if defined $old_rod && $old_rod == 2 && @!disks[2] == 3
           && hole_is_revealed() {

            say "The whole floor tips back, hiding the hole again.";
            %rooms<hall>.disconnect('down');
        }
    }
}

## The game itself

say "CRYPT";
say "=====";
say "";

say "You've heard there's supposed to be an ancient hidden crypt in these";
say "woods. One containing a priceless treasure. Well, there's only one way";
say "to find out...";
say "";

%rooms<clearing>.enter;

loop {
    say "";
    my $command = prompt("> ");

    given $command {
        when !.defined || .lc eq "q" | "quit" {
            say "";
            my $really = prompt "Really quit (y/N)? ";
            if !defined $really || "y"|"yes" eq lc $really {
                last;
            }
        }

        $command .= trim;
        $command .= lc;

        when "" {
            succeed;
        }

        when /^help>>/|"h"|"?" {
            say "Here are some (made-up) examples of commands you can use:";
            say "";
            say "look (l) | ",
                "take banana";
            say "examine banana (x banana) | ",
                "drop banana";
            say "[go] north/south/east/west (n/s/e/w) | ",
                "put banana in bag";
            say "open bag | ",
                "close bag";
        }

        when /^ :s go (\w+) $/
             && $0 eq any @directions, %abbr_directions.keys, <in out> {

            $command = ~$0;
            proceed;
        }

        when any(%abbr_directions.keys) {
            $command = %abbr_directions{$command};
            proceed;
        }

        when 'in' {
            if $room.in -> $real_direction {
                $command = $real_direction;
            }
            proceed;
        }

        when 'out' {
            if $room.out -> $real_direction {
                $command = $real_direction;
            }
            proceed;
        }

        when Direction {
            my $direction = $command;
            if $room.exits{$direction} -> $new_room {
                my $succeeded = $room.?on_try_exit($direction) // True;
                if $succeeded {
                    $new_room.enter;
                }
                %things<doom>.tick;
            }
            else {
                say "Sorry, you can't go $direction from here.";
            }
        }

        when "look"|"l" {
            $room.look;
        }

        when /^ :s look in $<noun>=[\w+] $/ {
            my $thing = %things{$<noun>};

            unless $thing {
                say "I am unfamiliar with the noun '$<noun>'.";
                succeed;
            }
            unless player_can_see($thing) {
                say "You see no $<noun> here.";
                succeed;
            }

            unless player_can_see_inside($thing) {
                say "You can't see inside the $<noun>.";
                succeed;
            }

            say "The $thing.name() contains:";
            $thing.list_container_contents("A %s.");
        }

        when "inventory"|"i" {
            if $inventory.contents {
                say "You are carrying:";
                $inventory.list_container_contents("A %s.");
            }
            else {
                say "You are empty-handed.";
            }
        }

        when /^ :s $<verb>=[\w+[ \w+]?] <?{ $<verb> eq any(@verbs) }> $/ {
            say "What do you want to $<verb>?";
        }

        # RAKUDO: Due to [perl #95504], we have to do the checking like
        # this instead of just $<verb>=@verbs
        when /^ :s $<verb>=[\w+[ \w+]?] <?{ $<verb> eq any(@verbs) }>
                [the]? $<noun>=[\w+] $/ {

            my $verb = $<verb>;
            if %verb_synonyms{$verb} -> $synonym {
                $verb = $synonym;
            }

            my $thing = %things{$<noun>};
            unless $thing {
                say "I am unfamiliar with the noun '$<noun>'.";
                succeed;
            }
            unless player_can_see($thing) {
                say "You see no $<noun> here.";
                succeed;
            }

            unless $thing.can($verb) {
                say "You can't $<verb> the $<noun>.";
                succeed;
            }

            $thing."$verb"();
            %things<doom>.tick;
        }

        # RAKUDO: Due to [perl #95504], we have to do the checking like
        # this instead of just $<verb>=@verbs
        when /^ :s $<verb>=[\w+[ \w+]?] <?{ $<verb> eq any(@verbs) }>
                [the]? $<noun1>=[\w+] $<prep>=[in||on]
                [the]? $<noun2>=[\w+] $/ {

            my $verb = $<verb>;
            if %verb_synonyms{$verb} -> $synonym {
                $verb = $synonym;
            }

            unless $verb eq 'put' {
                say "Sorry, I did not understand that.";
                say "Type 'help' for suggestions.";
                succeed;
            }

            if $<noun1> eq "disk" && $room ~~ Hall {
                say "Which disk do you mean; the tiny disk, the small disk, ",
                    "the medium disk,";
                say "the large disk, or the huge disk?";
                succeed;
            }

            my $noun1 = $<noun1>;
            if $<noun1> eq "disk" && $room ~~ Crypt {
                $noun1 = "tiny disk";
            }

            my $thing = %things{$noun1};
            unless $thing {
                say "I am unfamiliar with the noun '$noun1'.";
                succeed;
            }
            unless player_can_see($thing) {
                say "You see no $<noun1> here.";
                succeed;
            }

            my $receiver = %things{$<noun2>};
            unless $receiver {
                say "I am unfamiliar with the noun '$<noun2>'.";
                succeed;
            }
            unless player_can_see($receiver) {
                say "You see no $<noun2> here.";
                succeed;
            }

            unless $thing ~~ Takable {
                say "You can't move the $noun1.";
                succeed;
            }

            if $<prep> eq "in" {
                unless $receiver ~~ Container {
                    say "You can't put things in the $<noun2>.";
                    succeed;
                }
                unless player_can_see_inside($receiver) {
                    $receiver.open;
                }
                say "You put the $noun1 in the $<noun2>.";
            }
            elsif $<prep> eq "on" {
                unless $receiver ~~ Platform {
                    say "You can't put things on the $<noun2>.";
                    succeed;
                }
                say "You put the $noun1 on the $<noun2>.";
            }
            $thing.put($receiver);
        }

        when /^ :s [move|put|take] [the]? disk / {
            say "Which disk do you mean; the tiny disk, the small disk, ",
                "the medium disk,";
            say "the large disk, or the huge disk?";
        }

        when /^ :s take [the]?
                $<adjective>=[tiny||small||medium||large||huge] disk / {

            unless player_can_see(%things{"$<adjective> disk"}) {
                say "You see no $<adjective> disk here.";
                succeed;
            }
            if $room ~~ Hall {
                $room.take_disk(~$<adjective>);
            }
            else {
                %things{"$<adjective> disk"}.take;
            }
        }

        when /^ :s [move|put] [the]?
                $<adjective>=[tiny||small||medium||large||huge]
                disk [on|to] [the]?
                $<position>=[left||middle||right]
                rod $/ {

            unless player_can_see(%things{"$<adjective> disk"}) {
                say "You see no $<adjective> disk here.";
                succeed;
            }
            unless $room ~~ Hall {
                say "You see no rod here.";
                succeed;
            }

            $room.move_disk_to_rod(~$<adjective>, ~$<position>);
        }

        when /^ :s [move|put] [the]?
                $<adjective>=[left||middle||right]
                disk [on|to] [the]?
                $<position>=[left||middle||right]
                rod $/ {

            unless $room ~~ Hall {
                say "You see no rod here.";
                succeed;
            }

            my $old_rod = inverse_index <left middle right>, $<adjective>;
            my $new_rod = inverse_index <left middle right>, $<position>;
            if $old_rod == $new_rod {
                succeed;
            }

            $room.move_rod_to_rod($old_rod, $new_rod);
        }

        when /^ :s (<[abc]>)(<[abc]>) $/ {
            $used_abbreviated_syntax = True;

            unless $room ~~ Hall {
                say "That command only works in the Hall.";
                succeed;
            }

            my $old_rod = inverse_index <a b c>, $0;
            my $new_rod = inverse_index <a b c>, $1;
            if $old_rod == $new_rod {
                succeed;
            }

            $room.move_rod_to_rod($old_rod, $new_rod);
        }

        default {
            say "Sorry, I did not understand that.";
            say "Type 'help' for suggestions.";
        }
    }
}

say "Thanks for playing.";
Something went wrong with that request. Please try again.