#!/usr/bin/perl -w

use strict;
use Time::HiRes qw(gettimeofday tv_interval);
use POSIX;

my $max_moves = 25;
my $max_secs = 250;

## Clear scores to make room for new testing random scores.
#open(TMP, "+>scores.log");
#close TMP;

#foreach my $i (0..10) {
#	get_score();
#}

#sort_scores();

timer();

sub get_score {
 my $moves_left = int(rand(15))+1;
 my $time_left = int(rand(249))+1;
 my $score = 0;

 foreach my $looper (0..$moves_left) {
	$score += (250 + (int(rand(8))+1*int(rand(9))+1));
 }
 foreach my $looper (0..$time_left) {
	$score += (7 + int(rand(5)));
 }
 
 ## Write new random scores to scores.log
 open(LIST, "+>>scores.log");
 print LIST "$score\n";
 close LIST;
}

sub sort_scores {
 ## Get the random scores from scores.log
 my @score_list;
 open(LIST, "<scores.log");
 while ( <LIST> ) {
	@score_list = <LIST>; 
 }
 close LIST;

 ## Clear scores in highs.log
 open(TMP, "+> highs.log");
 close TMP;

 ## Sort list of scores (highest to lowest) and print to highs.log
 open(SCORE, "+>> highs.log");
 my @sorted = sort{$b <=> $a} @score_list;
 my $i = 0;
 while (defined $sorted[$i]) { $i++; }
 foreach my $loop (0..$i) {
	print SCORE "$sorted[$loop]";
 }
 close SCORE;
}

sub timer {
### TIMER type stuff ###
my $start_time = [gettimeofday];	print "start_time = $start_time\n";
my $count = 0;
foreach my $i (0..999999) {
	$count++;
}
my $end_time = [gettimeofday];		print "end_time = $end_time\n";

my $elapsed = tv_interval $start_time, $end_time;	print "elapsed = $elapsed\n";
$elapsed = floor $elapsed;				print "elapsed floored = $elapsed\n";

print "I counted from 1 to $count in $elapsed seconds!\n";
}
