Giter Site home page Giter Site logo

Comments (5)

MarkZH avatar MarkZH commented on June 10, 2024 1

@HarshilPatel007

I would also use a set, but in a different way.

for target_square in set(move.to_square for move in legal_moves):

from python-chess.

niklasf avatar niklasf commented on June 10, 2024

Hi. Checking the entire output, it looks something like:

Updated FEN: rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1
Traceback (most recent call last):
  File "/home/niklas/Projekte/python-chess/chesss.py", line 117, in mousePressEvent
    self.move_manager.move_piece(square)
  File "/home/niklas/Projekte/python-chess/chesss.py", line 193, in move_piece
    pgn_move = self.chessboard.board.variation_san([move])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/niklas/Projekte/python-chess/chess/__init__.py", line 2970, in variation_san
    raise IllegalMoveError(f"illegal move {move} in position {board.fen()}")
chess.IllegalMoveError: illegal move e2e4 in position rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1

So the you're already in the position after 1. e4, where e4 is no longer a legal move (because it's black to move).

To fix that, you can reorder the code to create the notation before board.push(...).

from python-chess.

HarshilPatel007 avatar HarshilPatel007 commented on June 10, 2024

hi @niklasf ,
thanks a lot. it worked.

But, what about the points 2 and 3 ?
The pawn's legal move at the last rank highlighted with 100% dark. which means that it doesn't considers the opacity settings there.
I think the code is logic wise is correct. because, it works fine except the last rank with the pawn moves. All others pieces legal moves highlighted correctly with the opacity settings in all over the board. In fact, the pawn moves highlighted correctly excepts the last rank. (I'm facing the same issue with GTK as well)

  • pawn's legal moves highlight at the last rank. ( see the highlight on the knight. it's 100% dark)
    Screenshot from 2024-01-15 10-41-05

  • queen's legal moves highlight at the last rank. ( see the highlight on the knight. it considers the opacity settings)
    Screenshot from 2024-01-15 10-41-32

also, the self.chessboard.board.legal_moves check is working fine with all the pieces except the last rank+pawn combination.
means, if, I change the code from,

            if move in self.chessboard.board.legal_moves:
                if (
                    self.chessboard.board.piece_type_at(self.selected_square)
                    == chess.PAWN
                ):
                    if (
                        self.chessboard.board.turn == chess.WHITE
                        and chess.square_rank(target_square) == 7
                    ):
                        self.handle_pawn_promotion(move)
                    elif (
                        self.chessboard.board.turn == chess.BLACK
                        and chess.square_rank(target_square) == 0
                    ):
                        self.handle_pawn_promotion(move)

to,

            if move in self.chessboard.board.legal_moves:
                if (
                    self.chessboard.board.piece_type_at(self.selected_square)
                    == chess.PAWN
                ):
                    if (
                        self.chessboard.board.turn == chess.WHITE
                        and chess.square_rank(target_square) == 6
                    ):
                        self.handle_pawn_promotion(move)
                    elif (
                        self.chessboard.board.turn == chess.BLACK
                        and chess.square_rank(target_square) == 1
                    ):
                        self.handle_pawn_promotion(move)

then it works with all the ranks except the last rank. It's just mind boggling thing for me. As I'm getting same kind of (Bugs?) with GTK as well.

from python-chess.

MarkZH avatar MarkZH commented on June 10, 2024

@HarshilPatel007

For point (2), the reason for the circles being darker is that there are multiple circles being drawn on those squares. In the first image in your last post, the pawn has four legal moves: cxb8r, cxb8n, cxb8b, and cxb8q. So, each of those moves causes a circle to get drawn on square b8. In the loop starting on line 126, you need to loop over squares where pieces can land, not over legal moves.

For point (3), line 170, move = chess.Move(self.selected_square, target_square), does not create a legal move when the piece is a pawn and the target_square is a square on the back rank because it doesn't include a promotion piece. Instead of checking if the move is legal, you should check if there is any legal move from self.selected_square to target_square in self.chessboard.board.legal_moves list.

from python-chess.

HarshilPatel007 avatar HarshilPatel007 commented on June 10, 2024

hi @MarkZH ,
thanks a lot for providing insights. this helped me to solve those bugs.

for point 2, i don't know any other way to get squares where pieces can land other then getting it from a legal moves of a selected pieces. (If you know then, please give me some ideas) so, I just hacked around the code and used a set to keep track of the squares that have already has a circle to skip drawing a circle on that square. 😆

for point 3, I've done exactly as you've said and bug gone.

Thanks.

from python-chess.

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.