Important alert: (current site time 7/15/2013 9:00:53 PM EDT)
 

VB icon

GAN- Guess A Number game

Email
Submitted on: 3/23/2003 9:26:00 AM
By: SpyderCo  
Level: Intermediate
User Rating: By 1 Users
Compatibility: 5.0 (all versions), 4.0 (all versions)
Views: 9152
(About the author)
 
     Computer generates a semi-random number upto a max number you specify. User gets X guesses to guess the correct number (default set at 5 tries), the system will tell you whether your guess was too high or too low. Stats and percents are saved in DBM.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: GAN- Guess A Number game
= Description:Computer generates a semi-random number upto a max number you specify. User gets X guesses to guess the correct number (default set at 5 tries), the system will tell you whether your guess was too high or too low. Stats and percents are saved in DBM.
= By: SpyderCo
=
= Assumes:If you don't have SDBM installed change the affiliated lines to the database of your choice.
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=433&lngWId=6=for details.=**************************************

#!/usr/bin/perl -W
use strict;
use warnings;
use POSIX;
use Fcntl;
use SDBM_File;
my %dbm;
my $test = "game.dbm";
my $win = 0;
my ( $total, $wonpercent, $lostpercent );
tie( %dbm, 'SDBM_File', $test, O_CREAT | O_RDWR, 0644 )
 || die "Died tying database\nReason: $!";
unless ( exists $dbm{win} ) { $dbm{win} = 0 }
unless ( exists $dbm{lost} ) { $dbm{lost} = 0 }
# max = maximum number to randomize
# tries = # of tries not to exceed $allowed
# allowed = max number of tries allowed
my $tries = 0;
my $guess;
my $allowed = 5;
print "What's the highest number you want to try?\n";
chomp( my $max = <STDIN> );
my $answer = int( rand($max) ) + 1;
while ( $tries < $allowed ) {
print " Your guess: ";
chomp( $guess = <STDIN> );
$tries++;
if ( $guess eq $answer ) {
last;
}
elsif ( $guess > $answer ) {
print " $guess is too high!\n";
}
elsif ( $guess < $answer ) {
print " $guess is too low!\n";
}
}
if ( $guess eq $answer ) {
print "\nYou got it right!\n";
print "It only took you $tries tries!\n";
$dbm{won}++;
$dbm{total}++;# for stats
}
else {
print "\n You lose! You're only allowed $allowed guesses :(\n";
print "Answer was: $answer\n";
$dbm{lost}++;
$dbm{total}++;# for stats
}
print "-----------------------------------------\n";
print "Total games played: $dbm{total}\n";
$wonpercent = ( $dbm{won} / $dbm{total} ) * 100;
$lostpercent = ( $dbm{lost} / $dbm{total} ) * 100;
print "Games won: $dbm{won}\n";
print "Games lost: $dbm{lost}\n";
printf( "Percent won: %.0f\n", $wonpercent );
printf( "Percent lost: %.0f\n", $lostpercent );
print "-----------------------------------------\n";


Other 17 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.