A simple maths quiz, answer as many as possible before the time is up.
It works but as I am very new to perl I expect there are many things which I could have done much better, constructive critism is very welcome.
#!/usr/bin/perl -w
use strict;
sub file_create;
if (! -e "hi.dat") { file_create };
open (FILE, "hi.dat") || die "cannot open file hi.dat\n";
my $time = <FILE>;
chomp $time;
print "\n******** A simple maths game *******\n******** Highest Score
+is $time";
$time = <FILE>;
chomp $time;
print "$time ********\n\n";
close (FILE) || die "cannot close file hi.dat\n";
sleep(1);
srand;
my $num_c = 0;
my $score_r = 0;
my $score_w = 0;
my @symbols = qw(+ - *);
$time = time();
while (time() - $time < 30) {
my $num_a = int rand (9)+1;
my $num_b = int rand (9)+1;
my $x = int rand (3);
my $equat = "$num_a $symbols[$x] $num_b";#
print "$equat = "; # thanks jeffa
$num_c = eval $equat; #
my $answer = <>;
if ($answer == $num_c) {
print "correct\n";
$score_r +=1;
}
else
{
print "wrong heh!\n";
$score_w +=1;
}
}
$time = time() - $time;
open (FILE, "hi.dat") || die "cannot open hi.dat\n";
$time = <FILE>;
chomp $time;
close (FILE) || die "cannot close file hi.dat\n";
print "Time is up!! you scored $score_r correct and $score_w incorrect
+ \n ";
if ($time < $score_r) {
open (FILE, ">hi.dat") || die "cannot open hi.dat\n";
print "You beat the hi score!!.. please enter your name so you can
+ be credited for this feat!\n";
$time = <>;
print FILE "$score_r \nby $time";
close (FILE) || die "cannot close file hi.dat\n";
}
sub file_create {
print "cannot open file hi.dat shall I create it? y or n\n";
my $time = <>;
chomp $time;
if ($time eq "y")
{
open (FILE, ">hi.dat") || die "Sorry I was unable to create hi.dat
+\n";
print FILE "1\n by Gordy";
close (FILE) || die "cannot close file hi.dat\n";
}
else {
die "I need hi.dat sorry I am unable to proceed\n";
}
}
Thanks for the advice, I was looking for a better way of handling the way the sum was processed so your tip is most welcome.
Regarding your suggestion of changing the high score file format to score:name .. I could then handle the file with something like ..
#...
open (FILE, "hi.dat") || die "cannot open file hi.dat\n"; while (<FILE
+>) {
chomp;
($hi_score,$name) = split(/:/); }
close (FILE) || die "cannot close file hi.dat\n";
#...
One suggestion I can think of right off the top of my head is to say: chomp($time = <FILE>), instead of $time = <FILE>; chomp $time;, but that's really just a matter of taste.
Also, you might want to consider indenting the contents of subroutines, the subby at the end is a bit hard to read.
++ for reminding me how horridble I am at even simple math without scratch paper, and for use strict :)