Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[crypt.pl] can remove disks

  • Loading branch information...
commit a47664cab42119a83d7c6469326da4b2152d33d3 1 parent 0a4ebe5
Carl Mäsak authored July 01, 2012

Showing 1 changed file with 31 additions and 0 deletions. Show diff stats Hide diff stats

  1. 31  crypt.pl
31  crypt.pl
@@ -22,6 +22,11 @@
22 22
 class Hanoi::AchievementLocked does Event {
23 23
 }
24 24
 
  25
+class Hanoi::DiskRemoved does Event {
  26
+    has $.size;
  27
+    has $.source;
  28
+}
  29
+
25 30
 class X::Hanoi::LargerOnSmaller is Exception {
26 31
     has $.larger;
27 32
     has $.smaller;
@@ -124,6 +129,20 @@
124 129
         return @events;
125 130
     }
126 131
 
  132
+    method remove($disk) {
  133
+        my $size = $disk.words[0];
  134
+        my $source;
  135
+        for %!state -> ( :key($rod), :value(@disks) ) {
  136
+            if $disk eq any(@disks) {
  137
+                $source = $rod;
  138
+                last;
  139
+            }
  140
+        }
  141
+        my @events = Hanoi::DiskRemoved.new(:$size, :$source);
  142
+        self!apply($_) for @events;
  143
+        return @events;
  144
+    }
  145
+
127 146
     # RAKUDO: private multimethods NYI
128 147
     method !apply(Event $_) {
129 148
         when Hanoi::DiskMoved {
@@ -137,6 +156,10 @@
137 156
         when Hanoi::AchievementLocked {
138 157
             $!achievement = 'locked';
139 158
         }
  159
+        when Hanoi::DiskRemoved {
  160
+            my @source_rod := %!state{.source};
  161
+            @source_rod.pop;
  162
+        }
140 163
     }
141 164
 }
142 165
 
@@ -298,5 +321,13 @@ (&code, $ex_type, $message, &followup?)
298 321
             };
299 322
     }
300 323
 
  324
+    {
  325
+        my $game = Hanoi::Game.new();
  326
+
  327
+        is $game.remove('tiny disk'),
  328
+           Hanoi::DiskRemoved.new(:size<tiny>, :source<left>),
  329
+           'removing a disk (+)';
  330
+    }
  331
+
301 332
     done;
302 333
 }

0 notes on commit a47664c

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