Sudoku.pm: Simplifying handling and functionality, slurping stylesheet instead of heredoc, Fixing maxlength on input boxes

master
Rob Emery 2013-11-16 20:19:15 +00:00
parent 358bf94e90
commit 107999af11
3 changed files with 60 additions and 41 deletions

View File

@ -8,14 +8,15 @@ use strict;
use warnings;
triggers start => 'sudoku';
triggers start => 'i\'m bored';
triggers end => 'sudoku';
triggers start => "i'm bored";
triggers start => 'i am bored';
triggers start => 'this is boring';
zci is_cached => 0;
zci answer_type => "sudoku";
primary_example_queries 'sudoku 4x4 easy';
primary_example_queries 'sudoku easy';
secondary_example_queries 'I am bored';
description 'show a little sudoku you can play in the browser';
name 'Sudoku';
@ -24,29 +25,23 @@ category 'random';
attribution github => ['https://github.com/DrDub', 'DrDub'],
web => ['http://duboue.net/', 'Pablo Duboue'];
handle query => sub {
my($number, $difficulty) = m/^sudoku ([49])?(?:x[49])?\s*((easy)|(medium)|(hard))?$/;
$number = 9 unless ($number);
handle remainder => sub {
my($difficulty) = m/^((average)|(medium)|(hard))?$/;
$difficulty = "easy" unless ($difficulty);
if(/bored|boring/i){
$number = 9;
$difficulty = "hard";
}
my $sudoku = Games::Sudoku::Component->new(size=>$number);
my $blanks = 0.25;
if ($difficulty eq "hard")
{
$blanks = 0.75;
}
elsif($difficulty eq "medium")
{
$blanks = 0.5;
}
my $sudoku = Games::Sudoku::Component->new(size => 9);
$sudoku->generate(blanks => ($number ** 2) * $blanks);
my $str_output = $sudoku->as_string(separator => " ", linebreak => "\n");
#proportion of the grid to be blank
my $blanks = 0.25;
$blanks = 0.75 if ($difficulty eq "hard");
$blanks = 0.5 if ($difficulty eq "medium" || $difficulty eq "average");
$sudoku->generate(blanks => (9 ** 2) * $blanks);
my $str_output = $sudoku->as_string();
#switch 0 to more sensible placeholders
$str_output =~ s/0/_/g;
@ -59,33 +54,13 @@ handle query => sub {
my @chars = split(/ /, $line);
for my $char (@chars)
{
$char = "<input length='1'/>" if $char eq "_";
$char = "<input maxlength='1'/>" if $char eq "_";
}
$html_table .= "<tr><td>" . join("</td><td>", @chars) . "</td></tr>\n";
}
my $inline_style = "
table.sudoku td {
width: 1.5em;
height: 1.5em;
text-align: center;
vertical-align: middle;
border: 1px solid rgba(230,230,230,1);
}
table.sudoku tr:nth-of-type(3) td, table.sudoku tr:nth-of-type(6) td
{
border-bottom: 2px solid rgba(200,200,200,1);
}
table.sudoku td:nth-of-type(3), table.sudoku td:nth-of-type(6)
{
border-right: 2px solid rgba(200,200,200,1);
}
table.sudoku input {
border: 0px;
width: 1em;
}
";
my $html_output = "<style>" . $inline_style . "</style><table class='sudoku'>\n" . $html_table . "</table>";
my $stylesheet = scalar share("style.css")->slurp;
my $html_output = "<style type='text/css'>$stylesheet</style> \n<table class='sudoku'>\n" . $html_table . "</table>";
return $str_output, html => $html_output;
};

View File

@ -0,0 +1,22 @@
table.sudoku td {
width: 1.5em;
height: 1.5em;
text-align: center;
vertical-align: middle;
border: 1px solid rgba(136, 136, 136, 0.478);
}
table.sudoku tr:nth-of-type(3) td, table.sudoku tr:nth-of-type(6) td
{
border-bottom: 3px solid rgba(136, 136, 136, 0.478);
}
table.sudoku td:nth-of-type(3), table.sudoku td:nth-of-type(6)
{
border-right: 3px solid rgba(136, 136, 136, 0.478);
}
table.sudoku input {
border: 0px;
width: 1em;
}

22
t/Sudoku.t Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => 'sudoku';
zci is_cached => 0;
ddg_goodie_test(
[
'DDG::Goodie::Sudoku'
],
"sudoku" => test_zci(
qr/^[0-9_].*[0-9_]$/s,
html => qr/.*\<table.*\<\/table\>.*/s,
),
);
done_testing;