GithubHelp home page GithubHelp logo

Comments (4)

jhlywa avatar jhlywa commented on May 23, 2024 2

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?

from chess.js.

tgoldenberg avatar tgoldenberg commented on May 23, 2024

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);

from chess.js.

jhlywa avatar jhlywa commented on May 23, 2024

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.

from chess.js.

tgoldenberg avatar tgoldenberg commented on May 23, 2024

This sounds great. Thank you for the suggestions!

from chess.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.