Re: Dr. Camles Brain De-Age Game
- From: "DJ Stunks" <DJStunks@xxxxxxxxx>
- Date: 27 Sep 2006 09:09:52 -0700
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
.
- References:
- Dr. Camles Brain De-Age Game
- From: Angerstein
- Dr. Camles Brain De-Age Game
- Prev by Date: can you pass an array in a form in Perl
- Next by Date: Re: call HTML page from PERL
- Previous by thread: Dr. Camles Brain De-Age Game
- Next by thread: can you pass an array in a form in Perl
- Index(es):
Relevant Pages
|
|