Comments (5)
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.
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.
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)
-
queen's legal moves highlight at the last rank. ( see the highlight on the knight. it considers the opacity settings)
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.
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.
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)
- "info string" on startup causes assertion HOT 2
- chess.pgn.GameNode add_line() creates duplicate variations HOT 1
- Documentation incorrect for chess.Board.turn HOT 1
- board.mirror() HOT 1
- flipping the board HOT 1
- is_variant_draw seems not to work properly HOT 1
- always castling rights for clean_castling_rights()
- Incorrect perft(4) with Kiwipete. HOT 3
- impossible to get the moves in san format after pushing the move
- [feature request] add `board.san_move_stack` similar to `board.move_stack` to get san format moves HOT 1
- Is this expected or simply a bug? HOT 1
- Using limit(mate=mate) terminates search before mate HOT 3
- Exception in callback Protocol._line_received('') HOT 1
- CancelledError and TimeoutError when using multithreaded Stockfish to analyze certain sequence of board positions. HOT 1
- Feature request: new argument for chess.svg.board() HOT 1
- engine.analyse() exits with TimeoutError exception HOT 2
- Parsing a database with `read_game` results in roughly 2 extra results per game
- `illegal san` error message when the first game in a database contains a move in the White or Black header HOT 4
- Illegal san, but legal uci move leading to two black kings? 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 python-chess.