【问题标题】:FEN (Chess notation) to HTML generator? Open source Java [closed]FEN(国际象棋符号)到 HTML 生成器?开源Java [关闭]
【发布时间】:2012-02-02 00:39:56
【问题描述】:

在我们自己实现之前,是否有一个现有的开源 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 应用程序中。

(Here is an implementation in Python)

【问题讨论】:

  • 为什么是图片?国际象棋符号不是 Unicode 的一部分吗?
  • @KerrekSB:确实!那也很好。更新我的问题。
  • 嗯,似乎有一个很好的 LaTeX 包(“skak”),而且总是有 LaTeX2HTML,但这似乎很浪费。直接到 HTML 的解决方案会很好。
  • @KerrekSB:谢谢!不幸的是,Android 不理解 LaTeX。

标签: java android html open-source chess


【解决方案1】:

我从这个地方找到了一些有用的 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>

【讨论】:

    【解决方案2】:

    引用 Alexander Maryanovsky Jin 的site 网站:

    关于晋

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

    他的项目是开源的,可以在Sourceforge上找到。

    获取Position.java 类,您会发现一些处理FEN 的Java 代码。

    【讨论】:

    • +1 我已经接受了一个答案,但是 Jin 使用 Java 的事实使它更接近于我的书面问题(尽管在我的特定用例中 JavaScript 实际上也很好)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多