User prompt
Please fix the bug: 'ReferenceError: getRawMoves is not defined' in or related to this line: 'var moves = getRawMoves(piece, row, col);' Line Number: 803
User prompt
Please fix the following JavaScript ReferenceError in my chess game: Error: 'ReferenceError: getRawMoves is not defined' at line: var moves = getRawMoves(piece, row, col); Your task: - Insert a global function named getRawMoves that defines all legal moves for each chess piece. - Place this function at the correct scope so that it is available where var moves = getRawMoves(piece, row, col) is called. - Confirm that after adding this function, the ReferenceError is gone and the code runs without crashing. - After inserting the function, test a piece move to verify the function actually works. - If necessary, refactor the code to make sure getRawMoves is globally visible and accessible. Here is a simple template you must include inside the global scope: ```js function getRawMoves(piece, row, col) { let moves = []; const type = piece.type; switch (type) { case 'pawn': let dir = piece.color === 'white' ? -1 : 1; moves.push([row + dir, col]); moves.push([row + dir, col - 1]); moves.push([row + dir, col + 1]); break; case 'rook': for (let r=0; r<8; r++) if(r !== row) moves.push([r,col]); for (let c=0; c<8; c++) if(c !== col) moves.push([row,c]); break; case 'knight': const jumps = [ [row+2,col+1],[row+2,col-1],[row-2,col+1],[row-2,col-1], [row+1,col+2],[row+1,col-2],[row-1,col+2],[row-1,col-2] ]; moves.push(...jumps); break; case 'bishop': for (let i=1;i<8;i++){ moves.push([row+i,col+i]); moves.push([row-i,col-i]); moves.push([row+i,col-i]); moves.push([row-i,col+i]); } break; case 'queen': for (let r=0; r<8; r++) if(r !== row) moves.push([r,col]); for (let c=0; c<8; c++) if(c !== col) moves.push([row,c]); for (let i=1;i<8;i++){ moves.push([row+i,col+i]); moves.push([row-i,col-i]); moves.push([row+i,col-i]); moves.push([row-i,col+i]); } break; case 'king': for (let dr=-1; dr<=1; dr++){ for (let dc=-1; dc<=1; dc++){ if(dr!==0 || dc!==0) moves.push([row+dr,col+dc]); } } break; } return moves; }
User prompt
Please fix the bug: 'ReferenceError: getRawMoves is not defined' in or related to this line: 'var moves = getRawMoves(piece, row, col);' Line Number: 820
User prompt
Please fix the bug: 'ReferenceError: getRawMoves is not defined' in or related to this line: 'var moves = getRawMoves(piece, row, col);' Line Number: 820
User prompt
Please add the following missing function to my JavaScript chess code to fix the error 'ReferenceError: getRawMoves is not defined': ```js function getRawMoves(piece, row, col) { let moves = []; const type = piece.type; switch (type) { case 'pawn': // Pawn: move forward 1 (no capture), optionally 2 if on starting rank // capture diagonally let dir = piece.color === 'white' ? -1 : 1; moves.push([row + dir, col]); if ((piece.color === 'white' && row === 6) || (piece.color === 'black' && row === 1)) { moves.push([row + 2*dir, col]); } moves.push([row + dir, col - 1]); moves.push([row + dir, col + 1]); break; case 'rook': // rook: horizontal and vertical in all directions for (let r = 0; r < 8; r++) { if (r !== row) moves.push([r, col]); } for (let c = 0; c < 8; c++) { if (c !== col) moves.push([row, c]); } break; case 'knight': // knight: L-shaped const knightMoves = [ [row+2,col+1], [row+2,col-1], [row-2,col+1], [row-2,col-1], [row+1,col+2], [row+1,col-2], [row-1,col+2], [row-1,col-2], ]; moves.push(...knightMoves); break; case 'bishop': // bishop: diagonals for (let i=1; i<8; i++) { moves.push([row+i, col+i]); moves.push([row-i, col-i]); moves.push([row+i, col-i]); moves.push([row-i, col+i]); } break; case 'queen': // queen: rook + bishop for (let r = 0; r < 8; r++) { if (r !== row) moves.push([r, col]); } for (let c = 0; c < 8; c++) { if (c !== col) moves.push([row, c]); } for (let i=1; i<8; i++) { moves.push([row+i, col+i]); moves.push([row-i, col-i]); moves.push([row+i, col-i]); moves.push([row-i, col+i]); } break; case 'king': // king: one square any direction for (let dr=-1; dr<=1; dr++) { for (let dc=-1; dc<=1; dc++) { if (dr !== 0 || dc !== 0) moves.push([row+dr,col+dc]); } } break; } return moves; }
User prompt
Please fix the bug: 'ReferenceError: getRawMoves is not defined' in or related to this line: 'var moves = getRawMoves(piece, row, col);' Line Number: 820
User prompt
Please fix the bug: 'ReferenceError: getRawMoves is not defined' in or related to this line: 'var moves = getRawMoves(piece, row, col);' Line Number: 820
User prompt
Please fix the bug: 'ReferenceError: getRawMoves is not defined' in or related to this line: 'var moves = getRawMoves(piece, row, col);' Line Number: 820
User prompt
Please fix the bug: 'ReferenceError: getRawMoves is not defined' in or related to this line: 'var moves = getRawMoves(piece, row, col);' Line Number: 820
User prompt
Game ends while there is no check fix this
User prompt
Rakip taşı ilerlerken animasyon olsun dedim ama geldiği yönden değil rastgele yönden geliyor bunu düzelt taş neredeyse o anda olduğu yerden ilerlesin ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Rakip (ai) bizim taşımızı yediğinde taşımız yok olmuyor bunu düzelt
User prompt
Daha smooth animasyonlar koy ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Oyunuc ve ai sırası geldiğinde sadece 1 hamle yapabilir
User prompt
Ai hamle yaparken taşları bir anda ışınlanmasın hareket ederek orayq ilerlesin ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Ben taşımı tutup hareket ettirip koymadan rakip hamle yapmasın
User prompt
Rakip yapay zeka hamle yapmadan önce 1 sn beklesin
User prompt
Pinleme (bağlama) kuralını ekle: Bir taş, hareket ettiğinde kendi şahını açıkta bırakıyorsa o taş hareket edemez. Pinli taş sadece şahı koruyacak yönde hamle yapabilir. Yapay zeka ve oyuncu bu kurala uymalıdır.
User prompt
Satranç tahtasında şah taşını asla yenme, şah mat ile bitir."
User prompt
Taşlara tıkladığımız zaman taşlar uzağa gitmesin tıkladığımız yerde kalsın ordan sürükleyelim
Code edit (1 edits merged)
Please save this source code
User prompt
Chess Master - 2D Classic Chess
Initial prompt
Bir 2D klasik satranç oyunu oluştur. Bu oyun tüm resmi satranç kurallarına uygun olmalı ve aşağıdaki kuralları eksiksiz uygulamalı: 1. **Tahta:** 8x8 klasik siyah-beyaz kareli satranç tahtası. 2. **Başlangıç Dizilimi:** Satranç kurallarına uygun olarak tüm taşlar doğru yerleştirilmeli. 3. **Taş Hareketleri:** Her taş, satranç kurallarına uygun şekilde hareket etmeli (piyon, at, fil, kale, vezir, şah). 4. **Özel Kurallar:** - Şah tehdidi (şah), şah mat, pat, rok (castle), piyon terfisi (promotion), en passant dahil tüm özel kurallar eksiksiz çalışmalı. 5. **Hamle Sırası:** Beyaz oyuncu başlar, sonra siyah hamle yapar. Oyuncu beyaz, yapay zeka siyahı kontrol eder. 6. **Oyuncu Kontrolleri:** Sürükle-bırak yöntemiyle taş hareket ettirme. 7. **Oyun Sonu:** - Şah mat olursa "Kazandın!" veya "Kaybettin!" mesajı gösterilmeli. - Pat olursa "Beraberlik!" mesajı gösterilmeli. --- 8. 🎮 **Zorluk Seçimi:** Oyuncu oyun başında aşağıdaki üç zorluk seviyesinden birini seçebilmeli: - **Kolay (Easy):** - Yapay zeka rastgele ama kurallara uygun hamleler yapar. - Kısa vadeli tehditleri görmez. - **Orta (Medium):** - Yapay zeka tehditleri analiz eder, taş koruması yapar. - Basit kombinasyonlar ve şah tehdidini engellemeye çalışır. - **Zor (Hard):** - Yapay zeka en iyi hamleyi seçmek için birden fazla hamle ileriye bakar (en az 2-3 hamle derinlikli). - Mat etme fırsatlarını değerlendirir, vezir kazanma veya piyon ilerletmeyi planlar. - Açıkta kalan taşlara saldırır ve gerektiğinde savunma yapar. Yapay zekanın mantığı seçilen zorluk seviyesine göre ayarlanmalı. Oyun başlamadan önce "Kolay", "Orta" ve "Zor" butonlarıyla bu seçim yapılabilmeli. Seçilen zorluk, yapay zekanın stratejik gücünü belirlemeli. --- 9. **Tasarım:** - 2 boyutlu sade, temiz ikonlar kullanılmalı. - Mobil uyumlu ve tüm ekran boyutlarına uygun olmalı. Sadece asset (taş görselleri, ses efektleri vs.) eksik olacak şekilde tam çalışan, zorluk seçilebilir bir satranç oyunu oluştur.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var ChessPiece = Container.expand(function (type, color, row, col) { var self = Container.call(this); self.pieceType = type; self.pieceColor = color; self.boardRow = row; self.boardCol = col; self.hasMoved = false; var assetId = color + type.charAt(0).toUpperCase() + type.slice(1); var pieceGraphics = self.attachAsset(assetId, { anchorX: 0.5, anchorY: 0.5 }); self.updateBoardPosition = function (newRow, newCol) { self.boardRow = newRow; self.boardCol = newCol; self.hasMoved = true; }; return self; }); var ChessSquare = Container.expand(function (row, col) { var self = Container.call(this); self.row = row; self.col = col; self.piece = null; self.isHighlighted = false; var isLight = (row + col) % 2 === 0; var squareGraphics = self.attachAsset(isLight ? 'lightSquare' : 'darkSquare', { anchorX: 0.5, anchorY: 0.5 }); self.highlight = function () { if (!self.isHighlighted) { var highlight = LK.getAsset('highlightSquare', { anchorX: 0.5, anchorY: 0.5, alpha: 0.5 }); self.addChild(highlight); self.isHighlighted = true; self.highlightGraphics = highlight; } }; self.removeHighlight = function () { if (self.isHighlighted && self.highlightGraphics) { self.removeChild(self.highlightGraphics); self.isHighlighted = false; self.highlightGraphics = null; } }; self.setPiece = function (piece) { if (self.piece) { self.removeChild(self.piece); } self.piece = piece; if (piece) { self.addChild(piece); piece.x = 0; piece.y = 0; } }; return self; }); var DifficultyButton = Container.expand(function (text, difficulty) { var self = Container.call(this); self.difficulty = difficulty; var buttonBg = self.attachAsset('difficultyButton', { anchorX: 0.5, anchorY: 0.5 }); var buttonText = new Text2(text, { size: 60, fill: 0xFFFFFF }); buttonText.anchor.set(0.5, 0.5); self.addChild(buttonText); self.down = function (x, y, obj) { selectedDifficulty = self.difficulty; startGame(); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x8b4513 }); /**** * Game Code ****/ // Sound effects // UI elements // Board squares // Chess pieces - black pieces // Chess pieces - white pieces // Game state variables var gameState = 'difficulty'; // 'difficulty', 'playing', 'gameOver' var selectedDifficulty = 'medium'; var currentPlayer = 'white'; var board = []; var selectedSquare = null; var possibleMoves = []; var difficultyButtons = []; var draggedPiece = null; var dragOffset = { x: 0, y: 0 }; var aiHasMoved = false; // Board constants var BOARD_SIZE = 8; var SQUARE_SIZE = 230; var BOARD_START_X = 2048 / 2 - BOARD_SIZE * SQUARE_SIZE / 2; var BOARD_START_Y = 2732 / 2 - BOARD_SIZE * SQUARE_SIZE / 2; // Initialize difficulty selection function initializeDifficultySelection() { var easyButton = new DifficultyButton('EASY', 'easy'); easyButton.x = 2048 / 2; easyButton.y = 2732 / 2 - 200; game.addChild(easyButton); difficultyButtons.push(easyButton); var mediumButton = new DifficultyButton('MEDIUM', 'medium'); mediumButton.x = 2048 / 2; mediumButton.y = 2732 / 2; game.addChild(mediumButton); difficultyButtons.push(mediumButton); var hardButton = new DifficultyButton('HARD', 'hard'); hardButton.x = 2048 / 2; hardButton.y = 2732 / 2 + 200; game.addChild(hardButton); difficultyButtons.push(hardButton); var titleText = new Text2('Select Difficulty', { size: 80, fill: 0xFFFFFF }); titleText.anchor.set(0.5, 0.5); titleText.x = 2048 / 2; titleText.y = 2732 / 2 - 400; game.addChild(titleText); } // Start the game after difficulty selection function startGame() { // Remove difficulty buttons for (var i = 0; i < difficultyButtons.length; i++) { game.removeChild(difficultyButtons[i]); } difficultyButtons = []; // Remove title for (var j = game.children.length - 1; j >= 0; j--) { var child = game.children[j]; if (child instanceof Text2) { game.removeChild(child); } } gameState = 'playing'; initializeBoard(); setupInitialPieces(); } // Initialize the chess board function initializeBoard() { board = []; for (var row = 0; row < BOARD_SIZE; row++) { board[row] = []; for (var col = 0; col < BOARD_SIZE; col++) { var square = new ChessSquare(row, col); square.x = BOARD_START_X + col * SQUARE_SIZE + SQUARE_SIZE / 2; square.y = BOARD_START_Y + row * SQUARE_SIZE + SQUARE_SIZE / 2; game.addChild(square); board[row][col] = square; } } } // Setup initial piece positions function setupInitialPieces() { var pieceOrder = ['rook', 'knight', 'bishop', 'queen', 'king', 'bishop', 'knight', 'rook']; // Black pieces (top of board) for (var col = 0; col < 8; col++) { var blackPiece = new ChessPiece(pieceOrder[col], 'black', 0, col); board[0][col].setPiece(blackPiece); var blackPawn = new ChessPiece('pawn', 'black', 1, col); board[1][col].setPiece(blackPawn); } // White pieces (bottom of board) for (var col = 0; col < 8; col++) { var whitePawn = new ChessPiece('pawn', 'white', 6, col); board[6][col].setPiece(whitePawn); var whitePiece = new ChessPiece(pieceOrder[col], 'white', 7, col); board[7][col].setPiece(whitePiece); } } // Get legal moves for a piece function getLegalMoves(piece, fromRow, fromCol) { var moves = []; var type = piece.pieceType; var color = piece.pieceColor; if (type === 'pawn') { var direction = color === 'white' ? -1 : 1; var startRow = color === 'white' ? 6 : 1; // Forward move if (isValidPosition(fromRow + direction, fromCol) && !board[fromRow + direction][fromCol].piece) { moves.push({ row: fromRow + direction, col: fromCol }); // Two squares forward from starting position if (fromRow === startRow && !board[fromRow + 2 * direction][fromCol].piece) { moves.push({ row: fromRow + 2 * direction, col: fromCol }); } } // Diagonal captures for (var dc = -1; dc <= 1; dc += 2) { if (isValidPosition(fromRow + direction, fromCol + dc)) { var targetPiece = board[fromRow + direction][fromCol + dc].piece; if (targetPiece && targetPiece.pieceColor !== color) { moves.push({ row: fromRow + direction, col: fromCol + dc }); } } } } else if (type === 'rook') { var directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]; for (var d = 0; d < directions.length; d++) { var dr = directions[d][0]; var dc = directions[d][1]; for (var i = 1; i < 8; i++) { var newRow = fromRow + dr * i; var newCol = fromCol + dc * i; if (!isValidPosition(newRow, newCol)) break; var targetPiece = board[newRow][newCol].piece; if (targetPiece) { if (targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } break; } else { moves.push({ row: newRow, col: newCol }); } } } } else if (type === 'bishop') { var directions = [[1, 1], [1, -1], [-1, 1], [-1, -1]]; for (var d = 0; d < directions.length; d++) { var dr = directions[d][0]; var dc = directions[d][1]; for (var i = 1; i < 8; i++) { var newRow = fromRow + dr * i; var newCol = fromCol + dc * i; if (!isValidPosition(newRow, newCol)) break; var targetPiece = board[newRow][newCol].piece; if (targetPiece) { if (targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } break; } else { moves.push({ row: newRow, col: newCol }); } } } } else if (type === 'queen') { var directions = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]]; for (var d = 0; d < directions.length; d++) { var dr = directions[d][0]; var dc = directions[d][1]; for (var i = 1; i < 8; i++) { var newRow = fromRow + dr * i; var newCol = fromCol + dc * i; if (!isValidPosition(newRow, newCol)) break; var targetPiece = board[newRow][newCol].piece; if (targetPiece) { if (targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } break; } else { moves.push({ row: newRow, col: newCol }); } } } } else if (type === 'king') { var directions = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]]; for (var d = 0; d < directions.length; d++) { var newRow = fromRow + directions[d][0]; var newCol = fromCol + directions[d][1]; if (isValidPosition(newRow, newCol)) { var targetPiece = board[newRow][newCol].piece; if (!targetPiece || targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } } } } else if (type === 'knight') { var knightMoves = [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]]; for (var m = 0; m < knightMoves.length; m++) { var newRow = fromRow + knightMoves[m][0]; var newCol = fromCol + knightMoves[m][1]; if (isValidPosition(newRow, newCol)) { var targetPiece = board[newRow][newCol].piece; if (!targetPiece || targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } } } } // Filter out moves that would leave own king in check (pin rule) var legalMoves = []; var _loop = function _loop() { move = moves[i]; // Simulate the move fromPiece = board[fromRow][fromCol].piece; toPiece = board[move.row][move.col].piece; // Temporarily move the piece board[fromRow][fromCol].setPiece(null); board[move.row][move.col].setPiece(fromPiece); kingSafe = true; // Find own king position after move kingRow = -1, kingCol = -1; for (r = 0; r < BOARD_SIZE; r++) { for (c = 0; c < BOARD_SIZE; c++) { p = board[r][c].piece; if (p && p.pieceType === 'king' && p.pieceColor === color) { kingRow = r; kingCol = c; } } } // If this piece is the king, update kingRow/kingCol to new position if (type === 'king') { kingRow = move.row; kingCol = move.col; } // Check if any enemy piece can attack the king after this move for (r = 0; r < BOARD_SIZE; r++) { for (c = 0; c < BOARD_SIZE; c++) { enemy = board[r][c].piece; if (enemy && enemy.pieceColor !== color) { enemyMoves = getRawMoves(enemy, r, c); for (j = 0; j < enemyMoves.length; j++) { if (enemyMoves[j].row === kingRow && enemyMoves[j].col === kingCol) { kingSafe = false; break; } } } if (!kingSafe) break; } if (!kingSafe) break; } // Undo the move board[move.row][move.col].setPiece(toPiece); board[fromRow][fromCol].setPiece(fromPiece); if (kingSafe) { legalMoves.push(move); } // Get all pseudo-legal moves for a piece (ignores pins and king safety) function getRawMoves(piece, fromRow, fromCol) { var moves = []; var type = piece.pieceType; var color = piece.pieceColor; if (type === 'pawn') { var direction = color === 'white' ? -1 : 1; // Pawn captures only for (var dc = -1; dc <= 1; dc += 2) { if (isValidPosition(fromRow + direction, fromCol + dc)) { var targetPiece = board[fromRow + direction][fromCol + dc].piece; // For pin check, allow empty or enemy if (!targetPiece || targetPiece.pieceColor !== color) { moves.push({ row: fromRow + direction, col: fromCol + dc }); } } } // En passant not handled in pin check (safe for now) } else if (type === 'rook') { var directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]; for (var d = 0; d < directions.length; d++) { var dr = directions[d][0]; var dc = directions[d][1]; for (var i = 1; i < 8; i++) { var newRow = fromRow + dr * i; var newCol = fromCol + dc * i; if (!isValidPosition(newRow, newCol)) break; var targetPiece = board[newRow][newCol].piece; if (targetPiece) { if (targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } break; } else { moves.push({ row: newRow, col: newCol }); } } } } else if (type === 'bishop') { var directions = [[1, 1], [1, -1], [-1, 1], [-1, -1]]; for (var d = 0; d < directions.length; d++) { var dr = directions[d][0]; var dc = directions[d][1]; for (var i = 1; i < 8; i++) { var newRow = fromRow + dr * i; var newCol = fromCol + dc * i; if (!isValidPosition(newRow, newCol)) break; var targetPiece = board[newRow][newCol].piece; if (targetPiece) { if (targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } break; } else { moves.push({ row: newRow, col: newCol }); } } } } else if (type === 'queen') { var directions = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]]; for (var d = 0; d < directions.length; d++) { var dr = directions[d][0]; var dc = directions[d][1]; for (var i = 1; i < 8; i++) { var newRow = fromRow + dr * i; var newCol = fromCol + dc * i; if (!isValidPosition(newRow, newCol)) break; var targetPiece = board[newRow][newCol].piece; if (targetPiece) { if (targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } break; } else { moves.push({ row: newRow, col: newCol }); } } } } else if (type === 'king') { var directions = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]]; for (var d = 0; d < directions.length; d++) { var newRow = fromRow + directions[d][0]; var newCol = fromCol + directions[d][1]; if (isValidPosition(newRow, newCol)) { var targetPiece = board[newRow][newCol].piece; if (!targetPiece || targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } } } } else if (type === 'knight') { var knightMoves = [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]]; for (var m = 0; m < knightMoves.length; m++) { var newRow = fromRow + knightMoves[m][0]; var newCol = fromCol + knightMoves[m][1]; if (isValidPosition(newRow, newCol)) { var targetPiece = board[newRow][newCol].piece; if (!targetPiece || targetPiece.pieceColor !== color) { moves.push({ row: newRow, col: newCol }); } } } } return moves; } }, move, fromPiece, toPiece, kingSafe, kingRow, kingCol, r, c, p, r, c, enemy, enemyMoves, j; for (var i = 0; i < moves.length; i++) { _loop(); } return legalMoves; } // Check if position is valid on board function isValidPosition(row, col) { return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE; } // Highlight possible moves function highlightMoves(moves) { for (var i = 0; i < moves.length; i++) { board[moves[i].row][moves[i].col].highlight(); } } // Clear all highlights function clearHighlights() { for (var row = 0; row < BOARD_SIZE; row++) { for (var col = 0; col < BOARD_SIZE; col++) { board[row][col].removeHighlight(); } } } // Make a move function makeMove(fromRow, fromCol, toRow, toCol) { var piece = board[fromRow][fromCol].piece; var capturedPiece = board[toRow][toCol].piece; // Prevent capturing the king if (capturedPiece && capturedPiece.pieceType === 'king') { // Invalid move: cannot capture king, revert and return // Return piece to original position if dragged if (draggedPiece) { game.removeChild(draggedPiece); board[fromRow][fromCol].setPiece(draggedPiece); } clearHighlights(); draggedPiece = null; selectedSquare = null; possibleMoves = []; return; } // Calculate target position var targetX = BOARD_START_X + toCol * SQUARE_SIZE + SQUARE_SIZE / 2; var targetY = BOARD_START_Y + toRow * SQUARE_SIZE + SQUARE_SIZE / 2; // Check if this is an AI move (piece is black and not being dragged) var isAIMove = piece.pieceColor === 'black' && !draggedPiece; if (isAIMove) { // For AI moves, animate the piece movement // First, bring piece to game level for animation var currentSquare = board[fromRow][fromCol]; var currentGlobalPos = currentSquare.toGlobal(piece.position); currentSquare.removeChild(piece); game.addChild(piece); piece.x = currentGlobalPos.x; piece.y = currentGlobalPos.y; // Remove piece from old position on board board[fromRow][fromCol].setPiece(null); // Set piece in new position but don't add to square yet board[toRow][toCol].piece = piece; piece.updateBoardPosition(toRow, toCol); // Animate piece to target position tween(piece, { x: targetX, y: targetY }, { duration: 300, easing: tween.easeOut, onFinish: function onFinish() { // Move piece from game to target square game.removeChild(piece); board[toRow][toCol].setPiece(piece); // Play sound after animation if (capturedPiece) { LK.getSound('captureSound').play(); } else { LK.getSound('moveSound').play(); } // Check for pawn promotion if (piece.pieceType === 'pawn') { if (piece.pieceColor === 'white' && toRow === 0 || piece.pieceColor === 'black' && toRow === 7) { // Promote to queen board[toRow][toCol].setPiece(null); var promotedQueen = new ChessPiece('queen', piece.pieceColor, toRow, toCol); board[toRow][toCol].setPiece(promotedQueen); } } // Switch turns currentPlayer = currentPlayer === 'white' ? 'black' : 'white'; // Reset AI move flag when switching turns if (currentPlayer === 'black') { aiHasMoved = false; } // Check for game end conditions if (isCheckmate(currentPlayer)) { if (currentPlayer === 'white') { LK.showGameOver(); } else { LK.showYouWin(); } gameState = 'gameOver'; } else if (isStalemate(currentPlayer)) { LK.showGameOver(); // Show as draw gameState = 'gameOver'; } } }); } else { // For player moves, use instant movement as before // Remove piece from old position board[fromRow][fromCol].setPiece(null); // Place piece in new position board[toRow][toCol].setPiece(piece); piece.updateBoardPosition(toRow, toCol); // Play sound if (capturedPiece) { LK.getSound('captureSound').play(); } else { LK.getSound('moveSound').play(); } // Check for pawn promotion if (piece.pieceType === 'pawn') { if (piece.pieceColor === 'white' && toRow === 0 || piece.pieceColor === 'black' && toRow === 7) { // Promote to queen board[toRow][toCol].setPiece(null); var promotedQueen = new ChessPiece('queen', piece.pieceColor, toRow, toCol); board[toRow][toCol].setPiece(promotedQueen); } } // Switch turns currentPlayer = currentPlayer === 'white' ? 'black' : 'white'; // Reset AI move flag when switching turns if (currentPlayer === 'black') { aiHasMoved = false; } // Check for game end conditions if (isCheckmate(currentPlayer)) { if (currentPlayer === 'white') { LK.showGameOver(); } else { LK.showYouWin(); } gameState = 'gameOver'; } else if (isStalemate(currentPlayer)) { LK.showGameOver(); // Show as draw gameState = 'gameOver'; } } } // Simple checkmate detection function isCheckmate(color) { // Find king var kingPos = null; for (var row = 0; row < BOARD_SIZE; row++) { for (var col = 0; col < BOARD_SIZE; col++) { var piece = board[row][col].piece; if (piece && piece.pieceType === 'king' && piece.pieceColor === color) { kingPos = { row: row, col: col }; break; } } if (kingPos) break; } if (!kingPos) return true; // No king found // Check if any piece can make a legal move for (var row = 0; row < BOARD_SIZE; row++) { for (var col = 0; col < BOARD_SIZE; col++) { var piece = board[row][col].piece; if (piece && piece.pieceColor === color) { var moves = getLegalMoves(piece, row, col); if (moves.length > 0) { return false; // Found a legal move } } } } return true; // No legal moves found } // Simple stalemate detection function isStalemate(color) { // Similar to checkmate but king is not in check return false; // Simplified for now } // AI move selection based on difficulty function getAIMove() { var allMoves = []; // Get all possible moves for AI (black pieces) for (var row = 0; row < BOARD_SIZE; row++) { for (var col = 0; col < BOARD_SIZE; col++) { var piece = board[row][col].piece; if (piece && piece.pieceColor === 'black') { var moves = getLegalMoves(piece, row, col); for (var m = 0; m < moves.length; m++) { // Prevent AI from selecting a move that would capture the king var target = board[moves[m].row][moves[m].col].piece; if (target && target.pieceType === 'king') { continue; } allMoves.push({ fromRow: row, fromCol: col, toRow: moves[m].row, toCol: moves[m].col, piece: piece }); } } } } if (allMoves.length === 0) return null; if (selectedDifficulty === 'easy') { // Random move return allMoves[Math.floor(Math.random() * allMoves.length)]; } else if (selectedDifficulty === 'medium') { // Prefer captures var captures = []; for (var i = 0; i < allMoves.length; i++) { var move = allMoves[i]; if (board[move.toRow][move.toCol].piece) { captures.push(move); } } if (captures.length > 0) { return captures[Math.floor(Math.random() * captures.length)]; } return allMoves[Math.floor(Math.random() * allMoves.length)]; } else { // Hard: prefer captures, then center moves var captures = []; var centerMoves = []; for (var i = 0; i < allMoves.length; i++) { var move = allMoves[i]; if (board[move.toRow][move.toCol].piece) { captures.push(move); } else if (move.toRow >= 3 && move.toRow <= 4 && move.toCol >= 3 && move.toCol <= 4) { centerMoves.push(move); } } if (captures.length > 0) { return captures[Math.floor(Math.random() * captures.length)]; } if (centerMoves.length > 0) { return centerMoves[Math.floor(Math.random() * centerMoves.length)]; } return allMoves[Math.floor(Math.random() * allMoves.length)]; } } // Get board position from screen coordinates function getBoardPosition(x, y) { var col = Math.floor((x - BOARD_START_X) / SQUARE_SIZE); var row = Math.floor((y - BOARD_START_Y) / SQUARE_SIZE); if (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE) { return { row: row, col: col }; } return null; } // Handle mouse/touch down game.down = function (x, y, obj) { if (gameState !== 'playing' || currentPlayer !== 'white') return; var pos = getBoardPosition(x, y); if (!pos) return; var square = board[pos.row][pos.col]; var piece = square.piece; if (piece && piece.pieceColor === 'white') { // Start dragging draggedPiece = piece; // Set drag offset to zero so piece stays at click position dragOffset.x = 0; dragOffset.y = 0; // Clear previous highlights clearHighlights(); // Highlight possible moves possibleMoves = getLegalMoves(piece, pos.row, pos.col); highlightMoves(possibleMoves); selectedSquare = { row: pos.row, col: pos.col }; // Bring piece to front but keep it at current position var tempParent = piece.parent; var currentGlobalPos = tempParent.toGlobal(piece.position); tempParent.removeChild(piece); game.addChild(piece); piece.x = currentGlobalPos.x; piece.y = currentGlobalPos.y; } }; // Handle mouse/touch move game.move = function (x, y, obj) { if (draggedPiece) { draggedPiece.x = x; draggedPiece.y = y; } }; // Handle mouse/touch up game.up = function (x, y, obj) { if (!draggedPiece || gameState !== 'playing') return; var pos = getBoardPosition(x, y); var validMove = false; if (pos && selectedSquare) { // Check if this is a valid move for (var i = 0; i < possibleMoves.length; i++) { if (possibleMoves[i].row === pos.row && possibleMoves[i].col === pos.col) { validMove = true; break; } } if (validMove) { // Prevent player from capturing the king var targetPiece = board[pos.row][pos.col].piece; if (targetPiece && targetPiece.pieceType === 'king') { // Invalid move: cannot capture king, revert and return game.removeChild(draggedPiece); board[selectedSquare.row][selectedSquare.col].setPiece(draggedPiece); clearHighlights(); draggedPiece = null; selectedSquare = null; possibleMoves = []; return; } // Make the move makeMove(selectedSquare.row, selectedSquare.col, pos.row, pos.col); } } if (!validMove && selectedSquare) { // Return piece to original position var originalSquare = board[selectedSquare.row][selectedSquare.col]; game.removeChild(draggedPiece); originalSquare.setPiece(draggedPiece); } // Clean up clearHighlights(); draggedPiece = null; selectedSquare = null; possibleMoves = []; }; // Game update loop game.update = function () { if (gameState === 'playing' && currentPlayer === 'black' && !draggedPiece && !aiHasMoved) { // AI turn - only if player is not currently dragging a piece and AI hasn't moved yet if (LK.ticks % 60 === 0) { // Wait 1 second before AI move aiHasMoved = true; // Set flag immediately to prevent multiple moves LK.setTimeout(function () { var aiMove = getAIMove(); if (aiMove) { makeMove(aiMove.fromRow, aiMove.fromCol, aiMove.toRow, aiMove.toCol); } }, 1000); } } }; // Initialize the game if (gameState === 'difficulty') { initializeDifficultySelection(); }
===================================================================
--- original.js
+++ change.js
@@ -115,8 +115,9 @@
var dragOffset = {
x: 0,
y: 0
};
+var aiHasMoved = false;
// Board constants
var BOARD_SIZE = 8;
var SQUARE_SIZE = 230;
var BOARD_START_X = 2048 / 2 - BOARD_SIZE * SQUARE_SIZE / 2;
@@ -621,8 +622,12 @@
}
}
// Switch turns
currentPlayer = currentPlayer === 'white' ? 'black' : 'white';
+ // Reset AI move flag when switching turns
+ if (currentPlayer === 'black') {
+ aiHasMoved = false;
+ }
// Check for game end conditions
if (isCheckmate(currentPlayer)) {
if (currentPlayer === 'white') {
LK.showGameOver();
@@ -659,8 +664,12 @@
}
}
// Switch turns
currentPlayer = currentPlayer === 'white' ? 'black' : 'white';
+ // Reset AI move flag when switching turns
+ if (currentPlayer === 'black') {
+ aiHasMoved = false;
+ }
// Check for game end conditions
if (isCheckmate(currentPlayer)) {
if (currentPlayer === 'white') {
LK.showGameOver();
@@ -868,12 +877,13 @@
possibleMoves = [];
};
// Game update loop
game.update = function () {
- if (gameState === 'playing' && currentPlayer === 'black' && !draggedPiece) {
- // AI turn - only if player is not currently dragging a piece
+ if (gameState === 'playing' && currentPlayer === 'black' && !draggedPiece && !aiHasMoved) {
+ // AI turn - only if player is not currently dragging a piece and AI hasn't moved yet
if (LK.ticks % 60 === 0) {
// Wait 1 second before AI move
+ aiHasMoved = true; // Set flag immediately to prevent multiple moves
LK.setTimeout(function () {
var aiMove = getAIMove();
if (aiMove) {
makeMove(aiMove.fromRow, aiMove.fromCol, aiMove.toRow, aiMove.toCol);
Chess pawn View from above. In-Game asset. 2d. High contrast. No shadows
Chess king view from above. In-Game asset. 2d. High contrast. No shadows
Chess White king view from top. In-Game asset. 2d. High contrast. No shadows
Chess queen white View from above. In-Game asset. 2d. High contrast. No shadows
Chess black bishop. In-Game asset. 2d. High contrast. No shadows
Chess black queen. In-Game asset. 2d. High contrast. No shadows
Chess black knight. In-Game asset. 2d. High contrast. No shadows
Chess rook view from above. In-Game asset. 2d. High contrast. No shadows
Chess bishop white view frome above. In-Game asset. 2d. High contrast. No shadows
Şimdi içini beyaz renkle doldur