Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 4d11a9df30cf2e3eb9205daa79b3e33b480220ac 1 parent 41501d6
@masak authored
Showing with 25 additions and 0 deletions.
  1. +25 −0 crypt.pl
View
25 crypt.pl
@@ -98,6 +98,14 @@
}
}
+class X::Hanoi::DiskAlreadyOnARod is Exception {
+ has $.disk;
+
+ method message($_:) {
+ "Cannot add the {.disk} because it is already on a rod"
+ }
+}
+
class Hanoi::Game {
my @disks = map { "$_ disk" }, <tiny small medium large huge>;
my %size_of = @disks Z 1..5;
@@ -159,6 +167,8 @@
method add($disk, $target) {
die X::Hanoi::NoSuchDisk.new(:$disk)
unless $disk eq any(@disks);
+ die X::Hanoi::DiskAlreadyOnARod.new(:$disk)
+ if grep { $disk eq any(@$_) }, %!state.values;
my $size = $disk.words[0];
my @events = Hanoi::DiskAdded.new(:$size, :$target);
self!apply($_) for @events;
@@ -198,6 +208,10 @@
my @source_rod := %!state{.source};
@source_rod.pop;
}
+ when Hanoi::DiskAdded {
+ my @target_rod := %!state{.target};
+ @target_rod.push("{.size} disk");
+ }
}
}
@@ -454,6 +468,17 @@ (&code, $ex_type, $message, &followup?)
'Cannot add a humongous disk because there is no such disk',
'.message attribute';
};
+
+ throws_exception
+ { $game.add('tiny disk', 'right') },
+ X::Hanoi::DiskAlreadyOnARod,
+ 'adding a disk (-) the disk is already on a rod',
+ {
+ is .disk, 'tiny disk', '.disk attribute';
+ is .message,
+ 'Cannot add the tiny disk because it is already on a rod',
+ '.message attribute';
+ };
}
done;
Please sign in to comment.
Something went wrong with that request. Please try again.