Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

maths-thing

by Gordy (Scribe)
on Aug 31, 2002 at 20:29 UTC ( [id://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";

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";
}
}
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:
    score:name
    
    in case you want to keep a top 10 list - much easier to parse. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (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"; #...
      Gordy
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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://194383]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-20 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found