0

I have this javscript string:

response

"[[[#],[b,#],[b,w,b,b,b,#],[b,#],[b,w,w,b,#]],[[b,#],[b,b,#],[b,b,w,#],[b,b,b,#],[b,b,#]],[[b,#],[b,b,b,b,#],[b,w,#],[b,b,b,#],[b,#]],[[b,#],[b,#],[w,w,w,#],[b,b,w,w,#],[b,w,#]],[[b,#],[b,b,b,b,#],[b,w,b,#],[b,w,#],[b,b,#]]]"

This corresponds to a board game and which field (e.g [b,w,b,b,b,#]) is a cell with black and white pieces. The # is the top of the stack.

I need to parse this in order to create an array of tiles.

I have this:

XMLscene.prototype.readBoard = function(data){
    var response = data.target.response;
    console.log("REPONSE  NO PARS" + response);
    response = response.split("],");
    console.log("REPONSE " + response);
    response[0] = response[0].substring(1);
    response[5] = response[5].substring(0, response[5].length - 2);
    for(var i = 0; i < response.length; i++)
    {
        response[i] = response[i].substring(1);
        response[i] = response[i].split("),");
        for(var j = 0; j < response[i].length; j++)
            response[i][j] = response[i][j].substring(5);
    }
    this.scene.board.loadTiles(response);
    //this.scene.client.getPrologRequest('quit', 0, 1);
};

to be parsed in this function:

gameBoard.prototype.loadTiles = function(board){
    console.log("BOARD : " + board);
    this.tiles = [];
    for (var i = 0; i < board.length; i++){
        for (var j = 0; j < board[i].length; j++){
            var player = board[i][j].split(",")[0];
            console.log("PLAYER : " + player);
            var type = board[i][j].split(",")[1];
            console.log("Type : " + type);
            if (type != "e") {
                var tile = this.createTile(type, this.scene ,i*6 + j+100, player);
                tile.line = i;
                tile.col = j;
                this.tiles.push(tile);
            }
        }
    }


}

The board structure I want is something like this: for the first stack: [#] It's an empty cell

[b,#] - A cell with one piece - black

[b,w,w,b,#] - A cell with a black piece in the bottom, then two white pieces and a black on the top, therefore the black player is the owner of the stack!

The stack owner is the player that have his piece on the top of the stack (closest to #)

Is there any way to get an array with each stack being the element of it?

Regards

19
  • Where are you getting the string from? Any chance you can make it actual JSON? Commented Dec 28, 2016 at 21:36
  • It is parsed from a PROLOG program. It represents my board on a sicstus prolog program Commented Dec 28, 2016 at 21:39
  • Then you could parse it further to JSON format and then parse it to javascript object, which should be much easier than parsing your string to object directly and on your own Commented Dec 28, 2016 at 21:42
  • How could I do that since I have a #? I did something like response = JSON.parse(response); Commented Dec 28, 2016 at 21:46
  • 1
    JSON.parse(yourString.replace(/[bw#]/g, '"$&"')) Commented Dec 28, 2016 at 22:02

3 Answers 3

3

You could transform the data to JSON like this, ignoring the hashes as they seem to give information that is already known (the stack ends at the right):

var response = JSON.parse(response.replace(/,?#/g, '').replace(/[bw]/g, '"$&"'));

Then you can for instance identify the current player for a stack at (i, j), like this:

var player = board[i][j].slice(-1)[0]; // last element on the stack

Snippet:

// Sample data
var response = "[[[#],[b,#],[b,w,b,b,b,#],[b,#],[b,w,w,b,#]],[[b,#],[b,b,#],[b,b,w,#],[b,b,b,#],[b,b,#]],[[b,#],[b,b,b,b,#],[b,w,#],[b,b,b,#],[b,#]],[[b,#],[b,#],[w,w,w,#],[b,b,w,w,#],[b,w,#]],[[b,#],[b,b,b,b,#],[b,w,b,#],[b,w,#],[b,b,#]]]";

// Convert to nested array
var board = JSON.parse(response.replace(/,?#/g, '').replace(/[bw]/g, '"$&"'));

// Print the stack at 3, 3
console.log(board[3][3].join(','));

// Print player for that stack:
console.log(board[3][3].slice(-1)[0]);

Sign up to request clarification or add additional context in comments.

6 Comments

That could be a chance but then I get this: ,b,b,w,b,b,b,b,b,w,w,b,b,b,b,b,b,w,b,b,b,b,b,b,b,b,b,b,b,w,b,b,b,b,b,b,w,w,w,b,b,w,w,b,w,b,b,b,b,b,b,w,b,b,w,b,b And there's no way I could separate the stacks
It is not a single dimension array you get, but a 3D array. See the snippet I added, which illustrates that structure, shows one of the stacks, and the player for that stack.
You're superb! Thank you very much!
would you mind if I ask how do I put that result: b,b,w,w in an array? In order to iterate on it on my loadTiles?
It is already an array. In the snippet I did join(',') on it to turn it into the comma-separated string you see, but without it, it is an array. So you can do things like board[i][j].forEach(function (value) { ..... do something with value--it is either 'w' or 'b' ... });
|
0

A quick and dirty solution is to quote all your elements by using String.prototype.replace() and then put the entire result in an eval():

var str = "[[[#],[b,#],[b,w,b,b,b,#],[b,#],[b,w,w,b,#]],[[b,#],[b,b,#],[b,b,w,#],[b,b,b,#],[b,b,#]],[[b,#],[b,b,b,b,#],[b,w,#],[b,b,b,#],[b,#]],[[b,#],[b,#],[w,w,w,#],[b,b,w,w,#],[b,w,#]],[[b,#],[b,b,b,b,#],[b,w,b,#],[b,w,#],[b,b,#]]]";
var res = eval(str.replace(/[bw#]/g, "'$&'"));
console.log(res);
            

Comments

0

Modify your string to look like this...

var myString = '[[[#],[b,#],[b,w,b,b,b,#],[b,#],[b,w,w,b,#]],[[b,#],[b,b,#],[b,b,w,#],[b,b,b,#],[b,b,#]],[[b,#],[b,b,b,b,#],[b,w,#],[b,b,b,#],[b,#]],[[b,#],[b,#],[w,w,w,#],[b,b,w,w,#],[b,w,#]],[[b,#],[b,b,b,b,#],[b,w,b,#],[b,w,#],[b,b,#]]]' 

replace elements with ""

Now run following:

var myArrayObject = JSON.parse(myString);

You just converted it to array.

Sample code: https://fiddle.jshell.net/3gvzmwef/21/


6 Comments

I can't because it represents a PROLOG gameboard and it is made like that
@Perseverance you can always just do '{' + response + '}'
@spooky, what do you intend with this curly braces? That's still invalid JSON.
as @Ted said... Play with response object before it is passed to loadTiles.
I did that response = '{' + response + '}'; reponse = JSON.parse(response); console.log("REPONSE " + response); and: Unexpected token [ in JSON at position 1
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.