Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[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
Showing with 24 additions and 4 deletions.
  1. +24 −4 crypt.pl
28 crypt.pl
View
@@ -60,12 +60,24 @@
has $!achievement = 'locked';
- method move($source, $target) {
- die X::Hanoi::NoSuchRod.new(:rod<source>, :name($source))
- unless %!state.exists($source);
+ method move($source is copy, $target) {
+ my @source_rod;
+ if $source eq any @disks {
+ for %!state -> ( :key($rod), :value(@disks) ) {
+ if $source eq any(@disks) {
+ @source_rod := @disks;
+ $source = $rod;
+ last;
+ }
+ }
+ }
+ else {
+ die X::Hanoi::NoSuchRod.new(:rod<source>, :name($source))
+ unless %!state.exists($source);
+ @source_rod := %!state{$source};
+ }
die X::Hanoi::NoSuchRod.new(:rod<target>, :name($target))
unless %!state.exists($target);
- my @source_rod := %!state{$source};
die X::Hanoi::RodHasNoDisks.new(:name($source))
unless @source_rod;
my @target_rod := %!state{$target};
@@ -226,5 +238,13 @@ (&code, $ex_type, $message, &followup?)
}
}
+ {
+ my $game = HanoiGame.new();
+
+ is $game.move('tiny disk', my $target = 'middle'),
+ DiskMoved.new(:size<tiny>, :source<left>, :$target),
+ 'naming source disk instead of the rod (+)';
+ }
+
done;
}
Please sign in to comment.
Something went wrong with that request. Please try again.