in reply to Re^3: Feedback for programming a UI in Perl
in thread Feedback for programming a UI in Perl
I wrote a Battleship generator for Battleship solitaire puzzle generator
Then I thought about this node and added the Tk portion for a better display, also clicking
a button exposes the data under it.
#!/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]; transpose; } 0.5 < rand and transpose; # for random direction start print $sea; my @chars = $sea =~ /./g; use Tk; # for http://perlmonks.org/?node_id=1170638 my $mw = new MainWindow; for my $y (0..9) { for my $x (0..9) { my $char = shift @chars; my $b = $mw->Button( -font => 'courier 24', -text => ' ', )->grid(-row => $y, -column => $x); $b->configure(-command => sub {$b->configure(-text => $char) }); } } MainLoop;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Feedback for programming a UI in Perl
by Anonymous Monk on Aug 30, 2016 at 20:21 UTC | |
by Anonymous Monk on Aug 31, 2016 at 00:11 UTC | |
by Anonymous Monk on Sep 01, 2016 at 15:25 UTC | |
by Anonymous Monk on Sep 01, 2016 at 19:17 UTC |