Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2.  
  3.  
  4. @Injectable()
  5. export class PgnService {
  6.  
  7.     chess: any;
  8.  
  9.     constructor() {
  10.  
  11.      
  12.       this.chess = SingletonClass.getInstance().getChessJS;
  13.  
  14.     }
  15.  
  16.    
  17.  
  18.     test()
  19.     {
  20.        console.log('clicked');
  21.  
  22.  
  23.         let spgn: string = `1.e4 e5 2.Nf3( 2.Bc4 Qg5 3.d3 )2...Nc6`.replace(/\n/gm, "");
  24.         //let spgn: string = `1.e4 e5 2.Nf3 Nc6`.replace(/\n/gm,"");
  25.  
  26.         //let mynode: Node = new Node("(a b c d e (h i) f g)", NodeType.Variation, null);
  27.  
  28.         let mynode: Node = new Node(spgn, NodeType.Variation, null);
  29.  
  30.         console.log(mynode);
  31.  
  32.         /*
  33.         var nodestring = JSON.stringify(mynode, function (key, value) {
  34.             if (key == 'parent') { return null; }
  35.             else { return value; }
  36.         });
  37.  
  38.         */
  39.  
  40.         //console.log(mynode);
  41.  
  42.         //mynode.traverse(mynode);
  43.  
  44.        // mynode.traverseWithMoveFunc(mynode, (node:any) =>{ console.log(node.data) } );
  45.     }
  46.  
  47. }
  48.  
  49.   enum NodeType {
  50.     Move,
  51.     Variation
  52. }
  53.  
  54. export class Move
  55. {
  56.     sSAN: string;
  57.     sFEN:string;
  58.     sFrom:string;
  59.     sTo:string;
  60.  
  61. }
  62.  
  63. class SingletonClass {
  64.  
  65.     private static _instance:SingletonClass = new SingletonClass();
  66.  
  67.     private _score:number = 0;
  68.     private Chess:any = require('chess.js');
  69.     private chess:any = new this.Chess();
  70.  
  71.     constructor() {
  72.         if(SingletonClass._instance){
  73.             throw new Error("Error: Instantiation failed: Use SingletonDemo.getInstance() instead of new.");
  74.         }
  75.         SingletonClass._instance = this;
  76.     }
  77.  
  78.     public static getInstance():SingletonClass
  79.     {
  80.         return SingletonClass._instance;
  81.     }
  82.  
  83.     public setScore(value:number):void
  84.     {
  85.         this._score = value;
  86.     }
  87.  
  88.     public getScore():number
  89.     {
  90.         return this._score;
  91.     }
  92.  
  93.     public addPoints(value:number):void
  94.     {
  95.         this._score += value;
  96.     }
  97.  
  98.     public removePoints(value:number):void
  99.     {
  100.         this._score -= value;
  101.     }
  102.  
  103.     public getChessJS()
  104.     {
  105.         return this.chess;
  106.     }
  107.  
  108. }
  109.  
  110. export class Node {
  111.  
  112.     public chessjs = SingletonClass.getInstance().getChessJS();
  113.  
  114.     public move: Move = new Move();
  115.     public data: string;
  116.     public type: NodeType
  117.     public parent: Node;
  118.     public children: Array<Node> = new Array();
  119.  
  120.     public currentString: string = "";
  121.  
  122.  
  123.     constructor(input: string, type: NodeType, parent: Node) {
  124.         this.parent = parent;
  125.         this.type = type;
  126.         this.currentString = input;
  127.  
  128.         if (type == NodeType.Move) {
  129.             this.data = input;
  130.  
  131.         }
  132.         else if (type == NodeType.Variation) {
  133.  
  134.             this.parse();
  135.         }
  136.     }
  137.  
  138.  
  139.  
  140.     parse(): Node {
  141.  
  142.         while (this.currentString) {
  143.             let c = this.currentString.charAt(0);
  144.  
  145.             if (c == "" || c == " ") {
  146.                 //do nothing for spaces just move on one character
  147.                 this.currentString = this.currentString.substring(1);
  148.             }
  149.             else if (c == "(") {
  150.  
  151.                 let newNode: Node = new Node(this.currentString.substring(1), NodeType.Variation, this);
  152.  
  153.                 this.children.push(newNode);
  154.  
  155.                 this.currentString = newNode.currentString;
  156.  
  157.             }
  158.             else if (c == ")") {
  159.                 this.currentString = this.currentString = this.currentString.substring(1);
  160.                 return this;
  161.             }
  162.             else {
  163.                 //split string by occurence of next opening or closing bracket
  164.                 let match = this.currentString.match(/^([\S\s]*?)(?:[(]|[)])([\S\s]*)$/);
  165.  
  166.                 if(match==null)
  167.                 {
  168.                    let sMoves = this.currentString;
  169.                    let movesAndComments = this.moves2Array(sMoves);
  170.  
  171.                    for (let i = 0; i < movesAndComments.moves.length; i++) {
  172.                     let newNode: Node = new Node(movesAndComments.moves[i], NodeType.Move, this);
  173.                     this.children.push(newNode);
  174.                    }
  175.  
  176.                    this.currentString = "";
  177.                 }
  178.                 else
  179.                 {
  180.                 //get bracket type by extracting first character of initial string one character after cuttoff string length
  181.                 let sBracket = match[0].charAt(match[1].length);
  182.  
  183.                 let sMoves = match[1];
  184.  
  185.                 let movesAndComments = this.moves2Array(sMoves);
  186.  
  187.                 for (let i = 0; i < movesAndComments.moves.length; i++) {
  188.                     let newNode: Node = new Node(movesAndComments.moves[i], NodeType.Move, this);
  189.                     this.children.push(newNode);
  190.                 }
  191.  
  192.  
  193.                 this.currentString = sBracket + match[2];
  194.                 }
  195.  
  196.             }
  197.  
  198.         }
  199.  
  200.         return this;
  201.  
  202.  
  203.     }
  204.  
  205.     traverse(node: Node) {
  206.         for (let i = 0; i < node.children.length; i++) {
  207.             if (node.children[i].type == NodeType.Move) {
  208.                 console.log(node.children[i].data);
  209.             }
  210.             else {
  211.                 if (node.children[i].type == NodeType.Variation) {
  212.                     console.log("(");
  213.                     this.traverse(node.children[i]);
  214.                     console.log(")");
  215.                 }
  216.  
  217.             }
  218.         }
  219.     }
  220.  
  221.  
  222.  
  223.     traverseWithMoveFunc(node: Node, movefunc) {
  224.         for (let i = 0; i < node.children.length; i++) {
  225.             if (node.children[i].type == NodeType.Move) {
  226.  
  227.                      movefunc(node.children[i]);
  228.             }
  229.             else if (node.children[i].type == NodeType.Variation) {
  230.                    
  231.                     this.traverseWithMoveFunc(node.children[i],movefunc);
  232.                    
  233.             }
  234.  
  235.            
  236.         }
  237.     }
  238.  
  239.  
  240.  
  241.  
  242.     moves2Array(sMovesWithComments: string) {
  243.         var ms_c = sMovesWithComments;
  244.         ms_c = ms_c.replace(/{.+?}/g, function (c) { return c.replace(' ', '||') }); //replace spaces within curly braces with ||
  245.         ms_c = ms_c.replace(/\d+\./g, ''); //replace move numbers
  246.  
  247.         var isComment = function (moveOrComment) {
  248.             return moveOrComment.search(/\{.*}/g) != -1
  249.         }
  250.  
  251.         var cleanArray = function (actual) {
  252.             var newArray = new Array();
  253.             for (var i = 0; i < actual.length; i++) {
  254.                 if (actual[i]) {
  255.                     newArray.push(actual[i]);
  256.                 }
  257.             }
  258.             return newArray;
  259.         }
  260.  
  261.         function trim(str) {
  262.             return str.replace(/^\s+|\s+$/g, '');
  263.         }
  264.  
  265.  
  266.         var movesAndComments = cleanArray((ms_c).split(new RegExp(/\s+/g)));
  267.  
  268.         var plyCount = -1;
  269.         var comments = [];
  270.         for (var i = 0; i < movesAndComments.length;) {
  271.             var moveOrComment = movesAndComments[i]
  272.             var nextMoveOrComment = movesAndComments[i + 1]
  273.             if (!isComment(moveOrComment)) //current item is a move
  274.             {
  275.                 plyCount++
  276.                 if (nextMoveOrComment === undefined) //this is last item
  277.                     break;
  278.                 else if (isComment(nextMoveOrComment))  //next item is a comment
  279.                 {
  280.                     i = i + 2
  281.                     nextMoveOrComment = nextMoveOrComment.replace('||', ' ') //undoing what we did before
  282.                     comments[plyCount] = nextMoveOrComment  //add the comment to result array
  283.                 }
  284.                 else {
  285.                     i++
  286.                     comments[plyCount] = "";
  287.                 }
  288.             }
  289.             else
  290.                 i++ //theoretically this should never happen
  291.         }
  292.  
  293.  
  294.         // delete comments
  295.         let ms = sMovesWithComments.replace(/(\{[^}]+\})+?/g, '');
  296.  
  297.         // delete move numbers
  298.         ms = ms.replace(/\d+\.(\.\.)?/g, '');
  299.  
  300.         // delete ... indicating black to move
  301.         ms = ms.replace(/\.\.\./g, '');
  302.  
  303.         // delete numeric annotation glyphs
  304.         ms = ms.replace(/\$\d+/g, '');
  305.  
  306.         let moves = trim(ms).split(new RegExp(/\s+/));
  307.  
  308.         // delete empty entries
  309.         moves = moves.join(',').replace(/,,+/g, ',').split(',');
  310.  
  311.         let object = {
  312.             moves: moves,
  313.             comments: comments
  314.         }
  315.  
  316.         return object;
  317.     }
  318.  
  319.  
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement