in reply to Re^2: Battleship solitaire puzzle generator
in thread Battleship solitaire puzzle generator
Thanks. In my haste to get regexing I didn't read the rules very closely.
Easily fixed, though:
#!/usr/bin/perl # http://perlmonks.org/?node_id=1170733 use strict; use warnings; my $sea = ((' ' x 10) . "\n") x 10; sub transpose { local $_ = $sea; tr/<>^v/^v<>/; $sea = ''; $sea .= "\n" while s/^(.)/ $sea .= $1; '' /gem; } for my $ship ( 4,3,3,2,2,2,1,1,1,1 ) { my @places; push @places, $-[0] while $sea =~ /(?= {$ship})/g; substr $sea, $places[rand @places], $ship, ('O', '<>', '<#>', '<##>' )[$ship - 1]; for (0, 9, 11, 0, 9, 11) { $sea =~ s/(?<=[<>^v#O].{$_}) | (?=.{$_}[<>^v#O])/~/gs; transpose; } rand > 0.5 and transpose; } tr/ /~/ for $sea; print $sea; my @chars = $sea =~ /./g; use Tk; # for http://perlmonks.org/?node_id=1170638 my $mw = MainWindow->new( -title => "Battleship" ); for my $y (0..9) { for my $x (0..9) { my ($char, $b) = shift @chars; $b = $mw->Button( -font => 'courier 24', -text => ' ', -command => sub {$b->configure(-text => $char) }, )->grid(-row => $y, -column => $x); } } MainLoop;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Battleship solitaire puzzle generator
by toolic (Bishop) on Aug 30, 2016 at 17:10 UTC | |
by Anonymous Monk on Aug 30, 2016 at 23:58 UTC | |
by toolic (Bishop) on Sep 02, 2016 at 13:55 UTC | |
by Anonymous Monk on Sep 02, 2016 at 14:23 UTC | |
by Anonymous Monk on Aug 30, 2016 at 17:26 UTC | |
by Anonymous Monk on Aug 30, 2016 at 20:10 UTC |