Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Pugs Tic Tac

by mkirank (Chaplain)
on Apr 25, 2005 at 16:24 UTC ( #451261=perlquestion: print w/ replies, xml ) Need Help??
mkirank has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
I have successfully written my first perl 6 code :-)
Please feel free to make changes and then someone with commit access can put it to the pugs/examples/games directory
#!/usr/bin/pugs use v6; my @g = ('.') xx 9; my $cnt = 0; for (@g)->$x { print "$x \t"; print "\n" if ( !( ++$cnt % 3 ) ); } my $player; my %player = ('X','Player 1','O','Player 2'); my %entered; my $choice = any (1 .. 9); my $tmp = 0; while ($tmp < 9) { $player = ( $tmp % 2 ) ?? 'O' :: 'X'; say %player{$player} ," Enter the Position [1-9]:"; my $in = =$IN; if (!$in ~~ rx:perl5/[1-9]/) { say " Please enter digits from 1-9 \n"; eval 'next'; } if ($in == $choice) { $in --; if (%entered.exists($in)) { say "Element already entered at $in"; eval 'next'; } %entered{$in} ++; $tmp ++; } else { say "Please enter a value within 1-9"; $tmp --; eval 'next'; } @g[$in] = $player; for ( [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ], [ 0, 3, 6 ], [ 1, 4, 7 ], [ 2, 5, 8 ], [ 0, 4, 8 ], [ 2, 4, 6 ] ) -> @c { if (join ('',@g[@c]) ~~ rx:perl5/([XO])\1\1/) { say " %player{$player} Wins \n"; exit; } } for (@g) { print $_; print "\n" if ( !( ++ $cnt % 3 ) ); } }

Comment on Pugs Tic Tac
Download Code
Re: Pugs Tic Tac
by stvn (Monsignor) on Apr 25, 2005 at 16:36 UTC
Re: Pugs Tic Tac
by jacques (Priest) on Apr 25, 2005 at 17:00 UTC
    $player = ( $tmp % 2 ) ?? 'O' :: 'X';

    Yuck. I want my old trinary operator back.

      Yuck. I want my old trinary operator back.

      You use it often enough that The addition of two extra characters makes you say "yuck"? I mean, it works exactly the same. It looks nearly exactly the same. It just has two of each character instead of one.

      Update: don't want my position to hinge on how often people use the operator.

        What has the single ''?'' been usurped for?? I can''t remember.. Or is the problem that ""everyone wants the colon"",, so a doubled colon was needed,, leading to a doubled question mark??

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

        You use it often enough that two extra characters makes you say "yuck"?

        I do.

        I mean, it works exactly the same. It looks nearly exactly the same. It just has two of each character instead of one.

        It's an increase of 100%, you know? :-)

        But please note: I'm not saying that I'm against the change or anything. With time, I'll probably enjoy it. I just wanted to make it perfectly clear that some people use the ternary operator a lot.

        And I'm proudly one of them :-)

        print "$users user", ( $users == 1 ? '' : 's'), " currently use the ternary operator.\n";

Re: Pugs Tic Tac
by eric256 (Parson) on Apr 25, 2005 at 18:11 UTC

    For my own sake, and others I hope, why do you have to do  ~~ rx:perl5/([XO])\1\1/ instead of just plain old =~ /([XO])\1\1/?


    ___________
    Eric Hodges
      For my own sake, and others I hope, why do you have to do ~~ rx:perl5/([XO])\1\1/ instead of just plain old =~ /([XO])\1\1/?

      The old pattern-match-binding operator =~ has been usurped by the smart match operator ~~, and "regular expressions" have been reworked in Perl6 to "rules". The old regular expression engine will still be there, but you have to use the vaguely ugly rx:perl5// syntax, or rx:P5// will work too (I think). The reason he used a P5 regex in the code is because Pugs doesn't have Perl6 rules support yet.

        Yes, as of 6.2.1, P5 works, as do Perl5 and perl5.
      ~~ is called the "smart match operator" and supersedes the binding operator, =~. It allows you to use it on anything and it'll do the right thing, ie:
      $i = 1; if ( $i ~~ 1 ) { # $i == 1 $i = 'foo'; if ( $i ~~ 'foo' ) { # $i eq 'foo' $i = 'foo'; if ( $i ~~ m/fo(.)/ ) { # $1 contains 'o'
      Read the "Smart Matching" section of Synopsis 4 for details.

      rx is a replacement for qr. rx:perl5 means use perl5-compatible regular expressions. More details about rx in Synopsis 5.

        Thanks for the links. Are we sure this new language is perl? I mean it looks like perl, but it sure aint the same thing. Anyone else completly terrified by the need to learn a new language? I know it is not a need, and it does look quite cool, but I just feel like i'm getting the hang of 5.8. ;)


        ___________
        Eric Hodges
Re: Pugs Tic Tac
by dragonchild (Archbishop) on Apr 25, 2005 at 19:01 UTC
    I'm pretty sure this won't run on Pugs, but it does have more Perl6ish features.

    The Perfect is the Enemy of the Good.

      dragonchild

      I refactored your refactor to work in Pugs. It's a little hackish in parts, but it does work :)

      I committed it to svn http://svn.openfoundry.org/pugs/examples/games/tic_tac_toe.p6 and added some POD so as to give credit where credit is due.

      #!/usr/bin/pugs use v6; sub print_board ( @b ) { my $count = 0; for (@b) -> $x { print "$x\t"; print "\n" unless ++$count % 3; } } my @g = ('.') xx 9; print_board( @g ); my %player = ('X','Player 1','O','Player 2'); my %entered; my $choice = any (1 .. 9); my $player = 'X'; while (grep { $_ eq '.' } @g) { say %player{$player} ~ " Enter the Position [1-9]:"; my $in = =$IN; unless ($in == $choice) { say "Please enter a value within 1-9"; } else { my $idx = $in - 1; if (%entered.exists($idx)) { say "Element already entered at $in"; } else { @g[$idx] = $player; %entered{$idx}++; for ( [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ], [ 0, 3, 6 ], [ 1, 4, 7 ], [ 2, 5, 8 ], [ 0, 4, 8 ], [ 2, 4, 6 ] ) -> $c { if (@g[$c[0]] ne '.' && @g[$c[0]] eq @g[$c[1]] eq @g[$ +c[2]]) { print_board( @g ); say " " ~ %player{$player} ~ " Wins \n"; exit(); } } print_board( @g ); $player = $player eq 'X' ?? 'O' :: 'X'; } } } =pod =head1 NAME tic_tac_toe.p6 - Tic-Tac-Toe =head1 DESCRIPTION This is a perl6 implementation of the classic Tic-Tac-Toe game. =head1 AUTHORS mkirank L<http://www.perlmonks.org/index.pl?node_id=451261> rob kinyon L<http://www.perlmonks.org/index.pl?node_id=451302> stevan little, E<lt>stevan@iinteractive.comE<gt> =cut
      BTW - if either of you want commit access, just ask :)

      -stvn
Re: Pugs Tic Tac
by stvn (Monsignor) on Apr 26, 2005 at 03:45 UTC
    mkirank, dragonchild,

    I have refactored the code to be even more perl6-ish. Some of the changes include using a junction instead of the %entered hash, adding argument and return type signatures to the print_board function, and improving the 'for' loop to use multiple topics (for (@b) -> $x, $y, $z { }) when printing the board.

    You can see the changes here.

    -stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://451261]
Approved by moot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-08-22 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (158 votes), past polls