Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm pretty sure this won't run on Pugs, but it does have more Perl6ish features.
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 (@g ==> grep { $_ eq '.' }) { INPUT: { say %player{$player} ," Enter the Position [1-9]:"; my $in = =$IN; unless ($in == $choice) { say "Please enter a value within 1-9"; redo INPUT; } my $idx = $in - 1; if (%entered.exists($idx)) { say "Element already entered at $in"; redo INPUT; } @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]] eq @g[@c[1]] eq @g[@c[2]] ) say " %player{$player} Wins \n"; exit; } } print_board( @g ); $player = $player eq 'X' ?? 'O' :: 'X'; }

The Perfect is the Enemy of the Good.


In reply to Re: Pugs Tic Tac by dragonchild
in thread Pugs Tic Tac by mkirank

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-19 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found