Advertisement
hackloper775

Piedra paperl o t

Feb 9th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.56 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.      
  3. # Programa de prueba para Tiempo de Tux <http://www.itimetux.com>
  4. # Por Demianx864
  5. # Eres libre de estudiar modificar y añadir nuevas cosas a el programa,que te diviertas :D
  6.      
  7. use feature qw{ switch };
  8.      
  9. system('clear');
  10.    
  11. print "Nombre de jugador : ";
  12.      
  13. my $nombre = <STDIN>; chop($nombre);
  14.      
  15. my ($score,$score_cpu,$juegos) = (0,0,0);
  16.      
  17. my ($nj,$e) = (undef,undef);
  18.      
  19. my @cpu = ("piedra","papel","tijeras");
  20.      
  21. print "Numero de juegos : ";
  22.      
  23. while ($nj = <STDIN>) {
  24.      
  25. chop $nj;
  26.      
  27. given ($nj) {
  28.    when(/\d/) { last; }
  29.    when(/\D/) { print "Numero de juegos (numeros) : "; continue ;}
  30.    default { continue; }
  31. } }
  32.      
  33. &elejir();
  34.      
  35. sub elejir {
  36.      
  37. print "Piedra papel o tijeras : ";
  38.      
  39. while (my $eleccion = <STDIN>) {
  40.      
  41.  chop($eleccion);
  42.      
  43.  given ($eleccion) {
  44.      when(/piedra/i) { $e = 'piedra'; last; }
  45.      when(/papel/i) { $e = 'papel'; last; }
  46.      when(/tijeras/i) { $e = 'tijeras'; last; }
  47.      default {
  48.      print "Opcion incorrecta intenta de nuevo : "; continue }
  49.      
  50. } }
  51.      
  52. my @n = (0...2); my $i = $n[int rand @n];
  53.      
  54. &juego($e,$cpu[$i]);
  55.      
  56. }
  57.      
  58. sub juego {
  59.      
  60. my ($jugador,$pc) = @_;
  61.      
  62.  system ('clear');
  63.  print "Resultado : \n\n";
  64.  print "$nombre : $jugador " . "CPU : $pc";
  65.  if ($jugador eq $pc) {
  66.       print "\tEs un empate\n";
  67.  } elsif ($jugador eq "piedra") {
  68.       if ($pc eq "tijeras") {
  69.          print "\t$nombre gana\n"; $score = $score + 1;
  70.     } else {
  71.          print "\tCPU gana\n"; $score_cpu = $score_cpu + 1;
  72.     }
  73.  } elsif ($jugador eq "papel") {
  74.       if ($pc eq "piedra") {
  75.          print "\t$nombre gana\n"; $score = $score + 1;
  76.     } else {
  77.          print "\tCPU gana\n"; $score_cpu = $score_cpu + 1;
  78.     }
  79.  } else {
  80.       if ($pc eq "piedra") {
  81.          print "\tCPU gana\n"; $score_cpu = $score_cpu + 1;
  82.    } else {
  83.          print "\t$nombre gana\n"; $score = $score + 1;
  84.        }
  85.  }
  86.      
  87.  $juegos = $juegos + 1;
  88.      
  89.  print "\n$nombre Score : $score CPU Score : $score_cpu\n\n";
  90.      
  91.  &volver();
  92. }
  93.      
  94. sub volver {
  95.      
  96.   if ( $juegos == $nj ) {
  97.      print "$nombre Score Final : $score CPU Score final : $score_cpu\n\n";
  98.      if ( $score < $score_cpu ) {
  99.          print "CPU Gana $score_cpu a $score\n";
  100.      } elsif ( $score > $score_cpu ) {
  101.          print "$nombre Gana $score a $score_cpu\n";
  102.      } elsif ( $score == $score_cpu ) {
  103.          print "Es un empate :O\n"
  104.      } else { print "Error inesperado :("; }
  105.      
  106.   } else { &elejir; }
  107.      
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement