bugfix chess: moveToNotation promotion part wrong

The function moveToNotation added the promotion character
even if a piece and not a pawn moves to the final rank.
master
TiynGER 4 years ago
parent 4cac1ba6bf
commit 087da7d3f1

@ -154,7 +154,7 @@ proc fieldToInd(field: string): int =
proc indToField(ind: int): string = proc indToField(ind: int): string =
## Calculate and returns field name from board index `ind`. ## Calculate and returns field name from board index `ind`.
let line = (int)ind/10-1 let line = int(ind / 10 - 1)
let file_ind = 7 - ((ind) %% 10 - 1) let file_ind = 7 - ((ind) %% 10 - 1)
for file, i in FileChar: for file, i in FileChar:
if FileChar[file] == file_ind: if FileChar[file] == file_ind:
@ -164,7 +164,7 @@ proc getMove(start: int, dest: int, prom: int, color: Color): Move =
## Get a move object of the `color` player from `start` to `dest` with an ## Get a move object of the `color` player from `start` to `dest` with an
## eventual promition to `prom`. ## eventual promition to `prom`.
var move = Move(start: start, dest: dest, prom: prom * ord(color), color: color) var move = Move(start: start, dest: dest, prom: prom * ord(color), color: color)
if (WKnight > prom or WQueen < prom): if (prom < WKnight or prom > WQueen):
move.prom = WQueen move.prom = WQueen
return move return move
@ -183,30 +183,29 @@ proc notationToMove*(notation: string, color: Color): Move =
move = getMove(start, dest, color) move = getMove(start, dest, color)
if (len(notation) > 4): if (len(notation) > 4):
var promStr = $notation[4] var promStr = $notation[4]
var prom: int let prom = case promStr:
case promStr:
of "Q":
prom = WQueen * ord(color)
of "R": of "R":
prom = WRook * ord(color) WRook * ord(color)
of "B": of "B":
prom = WBishop * ord(color) WBishop * ord(color)
of "N": of "N":
prom = WKnight * ord(color) WKnight * ord(color)
else:
WQueen * ord(color)
move = getMove(start, dest, prom, color) move = getMove(start, dest, prom, color)
return move return move
proc moveToNotation*(move: Move): string = proc moveToNotation*(move: Move, board: Board): string =
## Convert and return a `move` object to simplified algebraic chess notation. ## Convert and return a `move` object to simplified algebraic chess notation.
var res = "" var res: string
var start = indToField(move.start) var start = indToField(move.start)
res.add(start) res.add(start)
var dest = indToField(move.dest) var dest = indToField(move.dest)
res.add(dest) res.add(dest)
var color = move.color var color = move.color
var prom = PieceChar[move.prom] var prom = PieceChar[move.prom]
if (color == Color.White and dest[1] == '8') or (color == Color.Black and if abs(board[move.start]) == WPawn and ((color == Color.White and dest[1] ==
dest[1] == '1'): '8') or (color == Color.Black and dest[1] == '1')):
res.add(prom) res.add(prom)
return res return res
@ -266,10 +265,10 @@ proc initChess(board: array[0..63, int], color: Color): Chess =
let board = initBoard(board) let board = initBoard(board)
let compare = initBoard() let compare = initBoard()
var same_piece: bool var same_piece: bool
var wk = false var wk: bool
var wq = false var wq: bool
var bk = false var bk: bool
var bq = false var bq: bool
if (board[fieldToInd("e1")] == compare[fieldToInd("e1")]): if (board[fieldToInd("e1")] == compare[fieldToInd("e1")]):
if (board[fieldToInd("a1")] == compare[fieldToInd("a1")]): if (board[fieldToInd("a1")] == compare[fieldToInd("a1")]):
wq = true wq = true
@ -283,8 +282,7 @@ proc initChess(board: array[0..63, int], color: Color): Chess =
for ind in board.low..board.high: for ind in board.low..board.high:
same_piece = (board[ind] != compare[ind]) same_piece = (board[ind] != compare[ind])
let chess = Chess(board: board, let chess = Chess(board: board,
to_move: color, previousBoard: @[], previousCastleRights: @[], to_move: color, castleRights: (wk, wq, bk, bq))
fiftyMoveCounter: 0, castleRights: (wk, wq, bk, bq))
return chess return chess
proc echoBoard*(chess: Chess, color: Color) = proc echoBoard*(chess: Chess, color: Color) =
@ -292,21 +290,21 @@ proc echoBoard*(chess: Chess, color: Color) =
## indices from perspecive of `color`. ## indices from perspecive of `color`.
var line_str = "" var line_str = ""
if (color == Color.Black): if (color == Color.Black):
for i in countup(0, len(chess.board)-1): for i in 0..len(chess.board)-1:
if (chess.board[i] == 999): if (chess.board[i] == Block):
continue continue
line_str &= PieceChar[chess.board[i]] & " " line_str &= PieceChar[chess.board[i]] & " "
if ((i + 2) %% 10 == 0): if ((i + 2) %% 10 == 0):
line_str &= $((int)((i)/10)-1) & "\n" line_str &= $(int((i / 10) - 1)) & "\n"
echo line_str echo line_str
echo "h g f e d c b a" echo "h g f e d c b a"
else: else:
for i in countdown(len(chess.board)-1, 0): for i in len(chess.board)-1..0:
if (chess.board[i] == 999): if (chess.board[i] == Block):
continue continue
line_str &= PieceChar[chess.board[i]] & " " line_str &= PieceChar[chess.board[i]] & " "
if ((i - 1) %% 10 == 0): if ((i - 1) %% 10 == 0):
line_str &= $((int)((i)/10)-1) & "\n" line_str &= $(int((i / 10) - 1)) & "\n"
echo line_str echo line_str
echo "a b c d e f g h" echo "a b c d e f g h"
@ -324,7 +322,7 @@ proc genPawnAttackDests(chess: Chess, field: int, color: Color): seq[int] =
if (not dest in chess.board.low..chess.board.high): if (not dest in chess.board.low..chess.board.high):
continue continue
target = chess.board[dest] target = chess.board[dest]
if (target == 999 or ord(color) * target >= 0): if (target == Block or ord(color) * target >= 0):
continue continue
res.add(dest) res.add(dest)
return res return res
@ -388,7 +386,7 @@ proc genKnightDests(chess: Chess, field: int, color: Color): seq[int] =
if (not dest in chess.board.low..chess.board.high): if (not dest in chess.board.low..chess.board.high):
continue continue
target = chess.board[dest] target = chess.board[dest]
if (target == 999 or (ord(color) * target > 0 and ord(color) * target != WEnPassant)): if (target == Block or (ord(color) * target > 0 and ord(color) * target != WEnPassant)):
continue continue
res.add(dest) res.add(dest)
return res return res
@ -407,10 +405,10 @@ proc genBishopDests(chess: Chess, field: int, color: Color): seq[int] =
if (not dest in chess.board.low..chess.board.high): if (not dest in chess.board.low..chess.board.high):
continue continue
target = chess.board[dest] target = chess.board[dest]
while (target != 999 and (ord(color) * target <= 0) or target == while (target != Block and (ord(color) * target <= 0) or abs(target) ==
WEnPassant or target == -WEnPassant): WEnPassant):
res.add(dest) res.add(dest)
if (ord(color) * target < 0 and ord(color) * target > -WEnPassant): if (ord(color) * target < 0 and ord(color) * target > BEnPassant):
break break
dest = dest + move dest = dest + move
target = chess.board[dest] target = chess.board[dest]
@ -430,10 +428,10 @@ proc genRookDests(chess: Chess, field: int, color: Color): seq[int] =
if (not dest in chess.board.low..chess.board.high): if (not dest in chess.board.low..chess.board.high):
continue continue
target = chess.board[dest] target = chess.board[dest]
while (target != 999 and (ord(color) * target <= 0) or target == while (target != Block and (ord(color) * target <= 0) or abs(target) ==
WEnPassant or target == -WEnPassant): WEnPassant):
res.add(dest) res.add(dest)
if (ord(color) * target < 0 and ord(color) * target > -WEnPassant): if (ord(color) * target < 0 and ord(color) * target > BEnPassant):
break break
dest = dest + move dest = dest + move
target = chess.board[dest] target = chess.board[dest]
@ -453,10 +451,10 @@ proc genQueenDests(chess: Chess, field: int, color: Color): seq[int] =
if (not dest in chess.board.low..chess.board.high): if (not dest in chess.board.low..chess.board.high):
continue continue
target = chess.board[dest] target = chess.board[dest]
while (target != 999 and (ord(color) * target <= 0) or target == while (target != Block and (ord(color) * target <= 0) or abs(target) ==
WEnPassant or target == -WEnPassant): WEnPassant):
res.add(dest) res.add(dest)
if (ord(color) * target < 0 and ord(color) * target > -WEnPassant): if (ord(color) * target < 0 and ord(color) * target > BEnPassant):
break break
dest = dest + move dest = dest + move
target = chess.board[dest] target = chess.board[dest]
@ -478,11 +476,11 @@ proc genKingCastleDest(chess: Chess, field: int, color: Color): seq[int] =
if (not dest in chess.board.low..chess.board.high): if (not dest in chess.board.low..chess.board.high):
continue continue
target = chess.board[dest] target = chess.board[dest]
half_dest = field + (int)castle/2 half_dest = field + int(castle / 2)
half_target = chess.board[half_dest] half_target = chess.board[half_dest]
if (target == 999 or (target != 0)): if (target == Block or (target != 0)):
continue continue
if (half_target == 999 or (half_target != 0)): if (half_target == Block or (half_target != 0)):
continue continue
res.add(dest) res.add(dest)
return res return res
@ -501,7 +499,7 @@ proc genKingDests(chess: Chess, field: int, color: Color): seq[int] =
if (not dest in chess.board.low..chess.board.high): if (not dest in chess.board.low..chess.board.high):
continue continue
target = chess.board[dest] target = chess.board[dest]
if (target == 999 or (ord(color) * target > 0 and ord(color) * target != WEnPassant)): if (target == Block or (ord(color) * target > 0 and ord(color) * target != WEnPassant)):
continue continue
res.add(dest) res.add(dest)
res.add(chess.genKingCastleDest(field, color)) res.add(chess.genKingCastleDest(field, color))
@ -512,7 +510,7 @@ proc pieceOn(chess: Chess, color: Color, sequence: seq[int],
## Returns true if the `PieceID` of a given `color` is in `sequence` else ## Returns true if the `PieceID` of a given `color` is in `sequence` else
## wrong. ## wrong.
for check in sequence: for check in sequence:
if chess.board[check] == ord(color) * -1 * pieceID: if chess.board[check] == ord(color) * -pieceID:
return true return true
return false return false
@ -537,7 +535,7 @@ proc isAttacked(chess: Chess, position: int, color: Color): bool =
proc isInCheck(chess: Chess, color: Color): bool = proc isInCheck(chess: Chess, color: Color): bool =
## Returns true if the king of a given `color` is in check in a `chess`. ## Returns true if the king of a given `color` is in check in a `chess`.
var king_pos: int var king_pos: int
for i in countup(0, chess.board.high): for i in 0..chess.board.high:
if chess.board[i] == ord(color) * WKing: if chess.board[i] == ord(color) * WKing:
king_pos = i king_pos = i
return chess.isAttacked(king_pos, color) return chess.isAttacked(king_pos, color)
@ -548,22 +546,20 @@ proc uncheckedMove(chess: var Chess, start: int, dest: int): bool {.discardable.
let piece = chess.board[start] let piece = chess.board[start]
chess.board[start] = 0 chess.board[start] = 0
chess.board[dest] = piece chess.board[dest] = piece
if (start == fieldToInd("e1") or start == fieldToInd("a1")): if start == fieldToInd("e1"):
chess.castleRights.wq = false
if (start == fieldToInd("e1") or start == fieldToInd("h1")):
chess.castleRights.wk = false chess.castleRights.wk = false
if (start == fieldToInd("e8") or start == fieldToInd("a8")):
chess.castleRights.bq = false
if (start == fieldToInd("e8") or start == fieldToInd("h8")):
chess.castleRights.bk = false
if (dest == fieldToInd("e1") or dest == fieldToInd("a1")):
chess.castleRights.wq = false chess.castleRights.wq = false
if (dest == fieldToInd("e1") or dest == fieldToInd("h1")): elif start == fieldToInd("h1"):
chess.castleRights.wk = false chess.castleRights.wk = false
if (dest == fieldToInd("e8") or dest == fieldToInd("a8")): elif start == fieldToInd("a1"):
chess.castleRights.wq = false
elif start == fieldToInd("e8"):
chess.castleRights.bk = false
chess.castleRights.bq = false chess.castleRights.bq = false
if (dest == fieldToInd("e8") or dest == fieldToInd("h8")): elif start == fieldToInd("h8"):
chess.castleRights.bk = false chess.castleRights.bk = false
elif start == fieldToInd("a8"):
chess.castleRights.bq = false
return true return true
proc moveLeadsToCheck(chess: Chess, start: int, dest: int, proc moveLeadsToCheck(chess: Chess, start: int, dest: int,
@ -702,10 +698,10 @@ proc castling(chess: var Chess, kstart: int, dest_kingside: bool,
## of `chess.to_move` ## of `chess.to_move`
if chess.toMove != color: if chess.toMove != color:
return false return false
var kdest = kstart var kdest: int
var rstart: int var rstart: int
var rdest: int var rdest: int
var rights = false var rights: bool
if (dest_kingside): if (dest_kingside):
kdest = kstart + (E+E) kdest = kstart + (E+E)
rstart = kstart + (E+E+E) rstart = kstart + (E+E+E)
@ -723,7 +719,7 @@ proc castling(chess: var Chess, kstart: int, dest_kingside: bool,
else: else:
rights = chess.castleRights.bq rights = chess.castleRights.bq
if (rights): if (rights):
var check = false var check: bool
if (dest_kingside): if (dest_kingside):
check = check or chess.isAttacked(kstart, color) check = check or chess.isAttacked(kstart, color)
check = check or chess.isAttacked(kstart+(E), color) check = check or chess.isAttacked(kstart+(E), color)
@ -758,9 +754,9 @@ proc checkedMove*(chess: var Chess, move: Move): bool {.discardable.} =
return false return false
var sequence = newSeq[Move]() var sequence = newSeq[Move]()
let piece = chess.board[start] let piece = chess.board[start]
var createEnPassant = false var createEnPassant: bool
var capturedEnPassant = false var capturedEnPassant: bool
var fiftyMoveRuleReset = false var fiftyMoveRuleReset: bool
var move: Move var move: Move
move = getMove(start, dest, color) move = getMove(start, dest, color)
if (piece == WPawn * ord(color)): if (piece == WPawn * ord(color)):
@ -770,7 +766,6 @@ proc checkedMove*(chess: var Chess, move: Move): bool {.discardable.} =
if (chess.board[move.dest] != 0): if (chess.board[move.dest] != 0):
fiftyMoveRuleReset = true fiftyMoveRuleReset = true
sequence.add(chess.genLegalMoves(start, color)) sequence.add(chess.genLegalMoves(start, color))
# TODO: don't generate all legal moves, just check if result is legal and reachable
if (move in sequence): if (move in sequence):
chess.board.removeEnPassant(color) chess.board.removeEnPassant(color)
if (piece == WKing * ord(color) and (start - dest == (W+W))): if (piece == WKing * ord(color) and (start - dest == (W+W))):
@ -811,7 +806,7 @@ proc threeMoveRep(chess: Chess): bool =
return false return false
var lastState = chess.previousBoard[chess.previousBoard.high] var lastState = chess.previousBoard[chess.previousBoard.high]
var lastCastleRights = chess.previousCastleRights[chess.previousBoard.high] var lastCastleRights = chess.previousCastleRights[chess.previousBoard.high]
var reps = 0 var reps: int
for stateInd in (chess.previousBoard.low)..(chess.previousBoard.high): for stateInd in (chess.previousBoard.low)..(chess.previousBoard.high):
if (chess.previousBoard[stateInd] == lastState and if (chess.previousBoard[stateInd] == lastState and
chess.previousCastleRights[stateInd] == lastCastleRights): chess.previousCastleRights[stateInd] == lastCastleRights):

@ -184,7 +184,7 @@ proc bestMove*(chess: Chess, depth: int): Move =
var tmpChess = chess var tmpChess = chess
tmpChess.checkedMove(move) tmpChess.checkedMove(move)
var tmpEval = -tmpChess.negaMax(depth, LoVal, HiVal) var tmpEval = -tmpChess.negaMax(depth, LoVal, HiVal)
echo("move:", moveToNotation(move), "; eval:", tmpEval) echo("move:", moveToNotation(move, tmpChess.board), "; eval:", tmpEval)
if tmpEval > bestEval: if tmpEval > bestEval:
bestEval = tmpEval bestEval = tmpEval
bestMove = move bestMove = move

@ -39,7 +39,7 @@ proc playLichessGame(id: string) {.async.} =
chess.echoBoard(chess.toMove) chess.echoBoard(chess.toMove)
if chess.toMove == color: if chess.toMove == color:
echo("engine has to make a move") echo("engine has to make a move")
var bestMove = moveToNotation(chess.bestMove(engineDifficulty)) var bestMove = moveToNotation(chess.bestMove(engineDifficulty), chess.board)
echo(bestMove) echo(bestMove)
discard client.bots.make_move(id, bestMove) discard client.bots.make_move(id, bestMove)

Loading…
Cancel
Save