Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Board State #119

Closed
streamich opened this issue Jul 21, 2016 · 11 comments
Closed

Board State #119

streamich opened this issue Jul 21, 2016 · 11 comments

Comments

@streamich
Copy link

How can I get the current board state? You have this variable board but it seems to be private, so is there a way to get the current board state?

@streamich
Copy link
Author

Public API like this would be great:

board: function() {
    return board;
}

@streamich
Copy link
Author

BTW, why are there 128 squares on the board?

var board = new Array(128);

@streamich
Copy link
Author

streamich commented Jul 21, 2016

Maybe it could be like this then:

board: function() {
    var pieces = [];
    for(var pos in SQUARES) {
        var square = board[SQUARES[pos]];
        if(square) {
            // pieces.push({
            //     type: square.type,
            //     color: square.color,
            //     pos: pos,
            // });

            // pieces.push([pos[0], parseInt(pos[1]), square.type, square.color]);

            // pieces.push([parseInt(pos[0], 18) - 10, parseInt(pos[1]) - 1, square.type, square.color === 'w']);

            pieces.push([pos, square]);
        }
    }
    return pieces;
}

@jhlywa
Copy link
Owner

jhlywa commented Jul 21, 2016

The internal board uses 128 squares because of the 0x88 move generation algorithm. During move generation, the algorithm can easily tell which squares are off the board by checking for a non-zero return from square & 0x88. This saves cycles compared to the more obvious approach if (x < 0 || x > 7 || y < 0 || y > 7).

The FEN string (chess.fen()) contains the entire state of the board along with castling and en passant permissions, and move numbers. FEN is the standard way to import/export positions from chess utilities. You could also try using chess.get() if you like.

The internal board representation is not meant to be exported.

@streamich
Copy link
Author

streamich commented Jul 21, 2016

I wanted to use chess.js to parse a .fen string and then print the board on the screen, so exporting back to .fen is not an option for me.

I guess looping through chess.get() is the way to go.

Would be nice though to have a programmatic way of accessing the state of the whole board, like chess.toJson().

Also, for example, if I want to use chess.js to parse a .pgn game, I can load the .pgn file, but how do I access the header information? It seems you can only write header information?

.header()

Allows header information to be added to PGN output. Any number of key/value pairs can be passed to .header().

@AlexeiDarmin
Copy link

The internal board representation is not meant to be exported.

@jhlywa Being unable to access the board state is inconvenient when building bots. Without the functionality, I have to convert every fen position into an array before I can evaluate it, which is unnecassary when board already stores and works with the array.

It would be really awesome if this was part of the api :)

@jhlywa
Copy link
Owner

jhlywa commented Jan 13, 2017

@AlexeiDarmin The internal board array is an atypical board configuration of 128 squares. Exporting this would eventually leak all of internal constants (e.g. RAYS, SHIFTS, etc...) used to transverse this array. Probably not ideal.

Would a simple 8x8 board array be more appropriate for your use case?

@AlexeiDarmin
Copy link

@jhlywa I believe an 8x8 board would be fine, anything that is easily loop-able. For example: column based comparisons are inconvenient via the fen string such as checking for double pawns since some math needs to be done to calculate the columns.

Visual example of how arrays make this easier.:
8/p6p/1p4p1/2p2p2/3pp3/8/8/8/8

[
[null, null, null, null, null, null, null, null],
[pawn, null, null, null, null, null, null, pawn],
[null, pawn, null, null, null, null, pawn, null],
[null, null, pawn, null, null, pawn, null, null],
[null, null, null, pawn, pawn, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null]
]

@jhlywa
Copy link
Owner

jhlywa commented Jan 16, 2017

@AlexeiDarmin I've added a board() method to master. It'll be officially included in the 0.11.0 release.

@jhlywa jhlywa closed this as completed Jan 16, 2017
@jhlywa jhlywa added this to the 0.11.0 milestone Jan 18, 2017
@streamich
Copy link
Author

streamich commented Jan 18, 2017

Thank you!

It might also make sense to have an option to return only the list of squares where some piece are actually present. Consider an empty board with just a rook:

r7/8/8/8/8/8/8/8

You could return:

{
    a8: 'r'
}

@abdurrahmanekr
Copy link

When will this feature be added?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants