#!/usr/bin/perl # # checklog # # log file checker for Lab 2 # # Author: AHF # # Expects a file "log.txt" to exist # # Checks the following: # - that no two robots ever occupy the same grid square # - that all goals are consumed at the end of the program. # my($last_ngoals); my($last_nrobots) = -1; sub check_line { my($strg) = $_; my(%log) = {}; if(!($strg =~ /(\d+) goals left;/)) { die "Error: Can't read number of goals from line $.\n$_\n"; }; #print $1 . ":::$strg\n"; $last_ngoals = $1; $strg = $'; my($num) = 0; while($strg ne "") { #print "N:$last_ngoals\n"; #print "$strg\n"; if($strg =~ /^[^;]*(Robot\d+) (\S+) At \[(\d+),(\d+)\];/) { if(exists $log{$3, $4}) { die "Error: ". $1 . " and " . $log{$3,$4} . " occupy the same location ($3, $4) on line $.\n"; }; $log{$3, $4} = $1; $strg = $'; ++$num; #print "FOO $2::$3\n"; } else { $strg = ""; } }; # foreach $key (keys(%log)) # { # print "KEY:$key\n"; # }; #print "Num Robots: $num\n"; if($last_nrobots != -1 && $last_nrobots != $num) { die "Error: robot count is not correct on line $..\n"; }; $last_nrobots = $num; }; open(FP, ") { check_line($_); }; close(FP); print "N_GOALS: $last_ngoals; N_ROBOTS: $last_nrobots\n"; if($last_ngoals != 0) { die "Error: Not all goals were eaten.\n"; }; print "****ALL OK****\n";