Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] integrated hanoi game into crypt

  • Loading branch information...
commit 1a921765e3db2a77b98d1a2e6ff9ab24245edb60 1 parent 5320541
Carl Mäsak authored July 22, 2012

Showing 1 changed file with 50 additions and 2 deletions. Show diff stats Hide diff stats

  1. 52  bin/crypt
52  bin/crypt
@@ -945,8 +945,16 @@ class Adventure::Engine {
945 945
     }
946 946
 }
947 947
 
  948
+class X::Crypt is Exception {
  949
+}
  950
+
  951
+class X::Crypt::NoDisksHere is X::Crypt {
  952
+}
  953
+
948 954
 class Crypt::Game {
949 955
     has $!engine;
  956
+    has $!hanoi;
  957
+    has $!player_location;
950 958
 
951 959
     submethod BUILD() {
952 960
         $!engine = Adventure::Engine.new();
@@ -1015,16 +1023,27 @@ class Crypt::Game {
1015 1023
                     }
1016 1024
                 });
1017 1025
 
1018  
-            .place_player('clearing');
  1026
+            .place_player($!player_location = 'clearing');
1019 1027
         }
  1028
+
  1029
+        $!hanoi = Hanoi::Game.new();
1020 1030
     }
1021 1031
 
1022 1032
     method look {
1023 1033
         return $!engine.look;
1024 1034
     }
1025 1035
 
  1036
+    method !update_local_state(@events) {
  1037
+        for @events {
  1038
+            when Adventure::PlayerWalked { $!player_location = .to }
  1039
+            when Adventure::PlayerWasPlaced { $!player_location = .in }
  1040
+        }
  1041
+    }
  1042
+
1026 1043
     method walk($direction) {
1027  
-        return $!engine.walk($direction);
  1044
+        my @events = $!engine.walk($direction);
  1045
+        self!update_local_state(@events);
  1046
+        @events;
1028 1047
     }
1029 1048
 
1030 1049
     method open($thing) {
@@ -1055,6 +1074,13 @@ class Crypt::Game {
1055 1074
         return $!engine.use($thing);
1056 1075
     }
1057 1076
 
  1077
+    method move($source, $target) {
  1078
+        die X::Crypt::NoDisksHere.new
  1079
+            unless $!player_location eq 'hall';
  1080
+
  1081
+        return $!hanoi.move($source, $target);
  1082
+    }
  1083
+
1058 1084
     method save {
1059 1085
         $!engine.save;
1060 1086
     }
@@ -2209,6 +2235,28 @@ multi MAIN('test') {
2209 2235
             'looking with the flashlight switched on';
2210 2236
     }
2211 2237
 
  2238
+    {
  2239
+        my $game = game_from_chamber();
  2240
+
  2241
+        throws_exception
  2242
+            { $game.move('left', 'middle') },
  2243
+            X::Crypt::NoDisksHere,
  2244
+            'moving disks in the right room (-)';
  2245
+    }
  2246
+
  2247
+    {
  2248
+        my $game = game_from_hall();
  2249
+
  2250
+        $game.use('flashlight');
  2251
+        is $game.move('left', 'middle'),
  2252
+            Hanoi::DiskMoved.new(
  2253
+                :disk('tiny disk'),
  2254
+                :source<left>,
  2255
+                :target<middle>,
  2256
+            ),
  2257
+            'moving disks in the right room (+)';
  2258
+    }
  2259
+
2212 2260
     done;
2213 2261
 }
2214 2262
 

0 notes on commit 1a92176

Please sign in to comment.
Something went wrong with that request. Please try again.