Tic-tac-toe: Difference between revisions

(→‎{{header|J}}: inserted a blindfolded variant---requires thought though easier than blindfolded chess.)
Line 1,073:
nor=: 8 b.
infix_pairs_agree=: 2&(=/\)
</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo| 2011.11}}
The computer plays a random game.
Output looks a lot like the output of the better python version.
<lang Perl6>
use v6;
 
my @board = 1..9;
my @winning-positions = [0..2], [3..5], [6..8], [0,3,6], [1,4,7], [2,5,8],
[0,4,8], [6,4,2];
 
sub get-winner() {
for @winning-positions {
return (@board[$_][0], $_) if [eq] @board[$_];
}
}
 
sub free-indexes() {
@board.keys.grep: { @board[$_] eq any(1..9) }
}
 
sub ai-move() {
given free-indexes.pick {
@board[$_] = 'o';
say "I go at: { $_ + 1 }\n";
}
}
 
sub print-board() {
say @board.map({ "$^a | $^b | $^c" }).join("\n--+---+--\n"), "\n";
}
 
sub human-move() {
my $pos = prompt "Choose one of { (free-indexes >>+>> 1).join(",") }: ";
if $pos eq any(free-indexes >>+>> 1) {
@board[$pos - 1] = 'x';
} else {
say "Sorry, you want to put your 'x' where?";
human-move();
}
}
 
for (&ai-move, &human-move) xx * {
print-board;
.();
last if get-winner() or not free-indexes;
}
 
if get-winner() -> ($player, $across) {
say "$player wins across [", ($across >>+>> 1).join(", "), "].";
} else {
say "How boring, a draw!";
}
</lang>
 
Anonymous user