#! /usr/bin/perl -w # Use bondage and discipline use strict; use POSIX qw( ceil ); # Set up vars my $xmagnitude = 20; my $ymagnitude = 20; my @board; my %dir = ("x" => 0, "y" => -1); my $iterations = 1000; my $pause = 1; # Initialise board for (my $y = 0; $y < $ymagnitude; ++$y) { for (my $x = 0; $x < $xmagnitude; ++$x) { $board[$y][$x] = " "; } } # Find centre of board and X marks the spot my %pos; $pos{"x"} = ceil(($xmagnitude / 2) - 1); $pos{"y"} = ceil(($ymagnitude / 2) - 1); $board[$pos{"y"}][$pos{"x"}] = "X"; # start the loop my $count = 0; while ($count < $iterations) { system(($^O eq 'MSWin32') ? 'cls' : 'clear'); # Thx QandA Editors map {print join ("", @$_)."\n"} @board; $pos{"x"} += $dir{"x"}; $pos{"y"} += $dir{"y"};; # Exit if out of bounds if($pos{"x"} >= $ymagnitude || $pos{"x"} < 0 || $pos{"y"} >= $xmagnitude || $pos{"y"} < 0) { print "\nAnt has reached the edge of the board!\n\n"; exit; } if ($board[$pos{"y"}][$pos{"x"}] eq "X") { # Turn left and make an O $board[$pos{"y"}][$pos{"x"}] = " "; # Remember your trig calculus my $tempvar = - $dir{"x"}; $dir{"x"} = $dir{"y"}; $dir{"y"} = $tempvar; } else { # Turn right and make an X $board[$pos{"y"}][$pos{"x"}] = "X"; # Remember your trig calculus my $tempvar = - $dir{"y"}; $dir{"y"} = $dir{"x"}; $dir{"x"} = $tempvar; } ++$count; sleep $pause; } # ##### End of Script ##### #