From 4075292cb83a3c06ddded1015b281e5ea1d0faba Mon Sep 17 00:00:00 2001 From: Oceankoh <36119714+Oceankoh@users.noreply.github.com> Date: Sun, 21 Jan 2024 08:15:30 +0800 Subject: [PATCH] enable promotion --- backend/src/chess/engine.ts | 8 ++++---- backend/src/chess/llm.ts | 28 +++++++++++++++------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/backend/src/chess/engine.ts b/backend/src/chess/engine.ts index f9d6d00..5027fd6 100644 --- a/backend/src/chess/engine.ts +++ b/backend/src/chess/engine.ts @@ -112,12 +112,12 @@ export async function interpretMove( fen: string ): Promise { const move = await llmInterpretPrompt(prompt, fen); - if (move instanceof NormalMove) { - console.log('return normal move') - return movePiece(move, fen); - } else if (move instanceof PromotionMove) { + if (move instanceof PromotionMove) { console.log('return promotion move') return promotePiece(move, fen); + } else if (move instanceof NormalMove) { + console.log('return normal move') + return movePiece(move, fen); } else if (move instanceof InvalidMove) { console.log('return failed move') return new FailedMove(move.prompt); diff --git a/backend/src/chess/llm.ts b/backend/src/chess/llm.ts index d3fff13..64ac58f 100644 --- a/backend/src/chess/llm.ts +++ b/backend/src/chess/llm.ts @@ -107,7 +107,7 @@ async function llmCheckMoveValidity( history: [ { role: "user", - parts: `Assume the role of an Next Generation Chess Interpreter. Using the FEN and next move you are to determine whether the next move is legal. The move can be one of the following formats:\n1. (, ), to move a piece from the first square to the second square. For example, (e2, e4) moves the piece at e2 to e4.\n2. (, , ), to promote a pawn to a piece. For example, (e7, e8, q), promotes the pawn at e7 to a queen. The piece can be a 'q' (queen), 'r' (rook), 'b' (bishop), or 'n' (knight).\nIf the move is legal, respond with 'True'. If the move is illegal, respond with 'False'. You should only have either 'True' or 'False' in your response.\nIf you understand, respond with 'Yes, I understand'.`, + parts: `Assume the role of an Next Generation Chess Interpreter. Using the FEN and next move you are to determine whether the next move is legal. The move can be one of the following formats:\n1. (, ), to move a piece from the first square to the second square. For example, (e2, e4) moves the piece at e2 to e4.\n2. (, , ), to promote a pawn to a piece. For example, (e2, e1, q), promotes the pawn at e7 to a queen. The piece can be a 'q' (queen), 'r' (rook), 'b' (bishop), or 'n' (knight).\nIf the move is legal, respond with 'True'. If the move is illegal, respond with 'False'. You should only have either 'True' or 'False' in your response.\nIf you understand, respond with 'Yes, I understand'.`, }, { role: "model", @@ -135,7 +135,19 @@ async function llmCheckMoveValidity( } function parseResponseMove(response: string): Move { - // check if response is in the format (square, square) + // check if response is in the format (square, square) + const promotionRegex = /.*([abcdefgh]\d).*([abcdefgh]\d).*([qrbn]).*/; + const promotionMatch = response.match(promotionRegex); + if (promotionRegex.test(response)) { + const [_, square1, square2, piece] = promotionMatch; + return new PromotionMove( + square1 as Square, + square2 as Square, + piece as "q" | "r" | "b" | "n" + ); + } + + // check if response is in the format (square, square) const moveRegex = /.*([abcdefgh]\d).*([abcdefgh]\d).*/; const moveMatch = response.match(moveRegex); if (moveMatch) { @@ -143,17 +155,7 @@ function parseResponseMove(response: string): Move { return new NormalMove(square1 as Square, square2 as Square); } - // check if response is in the format (square, square) - const promotionRegex = /.*([abcdefgh]\d).*([abcdefgh]\d).*([qrbn]).*/; - const promotionMatch = response.match(promotionRegex); - if (promotionMatch) { - const [_, square1, square2, piece] = promotionMatch; - return new PromotionMove( - square1 as Square, - square2 as Square, - piece as "q" | "r" | "b" | "n" - ); - } + return new InvalidMove( `Prompt generated a response that could not be parsed: ${response}` );