bhlangonijr / chesslib

chess library for legal move generation, FEN/PGN parsing and more
Apache License 2.0
229 stars 80 forks source link

What is happening with pawns? #110

Closed thehagimy closed 1 year ago

thehagimy commented 1 year ago

Hello, I'm trying to do an Android app using your library, and i wanna ask why pawns can move everywhere, like from starting a2 the pawn can instantly get to a7 just by typing "a7". Is this a library ussue or mine missunderstanding?

from rnbqkbnr pppppppp ........ ........ ........ ........ PPPPPPPP RNBQKBNR Side: WHITE

to rnbqkbnr Pppppppp ........ ........ ........ ........ .PPPPPPP RNBQKBNR Side: BLACK

just by one "a7"

thanks =)

ghost commented 1 year ago

Depends on how you call it.

If you use doMove(final String move) it should only do legal moves.

If you use doMove(final Move move) then by default the validation is off and you should instead use doMove(final Move move, boolean fullValidation)

thehagimy commented 1 year ago

Depends on how you call it.

If you use doMove(final String move) it should only do legal moves.

If you use doMove(final Move move) then by default the validation is off and you should instead use doMove(final Move move, boolean fullValidation)

I use text from edittext to a movement String value, then I use it like this: board.doMove(movement)

and it doesn't work properly.

I tried to use board.doMove(movement, true) but it's saying something like "provided type: String, should use: Move".

I also tried use if (board.legalMoves().contains(movement)) { // my code here } but it didn't do anything, even update the board.

how can I maybe convert the "movement" string to a move or I can do something else?

thanks for replying

ghost commented 1 year ago

Can you provide a bit more info?

  1. Share your build.gradle.kts (or equivalent, depending on the project)
  2. Try to step into the "doMove" function with a debugger and try to find out whats wrong (which branches does the code take and what are the veriable values on the way)
  3. When you try your manual check variant with if (board.legalMoves()... does board.legalMoves have a list of moves or is it empty?
thehagimy commented 1 year ago

Can you provide a bit more info?

  1. Share your build.gradle.kts (or equivalent, depending on the project)
  2. Try to step into the "doMove" function with a debugger and try to find out whats wrong (which branches does the code take and what are the veriable values on the way)
  3. When you try your manual check variant with if (board.legalMoves()... does board.legalMoves have a list of moves or is it empty?

my code:

public void onClick(View v) {
                if (!board.isDraw() && !board.isMated()) {
                    try {
                        try {
                            chess_board = findViewById(R.id.chess_board);
                            Board board = new Board();
                            tin_field = findViewById(R.id.tin_field);
                            chess_board.setText("" + board);

                            String movement = tin_field.getText().toString();
                            if (board.legalMoves().contains(movement)) {
                                MoveGenerator.generateLegalMoves(board);
                                board.doMove(movement);
                                List<Move> moves = board.legalMoves(); // generates LEGAL moves that are really legal
                                System.out.println(board.toString());
                                chess_board.setText("" + board);
                            }

                        } catch (MoveConversionException e) {
                            chess_board.setText("" + board);
                        }
                    } catch (StringIndexOutOfBoundsException e) {
                        chess_board.setText("" + board);
                    }

                }
            } 

it doesn't work, so i tried this (also doesn't work):

... String movement = tin_field.getText().toString();
                            if (board.legalMoves().contains(movement)) {
                                MoveGenerator.generateLegalMoves(board);
                                board.doMove(movement, true); // <- here, error (Required type: Move, Provided: String)
                                List<Move> moves = board.legalMoves(); // also generates legal moves
                                System.out.println(board.toString());
                                chess_board.setText("" + board);
                            } ...

i do not really know what i should show, but here's my build.gradle(:app):

dependencies {
    implementation 'com.github.bhlangonijr:chesslib:1.3.3'
    // ...
}
android {
    // ...
    compileSdk 33
    // ...
}
bhlangonijr commented 1 year ago

The doMove function essentially moves the chess piece to any arbitrary position without validating standard chess rules, but it ensures that the chessboard remains in a valid state, meaning that it doesn't leave the player's own king in check. This function is useful in situations where you want to implement a chessboard edit interface for dragging and dropping pieces, for example. If you want to validate and restrict the moves to only valid ones, you should call a move generator function, such as this one, and compare the resulting move list with the user's input.

bhlangonijr commented 1 year ago

By the way, the provided code only instantiates a new board with the standard opening chess position. If you don't set up the board with the appropriate chess position, you will always be matching the legalMoves from the standard chess opening position. To set up the board correctly, you can utilize class facilities like setFen or feed doMove in a loop with a list of chess moves that have already been made.