#!/usr/local/bin/perl -w
# Example script using Games::Boggle::Board
# Play a game of cgi-Boggle
use strict;
use HTML::Template;
my $template;
use CGI qw(param header);
if ( param('board') && param('words')) {
$template = HTML::Template->new(filename=>'boggle-words.template')
or die;
my $brd = build_board_array(param('board'));
$template->param( board => $brd );
use Games::Boggle;
my $board = Games::Boggle->new(param('board'));
my %scores = (
3 => 1,
4 => 1,
5 => 2,
6 => 3,
7 => 5,
8 => 11,
9 => 11,
10 => 11,
11 => 11,
12 => 11,
13 => 11,
14 => 11,
15 => 11,
16 => 11
);
my (@good, @bad);
my $total_score = 0;
foreach my $word
( keys %{{ map {$_ => 1} split /\s+/, param('words') }} ) {
next if $word =~ /\W/; # watch for nasties
if ( length($word)>2
&& $board->has_word($word)
## && grep( /^$word$/, `look $word`)
) {
push (@good, {word=>$word});
$total_score += $scores{length($word)};
}
else { push (@bad, {word => $word}) }
}
$template->param( good => \@good );
$template->param( bad => \@bad );
$template->param( score => $total_score );
}
else {
$template = HTML::Template->new(filename=>'boggle.template')
or die;
use Games::Boggle::Board;
my $b = Games::Boggle::Board->new;
$template->param( board_string => $b->as_string );
my $board = build_board_array($b->as_string);
$template->param( board => $board );
}
print header, $template->output();
# ++ to merlyn
sub build_board_array {
my @board = split //, shift;
s/Q/Qu/ foreach @board;
@board = map {
+{ line => [map +{ letter => $_ }, splice @board, 0, 4 ]};
} 1..4;
return \@board;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.