Merge pull request #1057 from rouzbeh/master

Add FenViewer IA
master
Zaahir Moolla 2015-03-31 19:13:00 -04:00
commit 957c4ddda6
3 changed files with 208 additions and 0 deletions

161
lib/DDG/Goodie/FenViewer.pm Normal file
View File

@ -0,0 +1,161 @@
package DDG::Goodie::FenViewer;
# ABSTRACT: This instant answer parses a chess position in the Forsyth-Edwards notation,
# and draws a chessboard on screen representing that position. The current version only
# parses the current board position (the first field in the FEN format) and does not check
# that the given position is actually legal.
use DDG::Goodie;
use strict;
use Scalar::Util qw(looks_like_number);
use Try::Tiny;
zci answer_type => "fen_viewer";
zci is_cached => 1;
name "FenViewer";
description "This instant answer parses a chess position in the Forsyth-Edwards notation, and draws a chessboard on screen representing that position.";
primary_example_queries "FEN rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "fen rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1";
category "entertainment";
topics "gaming";
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/FENViewer.pm";
attribution github => ["rouzbeh", "Ali Neishabouri"],
twitter => "Rou7_beh";
triggers start => "fen";
# Parse the FEN string into an array of length 64.
sub parse_position {
my ($i) = 0;
my ($position) = @_;
$position =~ s/^\s+|\s+$//g;
my (@cases) = ();
for (my $char = 0 ; $char < length($position) ; $char++ ) {
my $fenchar = substr($position, $char, 1);
if ($fenchar eq ' ') {
return @cases;
}
if (looks_like_number($fenchar)) {
for ($i = 0; $i < $fenchar; $i++){
push(@cases, 'e');
}
}
elsif ($fenchar ne '/') {
push(@cases, $fenchar);
}
}
return @cases;
}
# Generate a chessboard as a HTML table.
sub draw_chessboard_html {
my (@position) = @_;
my ($i) = 0;
my ($j) = 0;
my ($counter) = 0;
my (@arr) = ("A".."Z");
my (%class_dict) = (
'r' => 'black rook',
'n' => 'black knight',
'b' => 'black bishop',
'q' => 'black queen',
'k' => 'black king',
'p' => 'black pawn',
'e' => 'empty',
'R' => 'white rook',
'N' => 'white knight',
'B' => 'white bishop',
'Q' => 'white queen',
'K' => 'white king',
'P' => 'white pawn',
);
my (%unicode_dict) = (
'r' => '&#9820;',
'n' => '&#9822;',
'b' => '&#9821;',
'q' => '&#9819;',
'k' => '&#9818;',
'p' => '&#9823;',
'e' => '',
'R' => '&#9814;',
'N' => '&#9816;',
'B' => '&#9815;',
'Q' => '&#9813;',
'K' => '&#9812;',
'P' => '&#9817;',
);
my ($html_chessboard) = '<div class="zci--fenviewer"><table class="chess_board" cellpadding="0" cellspacing="0">';
for ($i = 0; $i < 8; $i++){
# Rows
$html_chessboard .= '<tr>';
for ($j = 0; $j < 8; $j++){
# Columns
$html_chessboard .= '<td id="'.$arr[$j].(8-$i).'">';
$html_chessboard .= '<a href="#" class="'.$class_dict{$position[$counter]};
$html_chessboard .= '">'.$unicode_dict{$position[$counter]}.'</a>';
$html_chessboard .= '</td>';
$counter++;
}
$html_chessboard .= '</tr>';
}
$html_chessboard .= '</table></div>';
return $html_chessboard;
}
# Generate a chessboard in ASCII, with the same format as
# 'text output from Chess::PGN::EPD
sub draw_chessboard_ascii {
my (@position) = @_;
my ($i) = 0;
my ($j) = 0;
my ($counter) = 0;
my ($ascii_chessboard) = "";
for ($i = 0; $i < 8; $i++){
# Rows
for ($j = 0; $j < 8; $j++){
# Columns
if ($position[$counter] ne 'e') {
# Occupied square
$ascii_chessboard .= $position[$counter];
}
elsif ($j % 2 != $i % 2) {
# Black square
$ascii_chessboard .= '-';
}
else {
# White square
$ascii_chessboard .= ' ';
}
$counter++;
}
if($counter < 63) {
$ascii_chessboard .= "\n";
}
}
return $ascii_chessboard;
}
handle remainder => sub {
my ($query) = $_;
return unless $query;
my (@pos) = parse_position($query);
if ($#pos != 63) {
# The format is wrong, e.g. space in the middle
return;
}
my ($html_out) = '';
my ($ascii_out) = '';
try {
$html_out = draw_chessboard_html(@pos);
$ascii_out = draw_chessboard_ascii(@pos);
}
catch {
# The format is wrong, e.g. non-existent piece
return;
};
return $ascii_out, html => $html_out;
};
1;

