Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[crypt.pl] allow move syntax specifying disks

So instead of move('left', 'middle'), you can also do
move('tiny disk', 'middle), and it works it out for you.
  • Loading branch information...
commit e2a3e5d9f70c5c21ce432a3c2ea03b85c83cf366 1 parent 66e182d
Carl Mäsak authored July 01, 2012

Showing 1 changed file with 24 additions and 4 deletions. Show diff stats Hide diff stats

  1. 28  crypt.pl
28  crypt.pl
@@ -60,12 +60,24 @@
60 60
 
61 61
     has $!achievement = 'locked';
62 62
 
63  
-    method move($source, $target) {
64  
-        die X::Hanoi::NoSuchRod.new(:rod<source>, :name($source))
65  
-            unless %!state.exists($source);
  63
+    method move($source is copy, $target) {
  64
+        my @source_rod;
  65
+        if $source eq any @disks {
  66
+            for %!state -> ( :key($rod), :value(@disks) ) {
  67
+                if $source eq any(@disks) {
  68
+                    @source_rod := @disks;
  69
+                    $source = $rod;
  70
+                    last;
  71
+                }
  72
+            }
  73
+        }
  74
+        else {
  75
+            die X::Hanoi::NoSuchRod.new(:rod<source>, :name($source))
  76
+                unless %!state.exists($source);
  77
+            @source_rod := %!state{$source};
  78
+        }
66 79
         die X::Hanoi::NoSuchRod.new(:rod<target>, :name($target))
67 80
             unless %!state.exists($target);
68  
-        my @source_rod := %!state{$source};
69 81
         die X::Hanoi::RodHasNoDisks.new(:name($source))
70 82
             unless @source_rod;
71 83
         my @target_rod := %!state{$target};
@@ -226,5 +238,13 @@ (&code, $ex_type, $message, &followup?)
226 238
         }
227 239
     }
228 240
 
  241
+    {
  242
+        my $game = HanoiGame.new();
  243
+
  244
+        is $game.move('tiny disk', my $target = 'middle'),
  245
+           DiskMoved.new(:size<tiny>, :source<left>, :$target),
  246
+           'naming source disk instead of the rod (+)';
  247
+    }
  248
+
229 249
     done;
230 250
 }

0 notes on commit e2a3e5d

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