Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"


by Gordy (Scribe)
on Aug 31, 2002 at 20:29 UTC ( #194383=sourcecode: print w/replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info Gordy
Description: 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";


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;
    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
    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";
Replies are listed 'Best First'.
(jeffa) Re: maths-thing
by jeffa (Bishop) on Sep 01, 2002 at 13:53 UTC
    You can use the power of eval to get rid of this block:
    if ($x == "0"){ $num_c = $num_a + $num_b } if ($x == "1"){ $num_c = $num_a - $num_b } if ($x == "2"){ $num_c = $num_a * $num_b }
    like so:
    # ... my $equat = "$num_a $symbols[$x] $num_b"; print "$equat = "; # ... $num_c = eval $equat; # ...
    And there is really nothing wrong with the way you store the high score, but you might be better off in the long run keeping a format like:
    in case you want to keep a top 10 list - much easier to parse. :)


    (the triplet paradiddle with high-hat)
      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"; #...
Re: maths-thing
by Dylan (Monk) on Sep 01, 2002 at 04:18 UTC
    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 :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://194383]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2017-05-29 11:50 GMT
Find Nodes?
    Voting Booth?