Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[crypt.pl] can remove disks

  • Loading branch information...
commit a47664cab42119a83d7c6469326da4b2152d33d3 1 parent 0a4ebe5
@masak authored
Showing with 31 additions and 0 deletions.
  1. +31 −0 crypt.pl
View
31 crypt.pl
@@ -22,6 +22,11 @@
class Hanoi::AchievementLocked does Event {
}
+class Hanoi::DiskRemoved does Event {
+ has $.size;
+ has $.source;
+}
+
class X::Hanoi::LargerOnSmaller is Exception {
has $.larger;
has $.smaller;
@@ -124,6 +129,20 @@
return @events;
}
+ method remove($disk) {
+ my $size = $disk.words[0];
+ my $source;
+ for %!state -> ( :key($rod), :value(@disks) ) {
+ if $disk eq any(@disks) {
+ $source = $rod;
+ last;
+ }
+ }
+ my @events = Hanoi::DiskRemoved.new(:$size, :$source);
+ self!apply($_) for @events;
+ return @events;
+ }
+
# RAKUDO: private multimethods NYI
method !apply(Event $_) {
when Hanoi::DiskMoved {
@@ -137,6 +156,10 @@
when Hanoi::AchievementLocked {
$!achievement = 'locked';
}
+ when Hanoi::DiskRemoved {
+ my @source_rod := %!state{.source};
+ @source_rod.pop;
+ }
}
}
@@ -298,5 +321,13 @@ (&code, $ex_type, $message, &followup?)
};
}
+ {
+ my $game = Hanoi::Game.new();
+
+ is $game.remove('tiny disk'),
+ Hanoi::DiskRemoved.new(:size<tiny>, :source<left>),
+ 'removing a disk (+)';
+ }
+
done;
}
Please sign in to comment.
Something went wrong with that request. Please try again.