#!c:/perl/bin/perl.exe use CGI::Carp qw(fatalsToBrowser); &read_form; $cordx=$INFO{'x'}; $cordy=$INFO{'y'}; $history=$INFO{'history'}; $decision=$INFO{'decision'}; @splitdecision=split(/,/,$decision); $decsize=@splitdecision; @olddecision = split(/-/,@splitdecision[$decsize-1]); $oldx=@olddecision[0]; $oldy=@olddecision[1]; if(!$cordx) {$cordx=0;} if(!$cordy) {$cordy=1;} &main; sub main { print "Content-type:text/html\n\n"; &loadmaze; &mapmaze; &printmap; &splithistory; &checkdirs; } sub loadmaze { open(maze, "./maze.txt"); @maze=; close(maze); } sub mapmaze { $x=0; $y=0; foreach $line (@maze) { $strlen=length($line); for ($x=0;$x<$strlen;$x++) { $mazemap[$x][$y]=substr($line, $strlen-($strlen-$x), 1); } $y++; } $totalx=$x; $totaly=$y; } sub printmap { print "
\n";
for($showy=0;$showy<$totaly;$showy++)
	{
	for($showx=0;$showx<$totalx;$showx++)
		{
			if($cordx == $showx && $cordy == $showy) { print "+"; }
			else { print $mazemap[$showx][$showy]; }
		}
		print "\n";
	}
	print "\n
\n"; } sub splithistory { $xcount=0; $ycount=0; @splithistory=split(/-/, $history); foreach $item (@splithistory) { @splititems=split(/,/, $item); @historyx[$xcount]=@splititems[0]; @historyy[$ycount]=@splititems[1]; $xcount++; $ycount++; } } sub checkdirs { $nextstep=""; if($mazemap[$cordx-1][$cordy] eq " ") { $cordcount=0; foreach $ycord (@historyy) { if(@historyx[$cordcount] == $cordx-1 && @historyy[$cordcount] == $cordy) {$nogoodl = 1;} $cordcount++; } if(!$nogoodl) {$nextstep="left";} } if($mazemap[$cordx+1][$cordy] eq " ") { $cordcount=0; foreach $ycord (@historyy) { if(@historyx[$cordcount] == $cordx+1 && @historyy[$cordcount] == $cordy) {$nogoodr = 1;} $cordcount++; } if(!$nogoodr && $nextstep ne "") {$decision .= "$cordx-$cordy,";} if(!$nogoodr) {$nextstep="right";} } if($mazemap[$cordx][$cordy-1] eq " ") { $cordcount=0; foreach $ycord (@historyy) { if(@historyx[$cordcount] == $cordx && @historyy[$cordcount] == $cordy-1) {$nogoodu = 1;} $cordcount++; } if(!$nogoodu && $nextstep ne "") {$decision.="$cordx-$cordy,";} if(!$nogoodu) {$nextstep="up";} } if($mazemap[$cordx][$cordy+1] eq " ") { $cordcount=0; foreach $ycord (@historyy) { if(@historyx[$cordcount] == $cordx && @historyy[$cordcount] == $cordy+1) {$nogoodd = 1;} $cordcount++; } if(!$nogoodd && $nextstep ne "") {$decision.="$cordx-$cordy,";} if(!$nogoodd) {$nextstep="down";} } if($mazemap[$cordx+1][$cordy] eq "*") {print "";} if($mazemap[$cordx-1][$cordy] eq "*") {print "";} if($mazemap[$cordx][$cordy+1] eq "*") {print "";} if($mazemap[$cordx][$cordy-1] eq "*") {print "";} if($nextstep eq "left") {$cordx--;} if($nextstep eq "right") {$cordx++;} if($nextstep eq "up") {$cordy--;} if($nextstep eq "down") {$cordy++;} if($nextstep) {$history .= "-$cordx,$cordy";} if(!$nextstep) {$cordx=$oldx;$cordy=$oldy;$decision=~s/$oldx-$oldy,//g;} if(!$nextstep && !$oldx) {print "";} print< var id=setTimeout("location.href='micromouse.pl?x=$cordx&y=$cordy&history=$history&decision=$decision'",100); EOF print $nextstep; } sub read_form { @vars = split(/&/, $ENV{QUERY_STRING}); foreach $var (@vars) { ($v,$i) = split(/=/, $var); $v =~ tr/+/ /; $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $i =~ tr/+/ /; $i =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $i =~ s///g; $INFO{$v} = $i; print $i; } } #sub read_form