FEN(国际象棋符号)转HTML生成器?开源Java。

5
在我们自己实现之前,是否有现成的开源Java代码可以将国际象棋FEN字符串转换为HTML表示的国际象棋棋盘? FEN代码如下:rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1输出结果应类似于<table><tr><td>♘</td><td>♛</td><td>...基于图标的解决方案,甚至是生成大型图像而不是HTML的解决方案也可以接受。它用于集成到Android应用程序中。这里有一个Python实现

1
为什么需要图片?国际标准码Unicode中不包含象棋符号吗? - Kerrek SB
@KerrekSB:确实!那也可以。我会更新我的问题。 - Nicolas Raoul
哦,似乎有一个不错的LaTeX包(“skak”),而且总是有LaTeX2HTML,但这似乎很浪费。直接生成HTML的解决方案会更好。 - Kerrek SB
@KerrekSB:谢谢!不幸的是,Android无法理解LaTeX。 - Nicolas Raoul
2个回答

5

我从这个地方找到一些有用的CSS3:http://designindevelopment.com/css/css3-chess-board/
因此,我得出了以下内容:

<html>
<head>
    <style type="text/css">
        .chess_board { border:1px solid #333; }
        .chess_board td {
            background:#fff; background:-moz-linear-gradient(top, #fff, #eee);
            background:-webkit-gradient(linear,0 0, 0 100%, from(#fff), to(#eee));
            box-shadow:inset 0 0 0 1px #fff;
            -moz-box-shadow:inset 0 0 0 1px #fff;
            -webkit-box-shadow:inset 0 0 0 1px #fff;
            height:40px; text-align:center; vertical-align:middle; width:40px; font-size:30px;}
        .chess_board tr:nth-child(odd) td:nth-child(even),
        .chess_board tr:nth-child(even) td:nth-child(odd) {
            background:#ccc; background:-moz-linear-gradient(top, #ccc, #eee);
            background:-webkit-gradient(linear,0 0, 0 100%, from(#ccc), to(#eee));
            box-shadow:inset 0 0 10px rgba(0,0,0,.4);
            -moz-box-shadow:inset 0 0 10px rgba(0,0,0,.4);
            -webkit-box-shadow:inset 0 0 10px rgba(0,0,0,.4); }
    </style>
    <script type="text/javascript">
        function renderFen(fentxt) {
            fentxt = fentxt.replace(/ .*/g, '');
            fentxt = fentxt.replace(/r/g, 'x'); // Convert black rooks to 'x' to avoid mixup with <tr></tr> tags
            fentxt = fentxt.replace(/\//g, '</tr><tr>');
            fentxt = fentxt.replace(/1/g, '<td></td>');
            fentxt = fentxt.replace(/2/g, '<td></td><td></td>');
            fentxt = fentxt.replace(/3/g, '<td></td><td></td><td></td>');
            fentxt = fentxt.replace(/4/g, '<td></td><td></td><td></td><td></td>');
            fentxt = fentxt.replace(/5/g, '<td></td><td></td><td></td><td></td><td></td>');
            fentxt = fentxt.replace(/6/g, '<td></td><td></td><td></td><td></td><td></td><td></td>');
            fentxt = fentxt.replace(/7/g, '<td></td><td></td><td></td><td></td><td></td><td></td><td></td>');
            fentxt = fentxt.replace(/8/g, '<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>');
            fentxt = fentxt.replace(/K/g, '<td>&#9812;</td>');
            fentxt = fentxt.replace(/Q/g, '<td>&#9813;</td>');
            fentxt = fentxt.replace(/R/g, '<td>&#9814;</td>');
            fentxt = fentxt.replace(/B/g, '<td>&#9815;</td>');
            fentxt = fentxt.replace(/N/g, '<td>&#9816;</td>');
            fentxt = fentxt.replace(/P/g, '<td>&#9817;</td>');
            fentxt = fentxt.replace(/k/g, '<td>&#9818;</td>');
            fentxt = fentxt.replace(/q/g, '<td>&#9819;</td>');
            fentxt = fentxt.replace(/x/g, '<td>&#9820;</td>');
            fentxt = fentxt.replace(/b/g, '<td>&#9821;</td>');
            fentxt = fentxt.replace(/n/g, '<td>&#9822;</td>');
            fentxt = fentxt.replace(/p/g, '<td>&#9823;</td>');
            return '<table class="chess_board" cellspacing="0" cellpadding="0"><tr>' + fentxt + '</tr></table>';
        }
    </script>
</head>
<body>
    <script type="text/javascript">
        document.write(renderFen('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'));
    </script>
</body>


1

引用 Alexander Maryanovsky Jin 的 网站

关于 Jin

Jin 是一个开源、跨平台的图形化国际象棋客户端,使用 Java 编写。Jin 可以作为独立应用程序(可在此网站上获得)或作为小程序直接从您的浏览器运行(可在国际象棋服务器的网站上获得)。

他的项目是开源的,并且可以在 Sourceforge 上获取。

获取 Position.java 类,您将找到一些处理 FEN 的 Java 代码片段。


+1 我已经接受了一个答案,但事实上Jin是在Java里面,使得它更接近我的书面问题的答案(尽管在我特定的用例中JavaScript也可以)。 - Nicolas Raoul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接