View File

@ -0,0 +1,24 @@
.zci--answer .zci--fenviewer a {
color:#333;
display:block;
font-size:27px;
height:40px;
position:relative;
text-decoration:none;
width:40px;
line-height: 1.5;
}
.zci--answer .zci--fenviewer .chess_board { border: 2px solid #333; }
.zci--answer .zci--fenviewer .chess_board td {
background:#fff;
height:40px;
text-align:center;
vertical-align:middle;
width:40px;
}
.zci--answer .zci--fenviewer .chess_board tr:nth-child(odd) td:nth-child(even),
.zci--answer .zci--fenviewer .chess_board tr:nth-child(even) td:nth-child(odd) {
background-color: #ddd;
}

23
t/FenViewer.t Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => "fen_viewer";
zci is_cached => 1;
ddg_goodie_test(
[qw( DDG::Goodie::FenViewer )],
'FEN rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' => test_zci("rnbqkbnr\npppppppp\n - - - -\n- - - - \n - - - -\n- - - - \nPPPPPPPP\nRNBQKBNR", html => '<div class="zci--fenviewer"><table class="chess_board" cellpadding="0" cellspacing="0"><tr><td id="A8"><a href="#" class="black rook">&#9820;</a></td><td id="B8"><a href="#" class="black knight">&#9822;</a></td><td id="C8"><a href="#" class="black bishop">&#9821;</a></td><td id="D8"><a href="#" class="black queen">&#9819;</a></td><td id="E8"><a href="#" class="black king">&#9818;</a></td><td id="F8"><a href="#" class="black bishop">&#9821;</a></td><td id="G8"><a href="#" class="black knight">&#9822;</a></td><td id="H8"><a href="#" class="black rook">&#9820;</a></td></tr><tr><td id="A7"><a href="#" class="black pawn">&#9823;</a></td><td id="B7"><a href="#" class="black pawn">&#9823;</a></td><td id="C7"><a href="#" class="black pawn">&#9823;</a></td><td id="D7"><a href="#" class="black pawn">&#9823;</a></td><td id="E7"><a href="#" class="black pawn">&#9823;</a></td><td id="F7"><a href="#" class="black pawn">&#9823;</a></td><td id="G7"><a href="#" class="black pawn">&#9823;</a></td><td id="H7"><a href="#" class="black pawn">&#9823;</a></td></tr><tr><td id="A6"><a href="#" class="empty"></a></td><td id="B6"><a href="#" class="empty"></a></td><td id="C6"><a href="#" class="empty"></a></td><td id="D6"><a href="#" class="empty"></a></td><td id="E6"><a href="#" class="empty"></a></td><td id="F6"><a href="#" class="empty"></a></td><td id="G6"><a href="#" class="empty"></a></td><td id="H6"><a href="#" class="empty"></a></td></tr><tr><td id="A5"><a href="#" class="empty"></a></td><td id="B5"><a href="#" class="empty"></a></td><td id="C5"><a href="#" class="empty"></a></td><td id="D5"><a href="#" class="empty"></a></td><td id="E5"><a href="#" class="empty"></a></td><td id="F5"><a href="#" class="empty"></a></td><td id="G5"><a href="#" class="empty"></a></td><td id="H5"><a href="#" class="empty"></a></td></tr><tr><td id="A4"><a href="#" class="empty"></a></td><td id="B4"><a href="#" class="empty"></a></td><td id="C4"><a href="#" class="empty"></a></td><td id="D4"><a href="#" class="empty"></a></td><td id="E4"><a href="#" class="empty"></a></td><td id="F4"><a href="#" class="empty"></a></td><td id="G4"><a href="#" class="empty"></a></td><td id="H4"><a href="#" class="empty"></a></td></tr><tr><td id="A3"><a href="#" class="empty"></a></td><td id="B3"><a href="#" class="empty"></a></td><td id="C3"><a href="#" class="empty"></a></td><td id="D3"><a href="#" class="empty"></a></td><td id="E3"><a href="#" class="empty"></a></td><td id="F3"><a href="#" class="empty"></a></td><td id="G3"><a href="#" class="empty"></a></td><td id="H3"><a href="#" class="empty"></a></td></tr><tr><td id="A2"><a href="#" class="white pawn">&#9817;</a></td><td id="B2"><a href="#" class="white pawn">&#9817;</a></td><td id="C2"><a href="#" class="white pawn">&#9817;</a></td><td id="D2"><a href="#" class="white pawn">&#9817;</a></td><td id="E2"><a href="#" class="white pawn">&#9817;</a></td><td id="F2"><a href="#" class="white pawn">&#9817;</a></td><td id="G2"><a href="#" class="white pawn">&#9817;</a></td><td id="H2"><a href="#" class="white pawn">&#9817;</a></td></tr><tr><td id="A1"><a href="#" class="white rook">&#9814;</a></td><td id="B1"><a href="#" class="white knight">&#9816;</a></td><td id="C1"><a href="#" class="white bishop">&#9815;</a></td><td id="D1"><a href="#" class="white queen">&#9813;</a></td><td id="E1"><a href="#" class="white king">&#9812;</a></td><td id="F1"><a href="#" class="white bishop">&#9815;</a></td><td id="G1"><a href="#" class="white knight">&#9816;</a></td><td id="H1"><a href="#" class="white rook">&#9814;</a></td></tr></table></div>'),
'fen rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1 ' => test_zci("rnbqkbnr\npppppppp\n - - - -\n- - - - \n - -P- -\n- - - - \nPPPP PPP\nRNBQKBNR", html => '<div class="zci--fenviewer"><table class="chess_board" cellpadding="0" cellspacing="0"><tr><td id="A8"><a href="#" class="black rook">&#9820;</a></td><td id="B8"><a href="#" class="black knight">&#9822;</a></td><td id="C8"><a href="#" class="black bishop">&#9821;</a></td><td id="D8"><a href="#" class="black queen">&#9819;</a></td><td id="E8"><a href="#" class="black king">&#9818;</a></td><td id="F8"><a href="#" class="black bishop">&#9821;</a></td><td id="G8"><a href="#" class="black knight">&#9822;</a></td><td id="H8"><a href="#" class="black rook">&#9820;</a></td></tr><tr><td id="A7"><a href="#" class="black pawn">&#9823;</a></td><td id="B7"><a href="#" class="black pawn">&#9823;</a></td><td id="C7"><a href="#" class="black pawn">&#9823;</a></td><td id="D7"><a href="#" class="black pawn">&#9823;</a></td><td id="E7"><a href="#" class="black pawn">&#9823;</a></td><td id="F7"><a href="#" class="black pawn">&#9823;</a></td><td id="G7"><a href="#" class="black pawn">&#9823;</a></td><td id="H7"><a href="#" class="black pawn">&#9823;</a></td></tr><tr><td id="A6"><a href="#" class="empty"></a></td><td id="B6"><a href="#" class="empty"></a></td><td id="C6"><a href="#" class="empty"></a></td><td id="D6"><a href="#" class="empty"></a></td><td id="E6"><a href="#" class="empty"></a></td><td id="F6"><a href="#" class="empty"></a></td><td id="G6"><a href="#" class="empty"></a></td><td id="H6"><a href="#" class="empty"></a></td></tr><tr><td id="A5"><a href="#" class="empty"></a></td><td id="B5"><a href="#" class="empty"></a></td><td id="C5"><a href="#" class="empty"></a></td><td id="D5"><a href="#" class="empty"></a></td><td id="E5"><a href="#" class="empty"></a></td><td id="F5"><a href="#" class="empty"></a></td><td id="G5"><a href="#" class="empty"></a></td><td id="H5"><a href="#" class="empty"></a></td></tr><tr><td id="A4"><a href="#" class="empty"></a></td><td id="B4"><a href="#" class="empty"></a></td><td id="C4"><a href="#" class="empty"></a></td><td id="D4"><a href="#" class="empty"></a></td><td id="E4"><a href="#" class="white pawn">&#9817;</a></td><td id="F4"><a href="#" class="empty"></a></td><td id="G4"><a href="#" class="empty"></a></td><td id="H4"><a href="#" class="empty"></a></td></tr><tr><td id="A3"><a href="#" class="empty"></a></td><td id="B3"><a href="#" class="empty"></a></td><td id="C3"><a href="#" class="empty"></a></td><td id="D3"><a href="#" class="empty"></a></td><td id="E3"><a href="#" class="empty"></a></td><td id="F3"><a href="#" class="empty"></a></td><td id="G3"><a href="#" class="empty"></a></td><td id="H3"><a href="#" class="empty"></a></td></tr><tr><td id="A2"><a href="#" class="white pawn">&#9817;</a></td><td id="B2"><a href="#" class="white pawn">&#9817;</a></td><td id="C2"><a href="#" class="white pawn">&#9817;</a></td><td id="D2"><a href="#" class="white pawn">&#9817;</a></td><td id="E2"><a href="#" class="empty"></a></td><td id="F2"><a href="#" class="white pawn">&#9817;</a></td><td id="G2"><a href="#" class="white pawn">&#9817;</a></td><td id="H2"><a href="#" class="white pawn">&#9817;</a></td></tr><tr><td id="A1"><a href="#" class="white rook">&#9814;</a></td><td id="B1"><a href="#" class="white knight">&#9816;</a></td><td id="C1"><a href="#" class="white bishop">&#9815;</a></td><td id="D1"><a href="#" class="white queen">&#9813;</a></td><td id="E1"><a href="#" class="white king">&#9812;</a></td><td id="F1"><a href="#" class="white bishop">&#9815;</a></td><td id="G1"><a href="#" class="white knight">&#9816;</a></td><td id="H1"><a href="#" class="white rook">&#9814;</a></td></tr></table></div>'),
# Two examples from the last world championship
'fen r1b1r1k1/2qn1ppp/1ppp4/p3p3/P2PP3/R4N1P/1PP2PP1/2BQR1K1 b - - 0 14 ' => test_zci("r-b-r-k-\n- qn-ppp\n ppp - -\np - p - \nP- PP- -\nR - -N-P\n PP- PP-\n- BQR K ", html => '<div class="zci--fenviewer"><table class="chess_board" cellpadding="0" cellspacing="0"><tr><td id="A8"><a href="#" class="black rook">&#9820;</a></td><td id="B8"><a href="#" class="empty"></a></td><td id="C8"><a href="#" class="black bishop">&#9821;</a></td><td id="D8"><a href="#" class="empty"></a></td><td id="E8"><a href="#" class="black rook">&#9820;</a></td><td id="F8"><a href="#" class="empty"></a></td><td id="G8"><a href="#" class="black king">&#9818;</a></td><td id="H8"><a href="#" class="empty"></a></td></tr><tr><td id="A7"><a href="#" class="empty"></a></td><td id="B7"><a href="#" class="empty"></a></td><td id="C7"><a href="#" class="black queen">&#9819;</a></td><td id="D7"><a href="#" class="black knight">&#9822;</a></td><td id="E7"><a href="#" class="empty"></a></td><td id="F7"><a href="#" class="black pawn">&#9823;</a></td><td id="G7"><a href="#" class="black pawn">&#9823;</a></td><td id="H7"><a href="#" class="black pawn">&#9823;</a></td></tr><tr><td id="A6"><a href="#" class="empty"></a></td><td id="B6"><a href="#" class="black pawn">&#9823;</a></td><td id="C6"><a href="#" class="black pawn">&#9823;</a></td><td id="D6"><a href="#" class="black pawn">&#9823;</a></td><td id="E6"><a href="#" class="empty"></a></td><td id="F6"><a href="#" class="empty"></a></td><td id="G6"><a href="#" class="empty"></a></td><td id="H6"><a href="#" class="empty"></a></td></tr><tr><td id="A5"><a href="#" class="black pawn">&#9823;</a></td><td id="B5"><a href="#" class="empty"></a></td><td id="C5"><a href="#" class="empty"></a></td><td id="D5"><a href="#" class="empty"></a></td><td id="E5"><a href="#" class="black pawn">&#9823;</a></td><td id="F5"><a href="#" class="empty"></a></td><td id="G5"><a href="#" class="empty"></a></td><td id="H5"><a href="#" class="empty"></a></td></tr><tr><td id="A4"><a href="#" class="white pawn">&#9817;</a></td><td id="B4"><a href="#" class="empty"></a></td><td id="C4"><a href="#" class="empty"></a></td><td id="D4"><a href="#" class="white pawn">&#9817;</a></td><td id="E4"><a href="#" class="white pawn">&#9817;</a></td><td id="F4"><a href="#" class="empty"></a></td><td id="G4"><a href="#" class="empty"></a></td><td id="H4"><a href="#" class="empty"></a></td></tr><tr><td id="A3"><a href="#" class="white rook">&#9814;</a></td><td id="B3"><a href="#" class="empty"></a></td><td id="C3"><a href="#" class="empty"></a></td><td id="D3"><a href="#" class="empty"></a></td><td id="E3"><a href="#" class="empty"></a></td><td id="F3"><a href="#" class="white knight">&#9816;</a></td><td id="G3"><a href="#" class="empty"></a></td><td id="H3"><a href="#" class="white pawn">&#9817;</a></td></tr><tr><td id="A2"><a href="#" class="empty"></a></td><td id="B2"><a href="#" class="white pawn">&#9817;</a></td><td id="C2"><a href="#" class="white pawn">&#9817;</a></td><td id="D2"><a href="#" class="empty"></a></td><td id="E2"><a href="#" class="empty"></a></td><td id="F2"><a href="#" class="white pawn">&#9817;</a></td><td id="G2"><a href="#" class="white pawn">&#9817;</a></td><td id="H2"><a href="#" class="empty"></a></td></tr><tr><td id="A1"><a href="#" class="empty"></a></td><td id="B1"><a href="#" class="empty"></a></td><td id="C1"><a href="#" class="white bishop">&#9815;</a></td><td id="D1"><a href="#" class="white queen">&#9813;</a></td><td id="E1"><a href="#" class="white rook">&#9814;</a></td><td id="F1"><a href="#" class="empty"></a></td><td id="G1"><a href="#" class="white king">&#9812;</a></td><td id="H1"><a href="#" class="empty"></a></td></tr></table></div>'),
'fen 5rk1/4R1p1/3q1p2/p1p2P1p/P3Q2P/5pP1/2P2P1K/8 w - - 0 35' => test_zci(" - - rk-\n- - R p \n - q p -\np p -P-p\nP- -Q- P\n- - -pP \n -P- P K\n- - - - ", html => '<div class="zci--fenviewer"><table class="chess_board" cellpadding="0" cellspacing="0"><tr><td id="A8"><a href="#" class="empty"></a></td><td id="B8"><a href="#" class="empty"></a></td><td id="C8"><a href="#" class="empty"></a></td><td id="D8"><a href="#" class="empty"></a></td><td id="E8"><a href="#" class="empty"></a></td><td id="F8"><a href="#" class="black rook">&#9820;</a></td><td id="G8"><a href="#" class="black king">&#9818;</a></td><td id="H8"><a href="#" class="empty"></a></td></tr><tr><td id="A7"><a href="#" class="empty"></a></td><td id="B7"><a href="#" class="empty"></a></td><td id="C7"><a href="#" class="empty"></a></td><td id="D7"><a href="#" class="empty"></a></td><td id="E7"><a href="#" class="white rook">&#9814;</a></td><td id="F7"><a href="#" class="empty"></a></td><td id="G7"><a href="#" class="black pawn">&#9823;</a></td><td id="H7"><a href="#" class="empty"></a></td></tr><tr><td id="A6"><a href="#" class="empty"></a></td><td id="B6"><a href="#" class="empty"></a></td><td id="C6"><a href="#" class="empty"></a></td><td id="D6"><a href="#" class="black queen">&#9819;</a></td><td id="E6"><a href="#" class="empty"></a></td><td id="F6"><a href="#" class="black pawn">&#9823;</a></td><td id="G6"><a href="#" class="empty"></a></td><td id="H6"><a href="#" class="empty"></a></td></tr><tr><td id="A5"><a href="#" class="black pawn">&#9823;</a></td><td id="B5"><a href="#" class="empty"></a></td><td id="C5"><a href="#" class="black pawn">&#9823;</a></td><td id="D5"><a href="#" class="empty"></a></td><td id="E5"><a href="#" class="empty"></a></td><td id="F5"><a href="#" class="white pawn">&#9817;</a></td><td id="G5"><a href="#" class="empty"></a></td><td id="H5"><a href="#" class="black pawn">&#9823;</a></td></tr><tr><td id="A4"><a href="#" class="white pawn">&#9817;</a></td><td id="B4"><a href="#" class="empty"></a></td><td id="C4"><a href="#" class="empty"></a></td><td id="D4"><a href="#" class="empty"></a></td><td id="E4"><a href="#" class="white queen">&#9813;</a></td><td id="F4"><a href="#" class="empty"></a></td><td id="G4"><a href="#" class="empty"></a></td><td id="H4"><a href="#" class="white pawn">&#9817;</a></td></tr><tr><td id="A3"><a href="#" class="empty"></a></td><td id="B3"><a href="#" class="empty"></a></td><td id="C3"><a href="#" class="empty"></a></td><td id="D3"><a href="#" class="empty"></a></td><td id="E3"><a href="#" class="empty"></a></td><td id="F3"><a href="#" class="black pawn">&#9823;</a></td><td id="G3"><a href="#" class="white pawn">&#9817;</a></td><td id="H3"><a href="#" class="empty"></a></td></tr><tr><td id="A2"><a href="#" class="empty"></a></td><td id="B2"><a href="#" class="empty"></a></td><td id="C2"><a href="#" class="white pawn">&#9817;</a></td><td id="D2"><a href="#" class="empty"></a></td><td id="E2"><a href="#" class="empty"></a></td><td id="F2"><a href="#" class="white pawn">&#9817;</a></td><td id="G2"><a href="#" class="empty"></a></td><td id="H2"><a href="#" class="white king">&#9812;</a></td></tr><tr><td id="A1"><a href="#" class="empty"></a></td><td id="B1"><a href="#" class="empty"></a></td><td id="C1"><a href="#" class="empty"></a></td><td id="D1"><a href="#" class="empty"></a></td><td id="E1"><a href="#" class="empty"></a></td><td id="F1"><a href="#" class="empty"></a></td><td id="G1"><a href="#" class="empty"></a></td><td id="H1"><a href="#" class="empty"></a></td></tr></table></div>'),
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' => undef,
'fen' => undef,
'fen ' => undef,
);
done_testing;