Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[crypt.pl] size -> disk refactor

Always pass the full name of the disk in events, not just the size.
  • Loading branch information...
commit 0f569fa20ecf7c37aa9250487bfded1e4fde1dcb 1 parent 4d11a9d
Carl Mäsak authored July 01, 2012

Showing 1 changed file with 21 additions and 19 deletions. Show diff stats Hide diff stats

  1. 40  crypt.pl
40  crypt.pl
@@ -11,7 +11,7 @@
11 11
 }
12 12
 
13 13
 class Hanoi::DiskMoved does Event {
14  
-    has $.size;
  14
+    has $.disk;
15 15
     has $.source;
16 16
     has $.target;
17 17
 }
@@ -23,12 +23,12 @@
23 23
 }
24 24
 
25 25
 class Hanoi::DiskRemoved does Event {
26  
-    has $.size;
  26
+    has $.disk;
27 27
     has $.source;
28 28
 }
29 29
 
30 30
 class Hanoi::DiskAdded does Event {
31  
-    has $.size;
  31
+    has $.disk;
32 32
     has $.target;
33 33
 }
34 34
 
@@ -140,14 +140,14 @@
140 140
                 );
141 141
             }
142 142
         }
143  
-        my $size = $moved_disk.words[0];
144  
-        my @events = Hanoi::DiskMoved.new(:$size, :$source, :$target);
  143
+        my @events
  144
+            = Hanoi::DiskMoved.new(:disk($moved_disk), :$source, :$target);
145 145
         if %!state<right> == @disks-1
146 146
            && $target eq 'right'
147 147
            && $!achievement eq 'locked' {
148 148
             @events.push(Hanoi::AchievementUnlocked.new);
149 149
         }
150  
-        if $size eq 'small' && $!achievement eq 'unlocked' {
  150
+        if $moved_disk eq 'small disk' && $!achievement eq 'unlocked' {
151 151
             @events.push(Hanoi::AchievementLocked.new);
152 152
         }
153 153
         self!apply($_) for @events;
@@ -156,10 +156,9 @@
156 156
 
157 157
     method remove($disk) {
158 158
         my $source = self!rod_with_disk($disk, 'remove');
159  
-        my $size = $disk.words[0];
160 159
         die X::Hanoi::ForbiddenDiskRemoval.new(:$disk)
161  
-            unless $size eq 'tiny';
162  
-        my @events = Hanoi::DiskRemoved.new(:$size, :$source);
  160
+            unless $disk eq 'tiny disk';
  161
+        my @events = Hanoi::DiskRemoved.new(:$disk, :$source);
163 162
         self!apply($_) for @events;
164 163
         return @events;
165 164
     }
@@ -169,8 +168,7 @@
169 168
             unless $disk eq any(@disks);
170 169
         die X::Hanoi::DiskAlreadyOnARod.new(:$disk)
171 170
             if grep { $disk eq any(@$_) }, %!state.values;
172  
-        my $size = $disk.words[0];
173  
-        my @events = Hanoi::DiskAdded.new(:$size, :$target);
  171
+        my @events = Hanoi::DiskAdded.new(:$disk, :$target);
174 172
         self!apply($_) for @events;
175 173
         return @events;
176 174
     }
@@ -210,7 +208,7 @@
210 208
         }
211 209
         when Hanoi::DiskAdded {
212 210
             my @target_rod := %!state{.target};
213  
-            @target_rod.push("{.size} disk");
  211
+            @target_rod.push(.disk);
214 212
         }
215 213
     }
216 214
 }
@@ -243,7 +241,11 @@ (&code, $ex_type, $message, &followup?)
243 241
         my $game = Hanoi::Game.new();
244 242
 
245 243
         is $game.move('left', 'middle'),
246  
-           Hanoi::DiskMoved.new(:size<tiny>, :source<left>, :target<middle>),
  244
+           Hanoi::DiskMoved.new(
  245
+                :disk('tiny disk'),
  246
+                :source<left>,
  247
+                :target<middle>
  248
+           ),
247 249
            'moving a disk (+)';
248 250
 
249 251
         throws_exception
@@ -328,20 +330,20 @@ (&code, $ex_type, $message, &followup?)
328 330
         {
329 331
             my ($source, $, $target) = @last_move;
330 332
             is $game.move($source, $target), (
331  
-                Hanoi::DiskMoved.new(:size<tiny>, :$source, :$target),
  333
+                Hanoi::DiskMoved.new(:disk('tiny disk'), :$source, :$target),
332 334
                 Hanoi::AchievementUnlocked.new(),
333 335
             ), 'putting all disks on the right rod unlocks achievement';
334 336
 
335 337
             $game.move($target, $source);
336 338
             is $game.move($source, $target), (
337  
-                Hanoi::DiskMoved.new(:size<tiny>, :$source, :$target),
  339
+                Hanoi::DiskMoved.new(:disk('tiny disk'), :$source, :$target),
338 340
             ), 'moving things back and forth does not unlock achievement again';
339 341
         }
340 342
 
341 343
         {
342 344
             $game.move('right', 'middle');
343 345
             is $game.move(my $source = 'right', my $target = 'left'), (
344  
-                Hanoi::DiskMoved.new(:size<small>, :$source, :$target),
  346
+                Hanoi::DiskMoved.new(:disk('small disk'), :$source, :$target),
345 347
                 Hanoi::AchievementLocked.new(),
346 348
             ), 'removing two disks from the right rod locks achievement';
347 349
         }
@@ -351,7 +353,7 @@ (&code, $ex_type, $message, &followup?)
351 353
         my $game = Hanoi::Game.new();
352 354
 
353 355
         is $game.move('tiny disk', my $target = 'middle'),
354  
-           Hanoi::DiskMoved.new(:size<tiny>, :source<left>, :$target),
  356
+           Hanoi::DiskMoved.new(:disk('tiny disk'), :source<left>, :$target),
355 357
            'naming source disk instead of the rod (+)';
356 358
     }
357 359
 
@@ -391,7 +393,7 @@ (&code, $ex_type, $message, &followup?)
391 393
         my $game = Hanoi::Game.new();
392 394
 
393 395
         is $game.remove('tiny disk'),
394  
-           Hanoi::DiskRemoved.new(:size<tiny>, :source<left>),
  396
+           Hanoi::DiskRemoved.new(:disk('tiny disk'), :source<left>),
395 397
            'removing a disk (+)';
396 398
 
397 399
         throws_exception
@@ -455,7 +457,7 @@ (&code, $ex_type, $message, &followup?)
455 457
             };
456 458
 
457 459
         is $game.add('tiny disk', 'left'),
458  
-           Hanoi::DiskAdded.new(:size<tiny>, :target<left>),
  460
+           Hanoi::DiskAdded.new(:disk('tiny disk'), :target<left>),
459 461
            'adding a disk (+)';
460 462
 
461 463
         throws_exception

0 notes on commit 0f569fa

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