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

count number of pieces or count captures #82

Closed
tgoldenberg opened this issue Apr 17, 2015 · 4 comments
Closed

count number of pieces or count captures #82

tgoldenberg opened this issue Apr 17, 2015 · 4 comments

Comments

@tgoldenberg
Copy link

I would like to be able to query the game for how many of each piece type are still on the board at any given time. For example, game.count('bP') could return 8. I would like to do this so I can display captured pieces on a sidebar. The simplest method I can think of is to check if the number of pawns < 8, then put the remainder of pawn icons in the sidebar. Any suggestions? Thank you in advance :).

@jhlywa
Copy link
Owner

jhlywa commented Apr 18, 2015

So I think your approach would run into problems whenever a player promotes a pawn - it would incorrectly assume the pawn was captured. A better way to build the captured pieces sidebar would be to scan the game history (chess.history({ verbose: true })) and count each captured piece, like so:

var history = chess.history({verbose: true});
var initial = {w: {p: 0, n: 0, b: 0, r: 0, q: 0},
               b: {p: 0, n: 0, b: 0, r: 0, q: 0}};

var captured = history.reduce(function(acc, move) {
  if ('captured' in move) {
    var piece = move.captured;
    // switch colors since the history stores the color of the player doing the
    // capturing, not the color of the captured piece
    var color = move.color == 'w' ? 'b' : 'w';
    acc[color][piece] += 1;
    return acc;
  } else {
    return acc;
  }
}, initial);

Does that work for you?

@tgoldenberg
Copy link
Author

Yes, I was able to get the captured pieces to the sidebar, except for in the case of pawn promotion. I will try your suggested method as well. Also, I notice that in my two player real-time game, the castling doesn't show on the opponent's screen - instead, just the king's move - i.e., `e1-g1'. How can I pass into the board.move(notation) to make the king move as well? Or do I have to pass in FEN notation? Here is my code, using Pusher :

channel.bind('new_move', function(data) {

onDrop(data.object_notation.split('-')[0], data.object_notation.split('-')[1]);
board.move(data.object_notation);

@jhlywa
Copy link
Owner

jhlywa commented Apr 18, 2015

When you call .move(string) your string needs to be valid Standard Algebraic Notation. So in this case, the SAN for kingside castling would be chess.move('O-O'). Be warned, the SAN parser in chess.js is strict, so you'll need correct capitalization and appropriate decorations (+ for check, and # for mate) or else the move will fail and return null.

An simpler approach would be to call .move(object), like chess.move({from: 'e1', to: 'g1'}). See the .move() docs for more info.

@jhlywa jhlywa closed this as completed Apr 18, 2015
@tgoldenberg
Copy link
Author

This sounds great. Thank you for the suggestions!

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

No branches or pull requests

2 participants