oakmac / chessboardjs

JavaScript chessboard
https://chessboardjs.com
MIT License
2.01k stars 408 forks source link

Feature request: allow boards smaller than 8x8 #198

Open ivan444 opened 4 years ago

ivan444 commented 4 years ago

Some games require boards which are smaller than 8x8. Add ability to create smaller boards. Controll it via config.

I'll send a PR.

slolo2000 commented 3 years ago

Hello,

Any chance to see this nice feature in a next release?

Thanks in advance and have a nice day.

ivan444 commented 3 years ago

My PR https://github.com/oakmac/chessboardjs/pull/199 the implements this (works correctly). Considering I got no comments on it, it's unlikely for it to be merged (which is understandable).

Feel free to fork it.

On Thu, Jun 10, 2021, 16:16 slolo2000 @.***> wrote:

Hello,

Any chance to see this nice feature in a next release?

Thanks in advance and have a nice day.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/oakmac/chessboardjs/issues/198#issuecomment-858660776, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH4W2OY3YL2LSCS3H46OQ3TSDCLHANCNFSM4QPTYQMQ .

oakmac commented 3 years ago

Any chance to see this nice feature in a next release?

I do not have any plans to add this to chessboard.js at this time (that does not mean "never", just "not right now").

@ivan444 solution looks like a good fix if you need this ASAP 👍 🤓

slolo2000 commented 3 years ago

Maybe if the chess.js library implemented this feature then it would make more sense in chessboardjs.

Anyway, thanks a lot for your answer.

slolo2000 commented 3 years ago

I have had look to the PR #199

var config = {position: fenString, numRows: 6, numColumns: 6}; board = Chessboard('myBoard', config);

It works well and it display a board with 6x6 like expected. But if the fen string does not respect the real format (with 8 /) the board is not filled with chess pieces.

This issue is due to the validFen function which does not take care of board dimensions. Also, objToFen and fenToObj functions need to be adapted to support fen string lower than 8 chunks.

@ivan444 Do you think you could add this to your PR #199 ?

Like that the ability to create smaller boards will be fully functional and will be add to chessboardjs, I hope.

ivan444 commented 3 years ago

It works well and it display a board with 6x6 like expected. But if the fen string does not respect the real format (with 8 /) the board is not filled with chess pieces.

@ivan444 Do you think you could add this to your PR #199 ?

That's intentional, as FEN string by definition is 8 characters long. Some assumptions break when using it for smaller boards.

My bad for not making it clear in the PR description

slolo2000 commented 3 years ago

You are right for the FEN string definition.

But for example, this one is valid on a 6x6 board:

6/5p/2N1pk/6/6/K1R3 w - - 1 1

And it could be really useful to support small board for young players when they have to solve chess problems.

slolo2000 commented 3 years ago

Maybe a first step to fully support smaller boards than 8x8

Based on your works @ivan444 I have completed validFen, fenToObj and objToFen functions in order to take in consideration config._boardDimension It would be nice if you could complete you PR #199 in order to have a better support of small board.

chessboard-1.1.js.zip

ivan444 commented 3 years ago

@oakmac what do you think about merging PR #199?

slolo2000 commented 3 years ago

I don't know how to merge my code into your PR and I am not really sure about my code addition. I have done so tests and it seems to work fine but if you could check what I have done before merging it would be nice.

ivan444 commented 3 years ago

@slolo2000 I meant that @oakmac merge PR #199 to the main repo.

You can then add your change base in the main repo. Or you can fork my fork of the repo and create PR based on that (I recomment the former).

slolo2000 commented 3 years ago

Always based on @ivan444 work, I have completed my previous work on "support for smaller boards"

I use it without problem and it seems to work quite nice.

chessboard-1.1.1.js.zip

You can even use this feature with chess.js using a piece of code like below in order to "normalize" your fen string.

// Used to normalize a fen string from smaller board to "normal" chess board
// dimBoardLength: the number of squares in a row/column
function normalizeFenString(fen, dimBoardLength) {
    var deltaFen = 8 - dimBoardLength;
    var position = fen;

    if (deltaFen > 0) {
        var fenBegin = fen.replace(/ .+$/, '');
        var fenEnd = fen.replace(fenBegin, '');
        var rows = fenBegin.split('/');

        position = "";
        for (var i = 0; i < deltaFen; i++) {
            if (position != "") {
                position += "/";
            }
            position += "8";
        }
        for (var i = 0; i < rows.length; i++) {
            var c = rows[i].slice(-1);
            if (c.search(/[1-8]/) !== -1) {
                position += "/" + rows[i].substring(0, rows[i].length - 1) + (parseInt(c, 10) + deltaFen).toString();
            } else {
                position += "/" + rows[i] + deltaFen.toString();
            }
        }

        position += fenEnd;
    }

    return position;
}