Skip to content

Commit

Permalink
[crypt.pl] refactor - private apply method
Browse files Browse the repository at this point in the history
This separates out the state change into a single private method.
(Or several multimethods when Rakudo does private multimethods.)
Interestingly, the condition for achievement unlocked now changed
since we're not applying the events until they have all been
constructed.
  • Loading branch information
Carl Masak committed Jul 1, 2012
1 parent e2a3e5d commit caea86f
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions crypt.pl
Expand Up @@ -91,19 +91,34 @@
);
}
}
@target_rod.push( @source_rod.pop );
my $size = $moved_disk.words[0];
my @events = DiskMoved.new(:$size, :$source, :$target);
if %!state<right> == @disks && $!achievement eq 'locked' {
$!achievement = 'unlocked';
if %!state<right> == @disks-1
&& $target eq 'right'
&& $!achievement eq 'locked' {
@events.push(AchievementUnlocked.new);
}
if $size eq 'small' && $!achievement eq 'unlocked' {
$!achievement = 'locked';
@events.push(AchievementLocked.new);
}
self!apply($_) for @events;
return @events;
}

# RAKUDO: private multimethods NYI
method !apply(Event $_) {
when DiskMoved {
my @source_rod := %!state{.source};
my @target_rod := %!state{.target};
@target_rod.push( @source_rod.pop );
}
when AchievementUnlocked {
$!achievement = 'unlocked';
}
when AchievementLocked {
$!achievement = 'locked';
}
}
}

sub throws_exception(&code, $ex_type, $message, &followup?) {
Expand Down

0 comments on commit caea86f

Please sign in to comment.