Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Closed geometry: a train track problem

by Anonymous Monk
on Jan 04, 2006 at 10:16 UTC ( [id://520835]=note: print w/replies, xml ) Need Help??


in reply to Closed geometry: a train track problem

Hi
You might want to take a look at the code of a game called "MAZE". It works with a 2 dimentional map, keeps track of your current position and gives you valid directions to make it through the MAZE. Maybe this gives you a good insperation.

Here is a copy of it:

#!/usr/bin/perl -w use strict; my @maze=( [ qw( e swe we ws ) ], [ qw( se new sw ns ) ], [ qw( ns - ns n ) ], [ qw( ne w ne w ) ], ); my %direction=( n=> [ -1, 0], s=> [1, 0], e=> [ 0, 1], w=> [0, -1]); my %full=( e => 'East', n => 'North', w=>'West', s=>'South'); my($curr_x, $curr_y, $x, $y)=(0,0,3,3); my $move; sub disp_location { my($cx, $cy)=@_; print "You may move "; while($maze[$cx][$cy]=~/([nsew])/g) { print "$full{$1} "; } print "($maze[$cx][$cy])\n"; } sub move_to { my($new, $xref, $yref)=@_; $new=substr(lc($new),0,1); if ($maze[$$xref][$$yref]!~/$new/) { print "Invalid direction, $new.\n"; return; } $$xref += $direction{$new}[0]; $$yref += $direction{$new}[1]; } until ( $curr_x == $x and $curr_y == $y ) { disp_location($curr_x, $curr_y); print "Which way? "; $move=<STDIN>; chomp $move; exit if ($move=~/^q/); move_to($move, \$curr_x, \$curr_y); } print "You made it through the maze!\n";

(Provided by "SAMS Teach yourself Perl in 24 Hours")

Good luck!
Marcel

200604 Janitored by Corion: Added formatting

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://520835]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found