From 53e1cc8df32bf99a21ad42a50411a00f2764f17c Mon Sep 17 00:00:00 2001 From: TiynGER Date: Fri, 7 May 2021 02:25:08 +0200 Subject: [PATCH] bugfix chess: genPawnPromotion always gives back promotions The if statement was not tailored to the color and according back ranks. It didnt even check if the piece is a pawn. Now it does. --- src/chess.nim | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/chess.nim b/src/chess.nim index 5f04bff..61f7b66 100644 --- a/src/chess.nim +++ b/src/chess.nim @@ -585,12 +585,15 @@ proc moveLeadsToCheck(chess: Chess, start: int, dest: int, check.uncheckedMove(start, dest) return check.isInCheck(color) -proc genPawnPromotion(move: Move, color: Color): seq[Move] = - ## Generate all possible promotions of a `move` by `color`. +proc genPawnPromotion(board: Board, move: Move, color: Color): seq[Move] = + ## Generate all possible promotions on a `board` of a `move` by `color`. var promotions = newSeq[Move]() let start = move.start let dest = move.dest - if (fieldToInd("h8") >= dest) or (fieldToInd("a8") >= dest): + if board[start] != WPawn * ord(color): + return @[] + if (fieldToInd("h8") <= dest and fieldToInd("a8") >= dest and color == Color.White) or + (fieldToInd("h1") <= dest and fieldToInd("a1") >= dest and color == Color.Black): for piece in WKnight..WQueen: promotions.add(getMove(start, dest, piece, color)) return promotions @@ -616,7 +619,7 @@ proc genLegalPawnMoves(chess: Chess, field: int, color: Color): seq[Move] = var moves = chess.genPawnDests(field, color) for dest in moves: if not chess.moveLeadsToCheck(field, dest, color): - var promotions = genPawnPromotion(getMove(field, dest, color), color) + var promotions = chess.board.genPawnPromotion(getMove(field, dest, color), color) if promotions != @[]: res.add(promotions) else: