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 ) ); }
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.