Morpion solitaire: Difference between revisions

m
→‎{{header|Perl}}: future-proof for 5.36, use new bitwise string operator
m (syntax highlighting fixup automation)
m (→‎{{header|Perl}}: future-proof for 5.36, use new bitwise string operator)
Line 994:
Picks a move at random from all possible moves at each step. A sample game is shown.
The largest score found so far (from just random play) is 92, also shown below.
<syntaxhighlight lang="perl">#!/usr/bin/perluse strict;
 
use strict; # https://rosettacode.org/wiki/Morpion_solitaire
use warnings;
use List::Util qw( none );
Line 1,145 ⟶ 1,143:
move count: 92
</pre>
 
A faster, shorter version without the single step display.
<br>
Uses the same kind of block shift/or technology I used in "Forest fire" and have used
for Conway's Life.
<syntaxhighlight lang="perl">#!/usr/bin/perluse strict;
 
use strict; # https://rosettacode.org/wiki/Morpion_solitaire
use warnings;
use List::Utilfeature qw( none )'bitwise';
use List::Util 'none';
 
local $_ = <<END;
Line 1,169 ⟶ 1,166:
$_ = tr/X./ /r . tr/./ /r . tr/X./ /r; # expand to 30x30 and spaces
 
my($count, @moves, %used) = 0;
my %used;
my $count = 0;
while( 1 )
{
Line 1,177 ⟶ 1,172:
for my $i ( 1, 30 .. 32 ) # directions 1 - 30 / 31 | 32 \
{
my $combined = tr/X \n/A\0/r |.
(substr $_, $i) =~ tr/X \n/B\0/r |.
(substr $_, 2 * $i) =~ tr/X \n/D\0/r |.
(substr $_, 3 * $i) =~ tr/X \n/H\0/r |.
(substr $_, 4 * $i) =~ tr/X \n/P\0/r;
while( $combined =~ /[OW\[\]\^]/g ) # exactly four Xs and one space
2,392

edits