www.webdeveloper.com
Results 1 to 5 of 5

Thread: Substituting symbols in chess notation.

  1. #1
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373

    Substituting symbols in chess notation.

    In English speaking countries, chess notation (.pgn) looks something like this:
    Code:
    1. d4 Nf6 2. Nf3 g6 3. Nc3 d5 4. Bf4 Bg7 5. e3 O-O 6. Be2 b6 7. Ne5 Bb7
    8. h4 Nbd7 9. f3 Nxe5 10. Bxe5 Ne8 11. Bxg7 Nxg7 12. f4 e5 13. fxe5 f6
    14. Bf3 fxe5 15. dxe5 c6 16. Qd2 Qe8 17. O-O-O Rd8 18. h5 gxh5 19. Qd4 c5
    20. Qh4 Qxe5
    The letters for the pieces (KQBNR) are converted to symbols as found in a diagram for international audiences using high ascii in specialized fonts.

    The regular expression for a typical move should look something like this: /([BKNRQ])([a-h1-8]?x?[a-h][1-8])/ and a pawn promotion /([a-h]?x?[a-h][18]=)([BNRQ])/ . A brute strength method of looking at all upper case letters wouldn't work when there are comments such as "Note the Queen is hanging." and symbols shouldn't be substituted for the N and Q outside of a move.

    I am at a loss as to how to do this when there are many moves on a line.

    TIA

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380
    Can you give a brief description of the 'chess notation (.pgn)'?

    What are the 'high ascii in specialized fonts' values for the (KQBNR) pieces?

    You described the input but can you describe the output a bit more?
    What should we convert to when detecting the typical moves?


  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    I had installed this font Chess Cases to do diagrams ChessDiag.htm and wanted to extend what it could do. In this font the symbols
    for "KQBNR" are "" respectively.
    I would want to change
    Code:
    1. d4 Nf6 2. Nf3 g6 3. Nc3 d5 4. Bf4 Bg7
    to
    Code:
    1. d4 f6 2. f3 g6 3. c3 d5 4. f4 g7
    When copied to a word processor with that font in control, the N and B would be replaced by the symbol for those pieces as it would appear in a diagram.

    This is a description of PGN from wikipedia: Portable_Game_Notation

    Thanks for looking at it.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Lightbulb

    The following is just a start. It does not filter out the { xxx } or ';' comments called for in your PGN chess link.
    But it does the majority of the work for you.

    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> PGN Conversion </title>
    <script type="text/javascript">
    //<![CDATA[
    // "KQBNR" are ""
    
    String.prototype.PGNamer = function() {
      var PGNsymbols = ['','','','',''];
      var tstr = '';  var ptr = '';
      var tarr = this.split('');
      for (var i=0; i<tarr.length; i++) {
        ptr = 'KQBNR'.indexOf(tarr[i]);  
        if (ptr == -1) { tstr += tarr[i]; }
                  else { tstr += PGNsymbols[ptr]; }
      }
      return tstr+' ';
    } 
    function PGNconverter() {
      var str = '';
      var tarr = document.getElementById('fromText').value.split(' ');
      for (var i=0; i<tarr.length; i++) {
        str += tarr[i].PGNamer();
      }
      document.getElementById('toPGN').value = str;
    }
    //]]>
    </script>
    
    </head>
    <body>
    From text:<br>
    <textarea rows="8" cols="80" id="fromText">
    1. d4 Nf6 2. Nf3 g6 3. Nc3 d5 4. Bf4 Bg7 5. e3 O-O 6. Be2 b6 7. Ne5 Bb7
    8. h4 Nbd7 9. f3 Nxe5 10. Bxe5 Ne8 11. Bxg7 Nxg7 12. f4 e5 13. fxe5 f6
    14. Bf3 fxe5 15. dxe5 c6 16. Qd2 Qe8 17. O-O-O Rd8 18. h5 gxh5 19. Qd4 c5
    20. Qh4 Qxe5
    </textarea><p>
    <button onclick="PGNconverter()">Converted to PGN</button>:<br>
    <textarea rows="8" cols="80" id="toPGN">
    </textarea><p>
    </body>
    </html>
    Good Luck!

  5. #5
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    Thanks, I'll be checking it out in a few days.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles