#!/usr/bin/perl -w # autoplay.pl - Don Yang (uguu.org) # # Script to try to play the game perfectly # # 2014-10-15 use strict; # Read the input source file name from Makefile sub FindCanonicalSource() { my $file; open $file, " ) { if( $line =~ /^source = (\S+)/ ) { close $file; return $1; } } close $file; die "Missing source reference in Makefile\n"; } # Detect end game condition sub GameOver($) { my ($source) = @_; open my $file, "<$source" or die $!; my $game_over = 0; while( my $line = <$file> ) { if( $line =~ /^\/\* You win/ || $line =~ /YOU WIN!/ || $line =~ /^Game over/ ) { close $file; return 1; } } close $file; return 0; } # Run compiler at scheduled times sub Run() { my $target = "./sinon.exe"; my $results = ""; foreach my $compiler ("gcc", "clang") { my $source = FindCanonicalSource(); my $start_time = time; my $fire1 = $start_time - 60; my $fire2 = $start_time - 60; my $count1 = 0; my $count2 = 0; for(my $i = 0; $i < 60; $i++) { my $current_time = time; if( $current_time - $fire1 > 5 ) { # Fire Hecate II system("$compiler -O2 $source -o sinon.exe") == 0 or die; system("$target | tee tmp.c") == 0 or die; $source = "tmp.c"; $fire1 = $current_time; $count1++; if( GameOver($source) ) { last; } } if( $current_time - $fire2 > 3 ) { # Fire Glock 18C system("$compiler -O0 $source -o sinon.exe") == 0 or die; system("$target | tee tmp.c") == 0 or die; $source = "tmp.c"; $fire2 = $current_time; $count2++; if( GameOver($source) ) { last; } } sleep 1; } $results .= "$compiler: count1 = $count1, count2 = $count2, time = " . (time - $start_time) . "\n"; } unlink $target or die $!; print $results; } Run();