Re: Dr. Camles Brain De-Age Game



Angerstein wrote:
Hi,
I had some spare time and write a perl version of dr. kamigawas brain
age / Big Brain Academy.
There are 6 Memory and Calculation tests. You need the Moduls
Heap-Simple and Time::HiRes.
If somebody adds further tests/games etc. please mail back.

Have Fun!

Well, since I'm in a meeting, I'll give you some pointers on the first
part of your code. Others can help you with the rest, or you can take
my suggestions and improve the rest of this script.


#!/usr/local/bin/perl

missing:
use strict;
use warnings;

use Heap::Simple;
use Time::HiRes;

########################################################################
########

my @abc = (a .. z);

this throws a warning (had you had them enabled).

my @alpha = ('a'..'z');

my $chars1 = 0;
my $chars2 = 0;
my $chars3 = 0;
my $chars4 = 0;
my $chars5 = 0;

anytime you name variables like this it means you should be using an
array.

also, don't ever predeclare variables in perl unless you absolutely
need to. just declare them when they're used.

my $string = undef;

$right = 0;
$wrong = 0;

I would have a variable representing the number of turns, and I'd init
$right and $wrong together, I suppose.

my $number_of_turns = 10;
my ($right, $wrong) = (0,0);


$turns = 0;
# start timer
$start = time();

while ($turns <= 10){

since you initialized $turns to 0 you have a fencepost error here. this
loop actually executes 11 times. I assume you wanted it to do it 10
times?

use a for instead

for (1..$number_of_turns) {

$chars1 = int(rand(26));
$chars2 = int(rand(26));
$chars3 = int(rand(26));
$chars4 = int(rand(26));
$chars5 = int(rand(26));
$string = $abc[$chars1] . $abc[$chars2] . $abc[$chars3] .
$abc[$chars4] . $abc[$chars5];

all this can be replaced with a one-liner:

my $string_length = 5;
my $string
= join '', map { $alpha[ rand @alpha ] } 1..$string_length;

print "Keep the letters in mind!\n $string\n";
sleep(1.5);

for ( my $i = 1; $i <= 30; $i++) {
print "\n\n\n\n\n\n\n\n\n\n";
}

perldoc perlop - look for the x operator

print "\n" x 300;

print "Enter:\n";
$input = <STDIN>;
chomp($input);

you can combine these:

chomp( my $input = <STDIN> );

if ( $input eq $string ) {
print "Very Good. $input is right! Feel Happy!\n";
$right++;
} else {
print "$input is wrong!!! Bad! Stupid!\n";
$wrong++;

if ( $input eq $string ) {
print "Very Good. $input is right! Feel Happy!\n";
$right_answers++;
} else {
print "$input is wrong!!! Bad! Stupid!\n";
}


}
$turns++;
}
# end timer
$end = time();
$tsum = ($end - $start);
print "Right: $right Wrong: $wrong\nIn $tsum seconds!";
$score += &score($right, $wrong, $tsum, '-0.2');
print "Your Score is $score!\n";

Hope that helps. I would highly recommend you buy
_Perl Best Practices_ by my main man Damian Conway. His
book would really help you with a lot of the coding practices
you used here.

here's my final version:

#!/usr/local/bin/perl

use strict;
use warnings;

my @alpha = ('a'..'z');
my $number_of_turns = 4;
my $string_length = 5;

my ($right,$wrong) = (0,0);
my $start = time();

for (1..$number_of_turns) {
my $string
= join '', map { $alpha[ rand @alpha ] } 1..$string_length;

print "Keep these letters in mind: $string\n";
sleep(1.5);
print "\n" x 300;

print "Type the letters you saw: ";
chomp( my $input = <STDIN> );

if ( $input eq $string ) {
print "Very Good. $input is right! Feel Happy!";
$right++;
} else {
print "$input is wrong!!! Bad! Stupid!";
$wrong++;
}

print "\n\n";
}

my $tsum = time() - $start;
print "Right: $right Wrong: $wrong\nIn $tsum seconds!";

__END__

-jp

.



Relevant Pages

  • Re: error messege: "subroutine Cwd::fastcwd redefined"
    ... Attached is the file created by perlbug. ... asking for all warnings even though Cwd is trying (via the older ... case it looks like a bug in the Perl distribution. ... HOME (unset) ...
    (perl.beginners)
  • Re: file rename help
    ... > I am very novice as you will see in my Perl skills. ... use warnings; ... because I am not afraid of undef values, ... even as a piece of shell code, your command is wasting at least two ...
    (comp.lang.perl.misc)
  • Re: Multiple Parameters
    ... The above two lines ask Perl to hold you to the laws of good ... programming. ... you'll get better error messages and warnings ...
    (perl.beginners)
  • Re: Newbie questions, migrating from c++
    ... > to get no warnings using SEEK_SET etc, ... So if I'm to do something with chars in string (or perl ... It is not a good idea to use this method of subroutine invocation unless ... sub no_need { ...
    (comp.lang.perl.misc)
  • Re: Newbie. Use of uninitialized value in print??
    ... > Im having problems getting some Perl code to work (im a programming ... You should ask for all the help the perl can give you. ... Did your online reading include the posting guidelines for this group? ... Warnings: The script below ignores various issues such as the cases ...
    (comp.lang.perl.misc)