Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[bin/crypt] integrated hanoi game into crypt

  • Loading branch information...
commit 1a921765e3db2a77b98d1a2e6ff9ab24245edb60 1 parent 5320541
Carl Mäsak authored
Showing with 50 additions and 2 deletions.
  1. +50 −2 bin/crypt
52 bin/crypt
View
@@ -945,8 +945,16 @@ class Adventure::Engine {
}
}
+class X::Crypt is Exception {
+}
+
+class X::Crypt::NoDisksHere is X::Crypt {
+}
+
class Crypt::Game {
has $!engine;
+ has $!hanoi;
+ has $!player_location;
submethod BUILD() {
$!engine = Adventure::Engine.new();
@@ -1015,16 +1023,27 @@ class Crypt::Game {
}
});
- .place_player('clearing');
+ .place_player($!player_location = 'clearing');
}
+
+ $!hanoi = Hanoi::Game.new();
}
method look {
return $!engine.look;
}
+ method !update_local_state(@events) {
+ for @events {
+ when Adventure::PlayerWalked { $!player_location = .to }
+ when Adventure::PlayerWasPlaced { $!player_location = .in }
+ }
+ }
+
method walk($direction) {
- return $!engine.walk($direction);
+ my @events = $!engine.walk($direction);
+ self!update_local_state(@events);
+ @events;
}
method open($thing) {
@@ -1055,6 +1074,13 @@ class Crypt::Game {
return $!engine.use($thing);
}
+ method move($source, $target) {
+ die X::Crypt::NoDisksHere.new
+ unless $!player_location eq 'hall';
+
+ return $!hanoi.move($source, $target);
+ }
+
method save {
$!engine.save;
}
@@ -2209,6 +2235,28 @@ multi MAIN('test') {
'looking with the flashlight switched on';
}
+ {
+ my $game = game_from_chamber();
+
+ throws_exception
+ { $game.move('left', 'middle') },
+ X::Crypt::NoDisksHere,
+ 'moving disks in the right room (-)';
+ }
+
+ {
+ my $game = game_from_hall();
+
+ $game.use('flashlight');
+ is $game.move('left', 'middle'),
+ Hanoi::DiskMoved.new(
+ :disk('tiny disk'),
+ :source<left>,
+ :target<middle>,
+ ),
+ 'moving disks in the right room (+)';
+ }
+
done;
}
Please sign in to comment.
Something went wrong with that request. Please try again.