Sunday, December 27, 2009

Typing game in perl that I wrote.

ב''ה

Err... This would have to be more on the fun side and less on the functional. None the less I am having a great time learning perl and am looking forward to figuring out any functional programming aspects it might have.

#!/usr/bin/perl -w

use List::Util 'shuffle';
use Time::HiRes;

$file = "/users/foobar/perl/words_to_spell_document";
open (FH, "< $file") or die "Can't open $file for read: $!";
my @words = ;
close FH or die "Cannot close $file: $!";

$highest_score = 0;
$highest_word_points = 0;
$highest_word = "";
$equation = "";

do
{
@movewords = shuffle(@words);
@words = @movewords;
$percent = 100;
$speed = 0;
$correct = 0;
$start_time = 0;
$elapsed = 0;
$score = 0;
$total_length = 0;
$points = 0;
$highest_word_points_this = 0;
$highest_word_this = "";
$equation_this = "";
$accuracy_bonus = 1.0;

foreach $word (@words)
{
system "clear";
$current_time = time();
if ($current_time < ($start_time + 60) or $start_time == 0)
{
print "Welcome To mtyping! (q to quit)\n\n";
printf "Accuracy: %d", $percent; print "% - $correct Words Correct out of $speed\n\n";
if ($start_time == 0)
{
$elapsed = $start_time
} else
{
$elapsed = $current_time - $start_time;
}
print "Seconds Elapsed: $elapsed\n\n";
printf "Score: %d\n\n", $score;
printf "Last : %d\n\n", $points;
print "$movewords[0]";

my $start = [ Time::HiRes::gettimeofday( ) ];

$type = "";
while ($type eq "" or $type eq "\n" or $type =~ / / )
{
#print "Start Typing! (q to quite):\n";
$type = ;
}
my $score_elapsed = Time::HiRes::tv_interval( $start );

if ($start_time == 0){ $start_time = time();}

$total_length += length($type);
if ($type eq $word)
{
shift @movewords;
$speed++;
$correct++;
$percent = ($correct/$speed)*100;
$score += length($word) * (length($word) - ($score_elapsed * $accuracy_bonus));
$points = length($word) * (length($word) - ($score_elapsed * $accuracy_bonus));
if ($points > $highest_word_points_this)
{
$highest_word_points_this = $points;
$highest_word_this = $type;
$equation_this = length($word);
$equation_this = "$equation_this * ($equation_this - ($score_elapsed * $accuracy_bonus))";
}
if ($points > $highest_word_points)
{
$highest_word_points = $points;
$highest_word = $type;
$equation = length($word);
$equation = "$equation * ($equation - ($score_elapsed * $accuracy_bonus))";
}

}
elsif ($type eq "q\n")
{
exit;
}
else
{
shift @movewords;
$speed++;
$divideby = $correct;
if ($correct == 0){ $divideby++ }
$percent = ($divideby/$speed)*100;
$score -= length($word);
$points = length($word) * -1;
$accuracy_bonus = 2 - ($percent * .01)
}
}
else
{
last;
}
}
system "clear";
print "Good Game!\n\n";
printf "Accuracy: %d", $percent; print "% - $correct Words Correct out of $speed\n\n";
$total_length /= 5;
printf "Real Words Per Minute: %d\n\n",$total_length;
$bonus = 3 * (($speed *2) + $percent);
printf "Bonus: %d\n\n", $bonus;
$score += $bonus;
printf "Score: %d\n\n", $score;
printf "Last : %d\n\n", $points;
printf "Higest Word Points This Game : %d for $highest_word_this - Equation: $equation_this\n\n", $highest_word_points_this;
if ($score > $highest_score){$highest_score = $score}
printf "Higest Score This Game Session : %d\n\n", $highest_score;
printf "Higest Word Points This Game Session : %d for $highest_word - Equation: $equation\n\n", $highest_word_points;

while ($type !~ /^y$/ and $type !~ /^n$/)
{
print "Do You Want To Play Again (y or n)?\n";
chomp($type = );
}
} while ( $type !~ /^n$/)