#!/usr/bin/perl # Nate's Word Galaxy Generator # # Nate Oostendorp - March 15, 1999 use strict; use CGI; use vars qw($width); use vars qw($height); $width = 20; $height = 20; #from the perl cookbook sub fisher_yates_shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i, $j] = @$array[$j, $i]; } } sub mapToGrid { my ($GR, $word, $x, $y, $pos, $direction, $RR, $DR) = @_; if ($direction eq 'right') { $x -= $pos; if ($x < 0 or $y < 0) { return;} for (my $i = 0; $i < length $word; $i++) { $$GR[$x+$i][$y] = substr $word, $i, 1; if ($x + $i >= $width) { $width = $x + $i + 1; } } push @$RR, [$word, $x, $y]; } else { $y -= $pos; if ($x < 0 or $y < 0) { return;} for (my $i = 0; $i < length $word; $i++) { $$GR[$x][$y+$i] = substr $word, $i, 1; if ($y + $i >= $height) { $height = $y + $i + 1; } } push @$DR, [$word, $x, $y]; } } sub canMapToGrid { my ($GR, $word, $x, $y, $pos, $direction) = @_; if ($direction eq 'right') { $x -= $pos; for (my $i = 0; $i < length $word; $i++) { if ($$GR[$x+$i][$y]) { return if ($$GR[$x+$i][$y] ne substr ($word, $i, 1) ); } } } else { $y -= $pos; for (my $i = 0; $i < length $word; $i++) { if ($$GR[$x][$y+$i]) { return if ($$GR[$x][$y+$i] ne substr ($word, $i, 1)); } } } 1; } sub goRight { my ($word, $DR, $GR) = @_; fisher_yates_shuffle ($DR) if @$DR > 1; for (my $i = 0; $i < length $word; $i++) { my $letter = substr $word, $i, 1; foreach my $REF (@$DR) { my $index = 0; while ($index = index ($$REF[0], $letter, $index) and $index >= 0) { return ($$REF[1], $$REF[2] + $index, $i, "right") if (canMapToGrid ($GR, $word, $$REF[1], $$REF[2] + $index, $i, "right")); $index++; } } } return (0, 0, 0, 0); print "uh-oh! We got an X floater: $word\n"; } sub goDown { my ($word, $RR, $GR) = @_; fisher_yates_shuffle $RR if @$RR > 1; for (my $i = 0; $i < length $word; $i++) { my $letter = substr $word, $i, 1; foreach my $REF (@$RR) { my $index = 0; # print "hi $$REF[0], $$REF[1], $$REF[2]\n"; while ($index = index ($$REF[0], $letter, $index) and $index >= 0) { return ($$REF[1] + $index, $$REF[2], $i, "down") if (canMapToGrid ($GR, $word, $$REF[1] + $index, $$REF[2], $i, "down")); $index++; } } } return (0, 0, 0, 0); print "uh-oh! We got a Y floater: $word\n"; } sub findPlace { my ($word, $GR) = @_; #we'll try 100 places for (my $i = 0; $i < 100; $i++) { my $x = int rand ($width); my $y = int rand ($height); if (canMapToGrid ($GR, $word, $x, $y, 0, 'right')) { return ($x, $y, 0, 'right'); } elsif (canMapToGrid ($GR, $word, $x, $y, 0, 'down')) { return ($x, $y, 0, 'down'); } } return (0, 0, 0, 0); } #thanks daron sub printHtmlTable { my($width, $height, $gr)=@_; print "
$$gr[$c][$d] | "; } print "
"; print " Nate's Word Galaxy Generator"; printHtmlTable $width, $height, \@grid; print "spin your own:\n"; print $query->startform . $query->textarea ('galaxytext', $string, 10, 50) . $query->submit ('explore'); print ""; print " this affordable entertainment provided by Nate Oostendorp. |