Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[crypt.pl] cannot add a disk that's already there

  • Loading branch information...
commit 4d11a9df30cf2e3eb9205daa79b3e33b480220ac 1 parent 41501d6
Carl Mäsak authored July 01, 2012

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

  1. 25  crypt.pl
25  crypt.pl
@@ -98,6 +98,14 @@
98 98
     }
99 99
 }
100 100
 
  101
+class X::Hanoi::DiskAlreadyOnARod is Exception {
  102
+    has $.disk;
  103
+
  104
+    method message($_:) {
  105
+        "Cannot add the {.disk} because it is already on a rod"
  106
+    }
  107
+}
  108
+
101 109
 class Hanoi::Game {
102 110
     my @disks = map { "$_ disk" }, <tiny small medium large huge>;
103 111
     my %size_of = @disks Z 1..5;
@@ -159,6 +167,8 @@
159 167
     method add($disk, $target) {
160 168
         die X::Hanoi::NoSuchDisk.new(:$disk)
161 169
             unless $disk eq any(@disks);
  170
+        die X::Hanoi::DiskAlreadyOnARod.new(:$disk)
  171
+            if grep { $disk eq any(@$_) }, %!state.values;
162 172
         my $size = $disk.words[0];
163 173
         my @events = Hanoi::DiskAdded.new(:$size, :$target);
164 174
         self!apply($_) for @events;
@@ -198,6 +208,10 @@
198 208
             my @source_rod := %!state{.source};
199 209
             @source_rod.pop;
200 210
         }
  211
+        when Hanoi::DiskAdded {
  212
+            my @target_rod := %!state{.target};
  213
+            @target_rod.push("{.size} disk");
  214
+        }
201 215
     }
202 216
 }
203 217
 
@@ -454,6 +468,17 @@ (&code, $ex_type, $message, &followup?)
454 468
                     'Cannot add a humongous disk because there is no such disk',
455 469
                     '.message attribute';
456 470
             };
  471
+
  472
+        throws_exception
  473
+            { $game.add('tiny disk', 'right') },
  474
+            X::Hanoi::DiskAlreadyOnARod,
  475
+            'adding a disk (-) the disk is already on a rod',
  476
+            {
  477
+                is .disk, 'tiny disk', '.disk attribute';
  478
+                is .message,
  479
+                    'Cannot add the tiny disk because it is already on a rod',
  480
+                    '.message attribute';
  481
+            };
457 482
     }
458 483
 
459 484
     done;

0 notes on commit 4d11a9d

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