Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] show hanoi game in hall

  • Loading branch information...
commit 532054147e50b56a98ab0899fbf40e513cc4f20b 1 parent 875023b
Carl Mäsak authored July 22, 2012

Showing 1 changed file with 52 additions and 38 deletions. Show diff stats Hide diff stats

  1. 90  bin/crypt
90  bin/crypt
@@ -1106,6 +1106,8 @@ multi MAIN() {
1106 1106
         say "";
1107 1107
     }
1108 1108
 
  1109
+    my @all_events;
  1110
+
1109 1111
     loop {
1110 1112
         my $command = prompt('> ');
1111 1113
         given $command {
@@ -1141,6 +1143,10 @@ multi MAIN() {
1141 1143
                         say ucfirst .room;
1142 1144
                         say "";
1143 1145
                         say %descriptions{.room};
  1146
+                        if .room eq 'hall' {
  1147
+                            print_hanoi_game(@all_events);
  1148
+                            say "";
  1149
+                        }
1144 1150
                         for .things -> $thing {
1145 1151
                             if $thing ~~ Pair {
1146 1152
                                 say "There is a $thing.key() here.";
@@ -1182,11 +1188,16 @@ multi MAIN() {
1182 1188
                     }
1183 1189
                 }
1184 1190
                 my @events = $game."$verb"(|@args);
  1191
+                push @all_events, @events;
1185 1192
                 for @events {
1186 1193
                     when Adventure::PlayerWalked { say ucfirst .to; }
1187 1194
                     when Adventure::PlayerLooked {
1188 1195
                         say "";
1189 1196
                         say %descriptions{.room};
  1197
+                        if .room eq 'hall' {
  1198
+                            print_hanoi_game(@all_events);
  1199
+                            say "";
  1200
+                        }
1190 1201
                         for .things -> $thing {
1191 1202
                             if $thing ~~ Pair {
1192 1203
                                 say "There is a $thing.key() here.";
@@ -1240,53 +1251,54 @@ multi MAIN() {
1240 1251
     say "Thanks for playing.";
1241 1252
 }
1242 1253
 
1243  
-multi MAIN('hanoi') {
1244  
-    my Hanoi::Game $game .= new;
  1254
+sub print_hanoi_game(@all_events) {
1245 1255
     my @disks = <tiny small medium large huge> X~ ' disk';
1246 1256
     my @rods = <left middle right>;
1247 1257
 
1248  
-    sub params($method) {
1249  
-        $method.signature.params
1250  
-            ==> grep { .positional && !.invocant }
1251  
-            ==> map { .name.substr(1) }
  1258
+    my %s =
  1259
+        left   => [reverse @disks],
  1260
+        middle => [],
  1261
+        right  => [],
  1262
+    ;
  1263
+    for @all_events {
  1264
+        when Hanoi::DiskMoved   { %s{.target}.push: %s{.source}.pop }
  1265
+        when Hanoi::DiskRemoved { %s{.source}.pop }
  1266
+        when Hanoi::DiskAdded   { %s{.target}.push: .disk }
1252 1267
     }
1253  
-    my %commands = map { $^m.name => params($m) }, $game.^methods;
1254  
-    my @all_events;
1255 1268
 
1256  
-    sub print_board() {
1257  
-        my %s =
1258  
-            left   => [reverse @disks],
1259  
-            middle => [],
1260  
-            right  => [],
  1269
+    say "";
  1270
+    for reverse ^6 -> $line {
  1271
+        my %disks =
  1272
+            'none'        => '     |     ',
  1273
+            'tiny disk'   => '     =     ',
  1274
+            'small disk'  => '    ===    ',
  1275
+            'medium disk' => '   =====   ',
  1276
+            'large disk'  => '  =======  ',
  1277
+            'huge disk'   => ' ========= ',
1261 1278
         ;
1262  
-        for @all_events {
1263  
-            when Hanoi::DiskMoved   { %s{.target}.push: %s{.source}.pop }
1264  
-            when Hanoi::DiskRemoved { %s{.source}.pop }
1265  
-            when Hanoi::DiskAdded   { %s{.target}.push: .disk }
  1279
+
  1280
+        sub disk($rod) {
  1281
+            my $disk = %s{$rod}[$line] // 'none';
  1282
+            %disks{ $disk };
1266 1283
         }
1267 1284
 
1268  
-        say "";
1269  
-        for reverse ^6 -> $line {
1270  
-            my %disks =
1271  
-                'none'        => '     |     ',
1272  
-                'tiny disk'   => '     =     ',
1273  
-                'small disk'  => '    ===    ',
1274  
-                'medium disk' => '   =====   ',
1275  
-                'large disk'  => '  =======  ',
1276  
-                'huge disk'   => ' ========= ',
1277  
-            ;
1278  
-
1279  
-            sub disk($rod) {
1280  
-                my $disk = %s{$rod}[$line] // 'none';
1281  
-                %disks{ $disk };
1282  
-            }
  1285
+        say join '  ', map &disk, @rods;
  1286
+    }
  1287
+    say join '--', '-----------' xx @rods;
  1288
+}
1283 1289
 
1284  
-            say join '  ', map &disk, @rods;
1285  
-        }
1286  
-        say join '--', '-----------' xx @rods;
  1290
+multi MAIN('hanoi') {
  1291
+    my Hanoi::Game $game .= new;
  1292
+
  1293
+    sub params($method) {
  1294
+        $method.signature.params
  1295
+            ==> grep { .positional && !.invocant }
  1296
+            ==> map { .name.substr(1) }
1287 1297
     }
  1298
+    my %commands = map { $^m.name => params($m) }, $game.^methods;
  1299
+    my @all_events;
1288 1300
 
1289  
-    print_board();
  1301
+    print_hanoi_game(@all_events);
1290 1302
     say "";
1291 1303
     loop {
1292 1304
         my $command = prompt('> ');
@@ -1307,10 +1319,12 @@ multi MAIN('hanoi') {
1307 1319
                 say "  h[elp]";
1308 1320
                 say "  s[how]";
1309 1321
                 say "";
  1322
+                my @disks = <tiny small medium large huge> X~ ' disk';
  1323
+                my @rods = <left middle right>;
1310 1324
                 say "Disks: ", join ', ', @disks;
1311 1325
                 say "Rods: ", join ', ', @rods;
1312 1326
             }
1313  
-            when 's' | 'show' { print_board() }
  1327
+            when 's' | 'show' { print_hanoi_game(@all_events) }
1314 1328
 
1315 1329
             sub munge   { $^s.subst(/' disk'»/, '_disk', :g) }
1316 1330
             sub unmunge { $^s.subst(/'_disk'»/, ' disk', :g) }
@@ -1325,7 +1339,7 @@ multi MAIN('hanoi') {
1325 1339
                 }
1326 1340
                 my @events = $game."$verb"(|@args);
1327 1341
                 push @all_events, @events;
1328  
-                print_board();
  1342
+                print_hanoi_game(@all_events);
1329 1343
                 for @events {
1330 1344
                     when Hanoi::AchievementUnlocked { say "Achievement unlocked!" }
1331 1345
                     when Hanoi::AchievementLocked { say "Achievement locked!" }

0 notes on commit 5320541

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