Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- @Injectable()
- export class PgnService {
- chess: any;
- constructor() {
- this.chess = SingletonClass.getInstance().getChessJS;
- }
- test()
- {
- console.log('clicked');
- let spgn: string = `1.e4 e5 2.Nf3( 2.Bc4 Qg5 3.d3 )2...Nc6`.replace(/\n/gm, "");
- //let spgn: string = `1.e4 e5 2.Nf3 Nc6`.replace(/\n/gm,"");
- //let mynode: Node = new Node("(a b c d e (h i) f g)", NodeType.Variation, null);
- let mynode: Node = new Node(spgn, NodeType.Variation, null);
- console.log(mynode);
- /*
- var nodestring = JSON.stringify(mynode, function (key, value) {
- if (key == 'parent') { return null; }
- else { return value; }
- });
- */
- //console.log(mynode);
- //mynode.traverse(mynode);
- // mynode.traverseWithMoveFunc(mynode, (node:any) =>{ console.log(node.data) } );
- }
- }
- enum NodeType {
- Move,
- Variation
- }
- export class Move
- {
- sSAN: string;
- sFEN:string;
- sFrom:string;
- sTo:string;
- }
- class SingletonClass {
- private static _instance:SingletonClass = new SingletonClass();
- private _score:number = 0;
- private Chess:any = require('chess.js');
- private chess:any = new this.Chess();
- constructor() {
- if(SingletonClass._instance){
- throw new Error("Error: Instantiation failed: Use SingletonDemo.getInstance() instead of new.");
- }
- SingletonClass._instance = this;
- }
- public static getInstance():SingletonClass
- {
- return SingletonClass._instance;
- }
- public setScore(value:number):void
- {
- this._score = value;
- }
- public getScore():number
- {
- return this._score;
- }
- public addPoints(value:number):void
- {
- this._score += value;
- }
- public removePoints(value:number):void
- {
- this._score -= value;
- }
- public getChessJS()
- {
- return this.chess;
- }
- }
- export class Node {
- public chessjs = SingletonClass.getInstance().getChessJS();
- public move: Move = new Move();
- public data: string;
- public type: NodeType
- public parent: Node;
- public children: Array<Node> = new Array();
- public currentString: string = "";
- constructor(input: string, type: NodeType, parent: Node) {
- this.parent = parent;
- this.type = type;
- this.currentString = input;
- if (type == NodeType.Move) {
- this.data = input;
- }
- else if (type == NodeType.Variation) {
- this.parse();
- }
- }
- parse(): Node {
- while (this.currentString) {
- let c = this.currentString.charAt(0);
- if (c == "" || c == " ") {
- //do nothing for spaces just move on one character
- this.currentString = this.currentString.substring(1);
- }
- else if (c == "(") {
- let newNode: Node = new Node(this.currentString.substring(1), NodeType.Variation, this);
- this.children.push(newNode);
- this.currentString = newNode.currentString;
- }
- else if (c == ")") {
- this.currentString = this.currentString = this.currentString.substring(1);
- return this;
- }
- else {
- //split string by occurence of next opening or closing bracket
- let match = this.currentString.match(/^([\S\s]*?)(?:[(]|[)])([\S\s]*)$/);
- if(match==null)
- {
- let sMoves = this.currentString;
- let movesAndComments = this.moves2Array(sMoves);
- for (let i = 0; i < movesAndComments.moves.length; i++) {
- let newNode: Node = new Node(movesAndComments.moves[i], NodeType.Move, this);
- this.children.push(newNode);
- }
- this.currentString = "";
- }
- else
- {
- //get bracket type by extracting first character of initial string one character after cuttoff string length
- let sBracket = match[0].charAt(match[1].length);
- let sMoves = match[1];
- let movesAndComments = this.moves2Array(sMoves);
- for (let i = 0; i < movesAndComments.moves.length; i++) {
- let newNode: Node = new Node(movesAndComments.moves[i], NodeType.Move, this);
- this.children.push(newNode);
- }
- this.currentString = sBracket + match[2];
- }
- }
- }
- return this;
- }
- traverse(node: Node) {
- for (let i = 0; i < node.children.length; i++) {
- if (node.children[i].type == NodeType.Move) {
- console.log(node.children[i].data);
- }
- else {
- if (node.children[i].type == NodeType.Variation) {
- console.log("(");
- this.traverse(node.children[i]);
- console.log(")");
- }
- }
- }
- }
- traverseWithMoveFunc(node: Node, movefunc) {
- for (let i = 0; i < node.children.length; i++) {
- if (node.children[i].type == NodeType.Move) {
- movefunc(node.children[i]);
- }
- else if (node.children[i].type == NodeType.Variation) {
- this.traverseWithMoveFunc(node.children[i],movefunc);
- }
- }
- }
- moves2Array(sMovesWithComments: string) {
- var ms_c = sMovesWithComments;
- ms_c = ms_c.replace(/{.+?}/g, function (c) { return c.replace(' ', '||') }); //replace spaces within curly braces with ||
- ms_c = ms_c.replace(/\d+\./g, ''); //replace move numbers
- var isComment = function (moveOrComment) {
- return moveOrComment.search(/\{.*}/g) != -1
- }
- var cleanArray = function (actual) {
- var newArray = new Array();
- for (var i = 0; i < actual.length; i++) {
- if (actual[i]) {
- newArray.push(actual[i]);
- }
- }
- return newArray;
- }
- function trim(str) {
- return str.replace(/^\s+|\s+$/g, '');
- }
- var movesAndComments = cleanArray((ms_c).split(new RegExp(/\s+/g)));
- var plyCount = -1;
- var comments = [];
- for (var i = 0; i < movesAndComments.length;) {
- var moveOrComment = movesAndComments[i]
- var nextMoveOrComment = movesAndComments[i + 1]
- if (!isComment(moveOrComment)) //current item is a move
- {
- plyCount++
- if (nextMoveOrComment === undefined) //this is last item
- break;
- else if (isComment(nextMoveOrComment)) //next item is a comment
- {
- i = i + 2
- nextMoveOrComment = nextMoveOrComment.replace('||', ' ') //undoing what we did before
- comments[plyCount] = nextMoveOrComment //add the comment to result array
- }
- else {
- i++
- comments[plyCount] = "";
- }
- }
- else
- i++ //theoretically this should never happen
- }
- // delete comments
- let ms = sMovesWithComments.replace(/(\{[^}]+\})+?/g, '');
- // delete move numbers
- ms = ms.replace(/\d+\.(\.\.)?/g, '');
- // delete ... indicating black to move
- ms = ms.replace(/\.\.\./g, '');
- // delete numeric annotation glyphs
- ms = ms.replace(/\$\d+/g, '');
- let moves = trim(ms).split(new RegExp(/\s+/));
- // delete empty entries
- moves = moves.join(',').replace(/,,+/g, ',').split(',');
- let object = {
- moves: moves,
- comments: comments
- }
- return object;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement