Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] abbr directions in Adventure::Engine

Better from a Postel's Law perspective.
  • Loading branch information...
commit 4b750afb4cfe34dcf1f58cd4ea7fcdb12d0f866e 1 parent ecdc8a1
Carl Mäsak authored July 29, 2012

Showing 1 changed file with 42 additions and 22 deletions. Show diff stats Hide diff stats

  1. 64  bin/crypt
64  bin/crypt
@@ -362,10 +362,11 @@ class X::Adventure is Exception {
362 362
 }
363 363
 
364 364
 class X::Adventure::NoSuchDirection is X::Adventure {
  365
+    has $.action;
365 366
     has $.direction;
366 367
 
367 368
     method message {
368  
-        "Cannot connect rooms because direction '$.direction' does not exist"
  369
+        "Cannot $.action because direction '$.direction' does not exist"
369 370
     }
370 371
 }
371 372
 
@@ -590,6 +591,19 @@ class Adventure::Engine {
590 591
         return @events;
591 592
     }
592 593
 
  594
+    my %abbr_directions = <
  595
+        n  north
  596
+        s  south
  597
+        e  east
  598
+        w  west
  599
+        ne northeast
  600
+        nw northwest
  601
+        se southeast
  602
+        sw southwest
  603
+        u  up
  604
+        d  down
  605
+    >;
  606
+
593 607
     method walk($direction) {
594 608
         die X::Adventure::GameOver.new()
595 609
             if $!game_finished;
@@ -598,7 +612,12 @@ class Adventure::Engine {
598 612
             unless defined $!player_location;
599 613
 
600 614
         my $actual_direction =
601  
-            %!exit_aliases{$!player_location}{$direction} // $direction;
  615
+            %!exit_aliases{$!player_location}{$direction}
  616
+            // %abbr_directions{$direction}
  617
+            // $direction;
  618
+
  619
+        die X::Adventure::NoSuchDirection.new(:action('walk that way'), :$direction)
  620
+            unless $actual_direction eq any(@possible_directions);
602 621
 
603 622
         my $to = %!exits{$!player_location}{$actual_direction};
604 623
 
@@ -1425,19 +1444,7 @@ multi MAIN() {
1425 1444
         north south east west
1426 1445
         northeast northwest southeast southwest
1427 1446
         up down in out
1428  
-    >;
1429  
-
1430  
-    my %abbr_directions = <
1431  
-        n  north
1432  
-        s  south
1433  
-        e  east
1434  
-        w  west
1435  
-        ne northeast
1436  
-        nw northwest
1437  
-        se southeast
1438  
-        sw southwest
1439  
-        u  up
1440  
-        d  down
  1447
+        n s e w ne nw se sw u d
1441 1448
     >;
1442 1449
 
1443 1450
     given 'clearing' {
@@ -1515,13 +1522,9 @@ inventory";
1515 1522
                 }
1516 1523
             }
1517 1524
 
1518  
-            when /^ [''|go \h+|walk \h+] (\w+) <?{ %abbr_directions{$0} }> $/ {
1519  
-                $command = "walk %abbr_directions{$0}";
1520  
-                proceed;
1521  
-            }
1522  
-
1523  
-            when any @possible_directions {
1524  
-                $command = "walk $command";
  1525
+            when /^ [''|go \h+] (\w+)
  1526
+                    <?{ $0 eq any @possible_directions }> $/ {
  1527
+                $command = "walk $0";
1525 1528
                 proceed;
1526 1529
             }
1527 1530
 
@@ -2996,6 +2999,23 @@ multi MAIN('test') {
2996 2999
             'examining a thing (+) in inventory';
2997 3000
     }
2998 3001
 
  3002
+    {
  3003
+        my $game = Crypt::Game.new();
  3004
+
  3005
+        is $game.walk('e'),
  3006
+            [
  3007
+                Adventure::PlayerWalked.new(
  3008
+                    :to<hill>,
  3009
+                ),
  3010
+                Adventure::PlayerLooked.new(
  3011
+                    :room<hill>,
  3012
+                    :exits<west>,
  3013
+                    :things<brook>,
  3014
+                ),
  3015
+            ],
  3016
+            'walking (+) abbreviated directions';
  3017
+    }
  3018
+
2999 3019
     done;
3000 3020
 }
3001 3021
 

0 notes on commit 4b750af

Please sign in to comment.
Something went wrong with that request. Please try again.