Posted By

gdonald on 09/27/06


Tagged

guess


Versions (?)

perl guess


 / Published in: Perl
 

  1. #!/usr/bin/env perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. srand (time ^ $$ ^ unpack "%32L*", `ps axww | gzip`);
  7. my $p = undef;
  8. do {
  9. run();
  10. print "Play again? ";
  11. chomp($p = <STDIN>);
  12. } while ((lc $p) eq 'y');
  13.  
  14. sub run {
  15. my $s = int(rand 100)+1;
  16. my $g = undef;
  17. my $t = 0;
  18. do {
  19. do {
  20. print "Guess (1-100)?\n";
  21. chomp($g = <STDIN>);
  22. } while (!($g=~/\d+/) || $g>100 || $g<1);
  23. $t++;
  24. if ($g > $s){
  25. print "$g is too HIGH!\n";
  26. } elsif ($g < $s) {
  27. print "$g is too LOW!\n";
  28. }
  29. } while ($g != $s);
  30. my $y = $t == 1 ? "y" : "ies";
  31. print "$g is correct, YAY! It only took you $t tr$y!\n";
  32. }

Report this snippet  

You need to login to post a comment.