Comments (4)
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.
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.
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.
This sounds great. Thank you for the suggestions!
from chess.js.
Related Issues (20)
- Double check dectection?
- FEN issue: It's not giving an error when a pawn is placed on the first and last ranks. HOT 1
- How to make illigal moves using chess.js? HOT 3
- Chuck
- [Question] After I load a pgn, how can I get the fen after the Nth ply? HOT 1
- isAttacked is not working HOT 4
- Minimalist Chess
- Chess960 - Wrong Move - Possible Castling HOT 1
- Less Strict FEN Validation HOT 8
- detect "chess pieces" had destroyed HOT 1
- unable to set side to move
- [Question]: How to access the various fields in .moves({ verbose: true}) HOT 1
- Using chess.js in Quasar/Vue app has issues with _positionCounts when used in pinia store HOT 2
- I can't get possible moves for black pieces HOT 4
- Short pawn moves like 'ab', 'de' etc.
- unique id for each piece HOT 1
- Typescript not compiling HOT 1
- Feature Request: halfMove number in getComments()
- `.moves()` can provide moves that throw an "invalid move" error
- Reversed board HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chess.js.