Dart el nuevo lenguaje Google
14 years ago
Anything about Perl
1 #!/usr/bin/env perl 2 use MooseX::Declare; 3 4 class QuestionNode { 5 has question => ( is => "ro", isa => "Str", required => 1 ); 6 has [ "yes", "no" ] => ( is => "rw", isa => "Str|QuestionNode", required => 1 ); 7 } 8 9 role AnimalsGame { 10 11 has tree => ( 12 is => "ro", 13 isa => "QuestionNode", 14 default => sub { 15 QuestionNode->new( 16 { question => 'lives in the water', no => 'tiger', yes => 'tiburón' } ); 17 } 18 ); 19 20 method play { 21 my $guess = $self->tree; 22 my ( $node, $branch ); 23 while ( ref($guess) ) { 24 $node = $guess; 25 $branch = $self->yes( $node->question ) ? "yes" : "no"; 26 $guess = $node->$branch; 27 } 28 return if $self->yes("Is it a(n) $guess"); 29 my $animal = $self->prompt("Animal's name? : "); 30 my $diff = $self->prompt( 31 "A question true for $animal, but false for $guess: "); 32 $node->$branch( QuestionNode->new( { question => $diff, no => $guess, yes => $animal } ) ); 33 } 34 35 } 36 37 role ConsoleGame { 38 use Term::ReadLine; 39 use IO::Handle; 40 41 has title => ( is => "ro", isa => "Str", default => "Animals' game" ); 42 has term => ( 43 is => "ro", 44 isa => "Object", 45 lazy_build => 1, 46 handles => { prompt => "readline" } 47 ); 48 49 method _build_term { 50 Term::ReadLine->new( $self->title ); 51 } 52 53 method yes(Str $prompt) { 54 while (1) { 55 my $answer = $self->prompt("$prompt? (y/n): "); 56 return ( $2 ? 1 : 0 ) if $answer =~ /^\s*((yes|y)|(no|n))\s*/i; 57 $self->term->OUT->print("Please answer 'y' or 'n'\n"); 58 } 59 } 60 61 method run { 62 $self->play; 63 $self->play while $self->yes("Do you want to play again"); 64 } 65 } 66 67 class Game with AnimalsGame with ConsoleGame {} 68 69 Game->new->run;
1 #!/usr/bin/env perl
2 sub prompt {
3 print $_[0];
4 $line = <>;
5 chomp $line;
6 $line;
7 }
8
9 sub yes {
10 prompt("$_[0]? (y/n): ") =~ /^\s*y/i;
11 }
12
13 $question = $root = "lives in the water";
14 %tree = ( $root => [ 'tiger', 'shark' ] );
15 do {
16 {
17 $branch = yes($question);
18 $guess = $tree{$question}[$branch];
19 $question = $guess, redo if $tree{$guess};
20 $question = $root, next if yes("Is it a(n) $guess");
21 $animal = prompt("Animal's name? : ");
22 $diff = prompt( "A question true for $animal" .
23 ", but false for $guess: " );
24 $tree{$diff} = [ $tree{$question}[$branch], $animal ];
25 $tree{$question}[$branch] = $diff;
26 $question = $root
27 }
28 } while yes("Do you want to play again");
1 #!/usr/bin/env perl
2 sub p{print$_[0];$l=<>;chomp$l;$l}sub a{p("$_[0]? (y/n): ")=~/^\s*y/i}$q=
3 $s="lives in the water";%t=($s=>["tiger","shark"]);do {{$v=a($q);$a=$t{$q}[$v];
4 $q=$a,redo if$t{$a};$q=$s,next if a"Is it a(n) $a";$n=p"Animal's name? : ";
5 $o=p"A question true for $n, but false for $a: ";$t{$o}
6 =[$t{$q}[$v],$n];$t{$q}[$v]=$o;$q=$s}}while a"Do you want to play again";
1 #!/usr/bin/env perl
2 sub p { print $_[0]; $l = <>; chomp $l; $l }
3 sub a { p("$_[0]? (y/n): ") =~ /^\s*y/i }
4 $q = $s = "lives in the water";
5 %t = ( $s => [ "tiger", "shark" ] );
6 do {
7 {
8 $v = a($q);
9 $a = $t{$q}[$v];
10 $q = $a, redo if $t{$a};
11 $q = $s, next if a("Is it a(n) $a");
12 $n = p("Animal's name? : ");
13 $o = p("A question true for $n, but false for $a: ");
14 $t{$o} = [ $t{$q}[$v], $n ];
15 $t{$q}[$v] = $o;
16 $q = $s
17 }
18 } while a("Do you want to play again");
1 #!/usr/bin/env perl
2 use Term::ReadLine;
3
4 use strict;
5
6 my $term = new Term::ReadLine "Animals' game";
7
8 sub prompt($) { $term->readline(shift) }
9
10 sub yes($) {
11 my $prompt = shift;
12 while ( my $answer = prompt "$prompt? (y/n): " ) {
13 return $answer =~ /^\s*((yes|y)|(no|n))\s*/i;
14 print { $term->OUT } "Please answer 'y' or 'n'\n";
15 }
16 }
17
18 sub play {
19 my $guess = shift;
20 my ($node, $branch);
21 while ( ref $guess ) {
22 $node = $guess;
23 $branch = yes $node->{question};
24 $guess = $node->{branches}[$branch];
25 }
26 return if yes "Is it a(n) $guess";
27 my $animal = prompt "Animal's name? : ";
28 my $diff = prompt "A question true for $animal" .
29 ", but false for $guess: ";
30 $node->{branches}[$branch] = { question => $diff, branches => [ $guess, $animal ] };
31 }
32
33 my $tree = { question => 'lives in the water', branches => [ 'tiger', 'shark' ] };
34 play $tree;
35 play $tree while yes "Do you want to play again";
1 #!/usr/bin/env perl
2 use QuestionNode;
3 use Term::ReadLine;
4 use IO::Handle;
5
6 use strict;
7
8 my $term = Term::ReadLine->new("Animals' game");
9
10 sub prompt($) { $term->readline(shift) }
11
12 sub yes($) {
13 my $prompt = shift;
14 while (1) {
15 my $answer = prompt("$prompt? (y/n): ");
16 return ( $2 ? 1 : 0 ) if $answer =~ /^\s*((yes|y)|(no|n))\s*/i;
17 $term->OUT->print("Please answer 'y' or 'n'\n");
18 }
19 }
20
21 sub play {
22 my $guess = shift;
23 my ( $node, $branch );
24 while ( ref $guess ) {
25 $node = $guess;
26 $branch = yes $node->question ? "yes" : "no";
27 $guess = $node->$branch;
28 }
29 return if yes "Is it a(n) $guess";
30 my $animal = prompt "Animal's name? : ";
31 my $diff = prompt
32 "A question true for $animal, but false for $guess: ";
33 $node->$branch( QuestionNode->new( $diff, $guess, $animal ) );
34 }
35
36 my $tree = new QuestionNode( 'lives in the water', 'tiger', 'shark' );
37 play $tree;
38 play $tree while yes "Do you want to play again";
39
1 package QuestionNode;
2 use Carp;
3 use strict;
4
5 sub new {
6 my ( $class, $question, $no, $yes ) = @_;
7 bless { question => $question, no => $no, yes => $yes }, ref $class || $class;
8 }
9
10 sub question {
11 my $self = shift;
12 return $self->{question} unless @_;
13 croak "question is a read only attribute";
14 }
15
16 sub yes {
17 my $self = shift;
18 return $self->{yes} unless @_;
19 return $self->{yes} = shift;
20 }
21
22 sub no {
23 my $self = shift;
24 return $self->{no} unless @_;
25 return $self->{no} = shift;
26 }
27
28 1;
1 #!/usr/bin/env perl 2 use AnimalsGame; 3 AnimalsGame->new->run;
1 package AnimalsGame;
2 use QuestionNode;
3 use Term::ReadLine;
4 use IO::Handle;
5 use base "Class::Accessor";
6 use strict;
7
8 __PACKAGE__->mk_ro_accessors(qw(tree term));
9
10 sub prompt {
11 my $self = shift;
12 $self->term->readline(shift);
13 }
14
15 sub yes {
16 my $self = shift;
17 my $prompt = shift;
18 while (1) {
19 my $answer = $self->prompt("$prompt? (y/n): ");
20 return ( $2 ? 1 : 0 ) if $answer =~ /^\s*((yes|y)|(no|n))\s*/i;
21 $self->term->OUT->print("Please answer 'y' or 'n'\n");
22 }
23 }
24
25 sub play {
26 my $self = shift;
27 my $guess = $self->tree;
28 my ( $node, $branch );
29 while ( ref $guess ) {
30 $node = $guess;
31 $branch = $self->yes( $node->question ) ? "yes" : "no";
32 $guess = $node->$branch;
33 }
34 return if $self->yes("Is it a(n) $guess");
35 my $animal = $self->prompt("Animal's name? : ");
36 my $diff = $self->prompt(
37 "A question true for $animal, but false for $guess: ");
38 $node->$branch( QuestionNode->new(
39 { question => $diff, no => $guess, yes => $animal } ) );
40 }
41
42 sub new {
43 my $class = shift;
44 my $opt = shift || {};
45 my $title = $opt->{title} || "Animals' game";
46 my $term = $opt->{term} || Term::ReadLine->new($title);
47 my $tree = $opt->{tree} || QuestionNode->new(
48 { question => 'lives in the water', no => 'tiger', yes => 'shark' } );
49 return $class->SUPER::new( { tree => $tree, term => $term } );
50 }
51
52 sub run {
53 my $self = shift;
54 $self->play;
55 $self->play while $self->yes("Do you want to play again");
56 }
57
58 1;
1 package QuestionNode;
2 use base "Class::Accessor";
3 use strict;
4
5 __PACKAGE__->mk_ro_accessors("question");
6 __PACKAGE__->mk_accessors("yes", "no");
7
8 1;
1 #!/usr/bin/env perl 2 package Game; 3 use Moose; 4 5 extends qw(AnimalsGame ConsoleGame); 6 7 __PACKAGE__->meta->make_immutable; 8 no Moose; 9 10 Game->new->run;
1 package AnimalsGame;
2 use Moose;
3 use QuestionNode;
4
5 has tree => (
6 is => "ro",
7 isa => "QuestionNode",
8 default => sub {
9 QuestionNode->new(
10 { question => 'lives in the water', no => 'tiger', yes => 'shark' } );
11 }
12 );
13
14 sub play {
15 my $self = shift;
16 my $guess = $self->tree;
17 my ( $node, $branch );
18 while ( ref($guess) ) {
19 $node = $guess;
20 $branch = $self->yes( $node->question ) ? "yes" : "no";
21 $guess = $node->$branch;
22 }
23 return if $self->yes("Is it a(n) $guess");
24 my $animal = $self->prompt("Animal's name? : ");
25 my $diff = $self->prompt(
26 "A question true for $animal, but false for $guess: ");
27 $node->$branch(
28 QuestionNode->new( { question => $diff, no => $guess, yes => $animal } ) );
29 }
30
31 __PACKAGE__->meta->make_immutable;
32 1;
1 package ConsoleGame;
2 use Moose;
3 use Term::ReadLine;
4 use IO::Handle;
5
6 has title => ( is => "ro", isa => "Str", default => "Animals' game" );
7 has term => ( is => "ro", isa => "Object", lazy_build => 1,
8 handles => { prompt => "readline" } );
9
10 sub _build_term {
11 my $self = shift;
12 Term::ReadLine->new( $self->title );
13 }
14
15 sub yes {
16 my $self = shift;
17 my $prompt = shift;
18 while (1) {
19 my $answer = $self->prompt("$prompt? (y/n): ");
20 return ( $2 ? 1 : 0 ) if $answer =~ /^\s*((yes|y)|(no|n))\s*/i;
21 $self->term->OUT->print("Please answer 'y' or 'n'\n");
22 }
23 }
24
25 sub run {
26 my $self = shift;
27 $self->play;
28 $self->play while $self->yes("Do you want to play again");
29 }
30
31 __PACKAGE__->meta->make_immutable;
32 1;
1 package QuestionNode; 2 use Moose; 3 4 has question => ( is => "ro", isa => "Str", required => 1 ); 5 has [ "yes", "no" ] => ( is => "rw", isa => "Str|QuestionNode", required => 1 ); 6 7 __PACKAGE__->meta->make_immutable; 8 1;
5 with qw(AnimalsGame ConsoleGame);
2 use Moose::Role;