Code edit (6 edits merged)
Please save this source code
User prompt
Add new piece: Piece name = Minister Movements = 1 square diagonally Special ability = Promotion When it reaches the opponent's last square, it can permanently move like a rook and a knight, and can jump over other pieces when moving like a knight
Code edit (7 edits merged)
Please save this source code
User prompt
Set up the chessboard as 10x11, i.e., 10 rows and 11 columns, and leave the pieces where they are
User prompt
Set up the chessboard as 11x10, i.e., 11 rows and 10 columns, and leave the pieces where they are
User prompt
Set up the chessboard to be 10x11 and leave the pieces where they are
User prompt
Set up the chessboard to be 11x10 and leave the pieces where they are
User prompt
Set up the chessboard to be 11x10 and do not change the position of the pieces
User prompt
Set up the chessboard to be 10x11 and do not change the position of the pieces
User prompt
Set up the chessboard to be 10x11
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
Make the board 10x11, and the extra squares, known as castles, protrude from the left side of the ninth row and the right side of the second row.
Code edit (1 edits merged)
Please save this source code
User prompt
The vizier and the king's royal guards switch places
User prompt
The vizier and the king's royal guards switch places
User prompt
Add a “promotion” mechanic to the game. If the Vizier reaches the opponent's last square, it should be “promoted” and permanently gain the ability to move 8 spaces
User prompt
The vizier closes the diagonal at the beginning of the game.
User prompt
Restrict the vizier's movements to one square left, right, forward, and backward, and when he reaches the opponent's last square, set him to move eight squares left, right, forward, backward, and diagonally.
User prompt
Pawns can move two steps forward on their first move
User prompt
Please fix the bug: 'RangeError: Maximum call stack size exceeded' in or related to this line: 'var moves = piece.getValidMoves();' Line Number: 800
User prompt
Integrate chess rules into the game
Code edit (1 edits merged)
Please save this source code
User prompt
Timurlenk Chess Master
Initial prompt
Make me a Timurlenk chess game
/****
* 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; // 0 = white, 1 = black
self.boardRow = row;
self.boardCol = col;
self.hasMoved = false;
var pieceGraphics = self.attachAsset(type, {
anchorX: 0.5,
anchorY: 0.5
});
if (color === 1) {
pieceGraphics.tint = 0x333333;
}
self.moveTo = function (newRow, newCol) {
self.boardRow = newRow;
self.boardCol = newCol;
self.hasMoved = true;
var boardPos = getBoardPosition(newRow, newCol);
tween(self, {
x: boardPos.x,
y: boardPos.y
}, {
duration: 300
});
};
self.getValidMoves = function () {
var moves = [];
var directions = [];
switch (self.pieceType) {
case 'pawn':
var direction = self.pieceColor === 0 ? -1 : 1;
var startRow = self.pieceColor === 0 ? 9 : 1;
// Forward move
if (isValidSquare(self.boardRow + direction, self.boardCol) && !getPieceAt(self.boardRow + direction, self.boardCol)) {
moves.push({
row: self.boardRow + direction,
col: self.boardCol
});
// Double move from start
if (self.boardRow === startRow && !getPieceAt(self.boardRow + 2 * direction, self.boardCol)) {
moves.push({
row: self.boardRow + 2 * direction,
col: self.boardCol
});
}
}
// Diagonal captures
for (var dc = -1; dc <= 1; dc += 2) {
var newRow = self.boardRow + direction;
var newCol = self.boardCol + dc;
if (isValidSquare(newRow, newCol)) {
var piece = getPieceAt(newRow, newCol);
if (piece && piece.pieceColor !== self.pieceColor) {
moves.push({
row: newRow,
col: newCol
});
}
}
}
break;
case 'rook':
directions = [{
dr: 0,
dc: 1
}, {
dr: 0,
dc: -1
}, {
dr: 1,
dc: 0
}, {
dr: -1,
dc: 0
}];
for (var i = 0; i < directions.length; i++) {
var dir = directions[i];
for (var dist = 1; dist < 11; dist++) {
var newRow = self.boardRow + dir.dr * dist;
var newCol = self.boardCol + dir.dc * dist;
if (!isValidSquare(newRow, newCol)) break;
var piece = getPieceAt(newRow, newCol);
if (piece) {
if (piece.pieceColor !== self.pieceColor) {
moves.push({
row: newRow,
col: newCol
});
}
break;
}
moves.push({
row: newRow,
col: newCol
});
}
}
break;
case 'bishop':
directions = [{
dr: 1,
dc: 1
}, {
dr: 1,
dc: -1
}, {
dr: -1,
dc: 1
}, {
dr: -1,
dc: -1
}];
for (var i = 0; i < directions.length; i++) {
var dir = directions[i];
for (var dist = 1; dist < 11; dist++) {
var newRow = self.boardRow + dir.dr * dist;
var newCol = self.boardCol + dir.dc * dist;
if (!isValidSquare(newRow, newCol)) break;
var piece = getPieceAt(newRow, newCol);
if (piece) {
if (piece.pieceColor !== self.pieceColor) {
moves.push({
row: newRow,
col: newCol
});
}
break;
}
moves.push({
row: newRow,
col: newCol
});
}
}
break;
case 'queen':
directions = [{
dr: 0,
dc: 1
}, {
dr: 0,
dc: -1
}, {
dr: 1,
dc: 0
}, {
dr: -1,
dc: 0
}, {
dr: 1,
dc: 1
}, {
dr: 1,
dc: -1
}, {
dr: -1,
dc: 1
}, {
dr: -1,
dc: -1
}];
for (var i = 0; i < directions.length; i++) {
var dir = directions[i];
for (var dist = 1; dist < 11; dist++) {
var newRow = self.boardRow + dir.dr * dist;
var newCol = self.boardCol + dir.dc * dist;
if (!isValidSquare(newRow, newCol)) break;
var piece = getPieceAt(newRow, newCol);
if (piece) {
if (piece.pieceColor !== self.pieceColor) {
moves.push({
row: newRow,
col: newCol
});
}
break;
}
moves.push({
row: newRow,
col: newCol
});
}
}
break;
case 'king':
directions = [{
dr: 0,
dc: 1
}, {
dr: 0,
dc: -1
}, {
dr: 1,
dc: 0
}, {
dr: -1,
dc: 0
}, {
dr: 1,
dc: 1
}, {
dr: 1,
dc: -1
}, {
dr: -1,
dc: 1
}, {
dr: -1,
dc: -1
}];
for (var i = 0; i < directions.length; i++) {
var dir = directions[i];
var newRow = self.boardRow + dir.dr;
var newCol = self.boardCol + dir.dc;
if (isValidSquare(newRow, newCol)) {
var piece = getPieceAt(newRow, newCol);
if (!piece || piece.pieceColor !== self.pieceColor) {
moves.push({
row: newRow,
col: newCol
});
}
}
}
break;
case 'knight':
var knightMoves = [{
dr: 2,
dc: 1
}, {
dr: 2,
dc: -1
}, {
dr: -2,
dc: 1
}, {
dr: -2,
dc: -1
}, {
dr: 1,
dc: 2
}, {
dr: 1,
dc: -2
}, {
dr: -1,
dc: 2
}, {
dr: -1,
dc: -2
}];
for (var i = 0; i < knightMoves.length; i++) {
var move = knightMoves[i];
var newRow = self.boardRow + move.dr;
var newCol = self.boardCol + move.dc;
if (isValidSquare(newRow, newCol)) {
var piece = getPieceAt(newRow, newCol);
if (!piece || piece.pieceColor !== self.pieceColor) {
moves.push({
row: newRow,
col: newCol
});
}
}
}
break;
case 'warElephant':
// Moves like rook but can jump over pieces
directions = [{
dr: 0,
dc: 1
}, {
dr: 0,
dc: -1
}, {
dr: 1,
dc: 0
}, {
dr: -1,
dc: 0
}];
for (var i = 0; i < directions.length; i++) {
var dir = directions[i];
for (var dist = 1; dist <= 3; dist++) {
var newRow = self.boardRow + dir.dr * dist;
var newCol = self.boardCol + dir.dc * dist;
if (!isValidSquare(newRow, newCol)) break;
var piece = getPieceAt(newRow, newCol);
if (!piece || piece.pieceColor !== self.pieceColor) {
moves.push({
row: newRow,
col: newCol
});
}
}
}
break;
case 'siegeTower':
// Enhanced diagonal movement
directions = [{
dr: 1,
dc: 1
}, {
dr: 1,
dc: -1
}, {
dr: -1,
dc: 1
}, {
dr: -1,
dc: -1
}];
for (var i = 0; i < directions.length; i++) {
var dir = directions[i];
for (var dist = 1; dist <= 4; dist++) {
var newRow = self.boardRow + dir.dr * dist;
var newCol = self.boardCol + dir.dc * dist;
if (!isValidSquare(newRow, newCol)) break;
var piece = getPieceAt(newRow, newCol);
if (piece) {
if (piece.pieceColor !== self.pieceColor) {
moves.push({
row: newRow,
col: newCol
});
}
break;
}
moves.push({
row: newRow,
col: newCol
});
}
}
break;
case 'royalGuard':
// Moves like king but with special abilities
directions = [{
dr: 0,
dc: 1
}, {
dr: 0,
dc: -1
}, {
dr: 1,
dc: 0
}, {
dr: -1,
dc: 0
}, {
dr: 1,
dc: 1
}, {
dr: 1,
dc: -1
}, {
dr: -1,
dc: 1
}, {
dr: -1,
dc: -1
}];
for (var i = 0; i < directions.length; i++) {
var dir = directions[i];
for (var dist = 1; dist <= 2; dist++) {
var newRow = self.boardRow + dir.dr * dist;
var newCol = self.boardCol + dir.dc * dist;
if (!isValidSquare(newRow, newCol)) break;
var piece = getPieceAt(newRow, newCol);
if (piece) {
if (piece.pieceColor !== self.pieceColor) {
moves.push({
row: newRow,
col: newCol
});
}
break;
}
moves.push({
row: newRow,
col: newCol
});
}
}
break;
}
return moves;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
var BOARD_WIDTH = 10;
var BOARD_HEIGHT = 11;
var SQUARE_SIZE = 180;
var BOARD_START_X = (2048 - BOARD_WIDTH * SQUARE_SIZE) / 2;
var BOARD_START_Y = (2732 - BOARD_HEIGHT * SQUARE_SIZE) / 2;
var boardSquares = [];
var pieces = [];
var selectedPiece = null;
var highlightedSquares = [];
var currentPlayer = 0; // 0 = white, 1 = black
var gameStarted = false;
function getBoardPosition(row, col) {
return {
x: BOARD_START_X + col * SQUARE_SIZE + SQUARE_SIZE / 2,
y: BOARD_START_Y + row * SQUARE_SIZE + SQUARE_SIZE / 2
};
}
function getBoardCoordinates(x, y) {
var col = Math.floor((x - BOARD_START_X) / SQUARE_SIZE);
var row = Math.floor((y - BOARD_START_Y) / SQUARE_SIZE);
return {
row: row,
col: col
};
}
function isValidSquare(row, col) {
return row >= 0 && row < BOARD_HEIGHT && col >= 0 && col < BOARD_WIDTH;
}
function getPieceAt(row, col) {
for (var i = 0; i < pieces.length; i++) {
if (pieces[i].boardRow === row && pieces[i].boardCol === col) {
return pieces[i];
}
}
return null;
}
function createBoard() {
for (var row = 0; row < BOARD_HEIGHT; row++) {
boardSquares[row] = [];
for (var col = 0; col < BOARD_WIDTH; col++) {
var isLight = (row + col) % 2 === 0;
var square = game.addChild(LK.getAsset(isLight ? 'lightSquare' : 'darkSquare', {
anchorX: 0.5,
anchorY: 0.5
}));
var pos = getBoardPosition(row, col);
square.x = pos.x;
square.y = pos.y;
square.boardRow = row;
square.boardCol = col;
boardSquares[row][col] = square;
}
}
}
function createPieces() {
// Create pieces for both players
var pieceSetup = [
// White pieces (bottom)
{
type: 'rook',
color: 0,
row: 10,
col: 0
}, {
type: 'knight',
color: 0,
row: 10,
col: 1
}, {
type: 'warElephant',
color: 0,
row: 10,
col: 2
}, {
type: 'bishop',
color: 0,
row: 10,
col: 3
}, {
type: 'queen',
color: 0,
row: 10,
col: 4
}, {
type: 'king',
color: 0,
row: 10,
col: 5
}, {
type: 'bishop',
color: 0,
row: 10,
col: 6
}, {
type: 'siegeTower',
color: 0,
row: 10,
col: 7
}, {
type: 'knight',
color: 0,
row: 10,
col: 8
}, {
type: 'rook',
color: 0,
row: 10,
col: 9
},
// White royal guards
{
type: 'royalGuard',
color: 0,
row: 9,
col: 4
}, {
type: 'royalGuard',
color: 0,
row: 9,
col: 5
},
// Black pieces (top)
{
type: 'rook',
color: 1,
row: 0,
col: 0
}, {
type: 'knight',
color: 1,
row: 0,
col: 1
}, {
type: 'warElephant',
color: 1,
row: 0,
col: 2
}, {
type: 'bishop',
color: 1,
row: 0,
col: 3
}, {
type: 'queen',
color: 1,
row: 0,
col: 4
}, {
type: 'king',
color: 1,
row: 0,
col: 5
}, {
type: 'bishop',
color: 1,
row: 0,
col: 6
}, {
type: 'siegeTower',
color: 1,
row: 0,
col: 7
}, {
type: 'knight',
color: 1,
row: 0,
col: 8
}, {
type: 'rook',
color: 1,
row: 0,
col: 9
},
// Black royal guards
{
type: 'royalGuard',
color: 1,
row: 1,
col: 4
}, {
type: 'royalGuard',
color: 1,
row: 1,
col: 5
}];
// Add pawns
for (var col = 0; col < BOARD_WIDTH; col++) {
pieceSetup.push({
type: 'pawn',
color: 0,
row: 8,
col: col
}); // White pawns
pieceSetup.push({
type: 'pawn',
color: 1,
row: 2,
col: col
}); // Black pawns
}
for (var i = 0; i < pieceSetup.length; i++) {
var setup = pieceSetup[i];
var piece = game.addChild(new ChessPiece(setup.type, setup.color, setup.row, setup.col));
var pos = getBoardPosition(setup.row, setup.col);
piece.x = pos.x;
piece.y = pos.y;
pieces.push(piece);
}
}
function clearHighlights() {
for (var i = 0; i < highlightedSquares.length; i++) {
highlightedSquares[i].destroy();
}
highlightedSquares = [];
}
function highlightMoves(moves) {
clearHighlights();
for (var i = 0; i < moves.length; i++) {
var move = moves[i];
var highlight = game.addChild(LK.getAsset('highlightSquare', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.5
}));
var pos = getBoardPosition(move.row, move.col);
highlight.x = pos.x;
highlight.y = pos.y;
highlightedSquares.push(highlight);
}
}
function selectPiece(piece) {
if (selectedPiece) {
selectedPiece.removeChild(selectedPiece.selectionHighlight);
selectedPiece.selectionHighlight = null;
}
selectedPiece = piece;
if (piece) {
piece.selectionHighlight = piece.addChild(LK.getAsset('selectedSquare', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3
}));
var validMoves = piece.getValidMoves();
highlightMoves(validMoves);
} else {
clearHighlights();
}
}
function makeMove(piece, targetRow, targetCol) {
var capturedPiece = getPieceAt(targetRow, targetCol);
if (capturedPiece) {
var index = pieces.indexOf(capturedPiece);
if (index > -1) {
pieces.splice(index, 1);
}
capturedPiece.destroy();
LK.getSound('capture').play();
} else {
LK.getSound('move').play();
}
piece.moveTo(targetRow, targetCol);
currentPlayer = 1 - currentPlayer;
selectPiece(null);
// Check for victory conditions
checkGameEnd();
}
function checkGameEnd() {
var whiteKing = null;
var blackKing = null;
for (var i = 0; i < pieces.length; i++) {
if (pieces[i].pieceType === 'king') {
if (pieces[i].pieceColor === 0) {
whiteKing = pieces[i];
} else {
blackKing = pieces[i];
}
}
}
if (!whiteKing) {
LK.setScore(currentPlayer === 1 ? 1 : 0);
LK.showGameOver();
} else if (!blackKing) {
LK.setScore(currentPlayer === 0 ? 1 : 0);
LK.showGameOver();
}
}
function initializeGame() {
createBoard();
createPieces();
gameStarted = true;
}
// Player turn indicator
var turnText = new Text2('White to move', {
size: 80,
fill: 0xFFFFFF
});
turnText.anchor.set(0.5, 0);
LK.gui.top.addChild(turnText);
game.down = function (x, y, obj) {
if (!gameStarted) return;
var coords = getBoardCoordinates(x, y);
if (!isValidSquare(coords.row, coords.col)) return;
var clickedPiece = getPieceAt(coords.row, coords.col);
if (selectedPiece) {
var validMoves = selectedPiece.getValidMoves();
var isValidMove = false;
for (var i = 0; i < validMoves.length; i++) {
if (validMoves[i].row === coords.row && validMoves[i].col === coords.col) {
isValidMove = true;
break;
}
}
if (isValidMove) {
makeMove(selectedPiece, coords.row, coords.col);
return;
}
}
if (clickedPiece && clickedPiece.pieceColor === currentPlayer) {
selectPiece(clickedPiece);
} else {
selectPiece(null);
}
};
game.update = function () {
if (!gameStarted) {
initializeGame();
}
turnText.setText(currentPlayer === 0 ? 'White to move' : 'Black to move');
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,739 @@
-/****
+/****
+* 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; // 0 = white, 1 = black
+ self.boardRow = row;
+ self.boardCol = col;
+ self.hasMoved = false;
+ var pieceGraphics = self.attachAsset(type, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ if (color === 1) {
+ pieceGraphics.tint = 0x333333;
+ }
+ self.moveTo = function (newRow, newCol) {
+ self.boardRow = newRow;
+ self.boardCol = newCol;
+ self.hasMoved = true;
+ var boardPos = getBoardPosition(newRow, newCol);
+ tween(self, {
+ x: boardPos.x,
+ y: boardPos.y
+ }, {
+ duration: 300
+ });
+ };
+ self.getValidMoves = function () {
+ var moves = [];
+ var directions = [];
+ switch (self.pieceType) {
+ case 'pawn':
+ var direction = self.pieceColor === 0 ? -1 : 1;
+ var startRow = self.pieceColor === 0 ? 9 : 1;
+ // Forward move
+ if (isValidSquare(self.boardRow + direction, self.boardCol) && !getPieceAt(self.boardRow + direction, self.boardCol)) {
+ moves.push({
+ row: self.boardRow + direction,
+ col: self.boardCol
+ });
+ // Double move from start
+ if (self.boardRow === startRow && !getPieceAt(self.boardRow + 2 * direction, self.boardCol)) {
+ moves.push({
+ row: self.boardRow + 2 * direction,
+ col: self.boardCol
+ });
+ }
+ }
+ // Diagonal captures
+ for (var dc = -1; dc <= 1; dc += 2) {
+ var newRow = self.boardRow + direction;
+ var newCol = self.boardCol + dc;
+ if (isValidSquare(newRow, newCol)) {
+ var piece = getPieceAt(newRow, newCol);
+ if (piece && piece.pieceColor !== self.pieceColor) {
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ }
+ }
+ break;
+ case 'rook':
+ directions = [{
+ dr: 0,
+ dc: 1
+ }, {
+ dr: 0,
+ dc: -1
+ }, {
+ dr: 1,
+ dc: 0
+ }, {
+ dr: -1,
+ dc: 0
+ }];
+ for (var i = 0; i < directions.length; i++) {
+ var dir = directions[i];
+ for (var dist = 1; dist < 11; dist++) {
+ var newRow = self.boardRow + dir.dr * dist;
+ var newCol = self.boardCol + dir.dc * dist;
+ if (!isValidSquare(newRow, newCol)) break;
+ var piece = getPieceAt(newRow, newCol);
+ if (piece) {
+ if (piece.pieceColor !== self.pieceColor) {
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ break;
+ }
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ }
+ break;
+ case 'bishop':
+ directions = [{
+ dr: 1,
+ dc: 1
+ }, {
+ dr: 1,
+ dc: -1
+ }, {
+ dr: -1,
+ dc: 1
+ }, {
+ dr: -1,
+ dc: -1
+ }];
+ for (var i = 0; i < directions.length; i++) {
+ var dir = directions[i];
+ for (var dist = 1; dist < 11; dist++) {
+ var newRow = self.boardRow + dir.dr * dist;
+ var newCol = self.boardCol + dir.dc * dist;
+ if (!isValidSquare(newRow, newCol)) break;
+ var piece = getPieceAt(newRow, newCol);
+ if (piece) {
+ if (piece.pieceColor !== self.pieceColor) {
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ break;
+ }
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ }
+ break;
+ case 'queen':
+ directions = [{
+ dr: 0,
+ dc: 1
+ }, {
+ dr: 0,
+ dc: -1
+ }, {
+ dr: 1,
+ dc: 0
+ }, {
+ dr: -1,
+ dc: 0
+ }, {
+ dr: 1,
+ dc: 1
+ }, {
+ dr: 1,
+ dc: -1
+ }, {
+ dr: -1,
+ dc: 1
+ }, {
+ dr: -1,
+ dc: -1
+ }];
+ for (var i = 0; i < directions.length; i++) {
+ var dir = directions[i];
+ for (var dist = 1; dist < 11; dist++) {
+ var newRow = self.boardRow + dir.dr * dist;
+ var newCol = self.boardCol + dir.dc * dist;
+ if (!isValidSquare(newRow, newCol)) break;
+ var piece = getPieceAt(newRow, newCol);
+ if (piece) {
+ if (piece.pieceColor !== self.pieceColor) {
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ break;
+ }
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ }
+ break;
+ case 'king':
+ directions = [{
+ dr: 0,
+ dc: 1
+ }, {
+ dr: 0,
+ dc: -1
+ }, {
+ dr: 1,
+ dc: 0
+ }, {
+ dr: -1,
+ dc: 0
+ }, {
+ dr: 1,
+ dc: 1
+ }, {
+ dr: 1,
+ dc: -1
+ }, {
+ dr: -1,
+ dc: 1
+ }, {
+ dr: -1,
+ dc: -1
+ }];
+ for (var i = 0; i < directions.length; i++) {
+ var dir = directions[i];
+ var newRow = self.boardRow + dir.dr;
+ var newCol = self.boardCol + dir.dc;
+ if (isValidSquare(newRow, newCol)) {
+ var piece = getPieceAt(newRow, newCol);
+ if (!piece || piece.pieceColor !== self.pieceColor) {
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ }
+ }
+ break;
+ case 'knight':
+ var knightMoves = [{
+ dr: 2,
+ dc: 1
+ }, {
+ dr: 2,
+ dc: -1
+ }, {
+ dr: -2,
+ dc: 1
+ }, {
+ dr: -2,
+ dc: -1
+ }, {
+ dr: 1,
+ dc: 2
+ }, {
+ dr: 1,
+ dc: -2
+ }, {
+ dr: -1,
+ dc: 2
+ }, {
+ dr: -1,
+ dc: -2
+ }];
+ for (var i = 0; i < knightMoves.length; i++) {
+ var move = knightMoves[i];
+ var newRow = self.boardRow + move.dr;
+ var newCol = self.boardCol + move.dc;
+ if (isValidSquare(newRow, newCol)) {
+ var piece = getPieceAt(newRow, newCol);
+ if (!piece || piece.pieceColor !== self.pieceColor) {
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ }
+ }
+ break;
+ case 'warElephant':
+ // Moves like rook but can jump over pieces
+ directions = [{
+ dr: 0,
+ dc: 1
+ }, {
+ dr: 0,
+ dc: -1
+ }, {
+ dr: 1,
+ dc: 0
+ }, {
+ dr: -1,
+ dc: 0
+ }];
+ for (var i = 0; i < directions.length; i++) {
+ var dir = directions[i];
+ for (var dist = 1; dist <= 3; dist++) {
+ var newRow = self.boardRow + dir.dr * dist;
+ var newCol = self.boardCol + dir.dc * dist;
+ if (!isValidSquare(newRow, newCol)) break;
+ var piece = getPieceAt(newRow, newCol);
+ if (!piece || piece.pieceColor !== self.pieceColor) {
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ }
+ }
+ break;
+ case 'siegeTower':
+ // Enhanced diagonal movement
+ directions = [{
+ dr: 1,
+ dc: 1
+ }, {
+ dr: 1,
+ dc: -1
+ }, {
+ dr: -1,
+ dc: 1
+ }, {
+ dr: -1,
+ dc: -1
+ }];
+ for (var i = 0; i < directions.length; i++) {
+ var dir = directions[i];
+ for (var dist = 1; dist <= 4; dist++) {
+ var newRow = self.boardRow + dir.dr * dist;
+ var newCol = self.boardCol + dir.dc * dist;
+ if (!isValidSquare(newRow, newCol)) break;
+ var piece = getPieceAt(newRow, newCol);
+ if (piece) {
+ if (piece.pieceColor !== self.pieceColor) {
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ break;
+ }
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ }
+ break;
+ case 'royalGuard':
+ // Moves like king but with special abilities
+ directions = [{
+ dr: 0,
+ dc: 1
+ }, {
+ dr: 0,
+ dc: -1
+ }, {
+ dr: 1,
+ dc: 0
+ }, {
+ dr: -1,
+ dc: 0
+ }, {
+ dr: 1,
+ dc: 1
+ }, {
+ dr: 1,
+ dc: -1
+ }, {
+ dr: -1,
+ dc: 1
+ }, {
+ dr: -1,
+ dc: -1
+ }];
+ for (var i = 0; i < directions.length; i++) {
+ var dir = directions[i];
+ for (var dist = 1; dist <= 2; dist++) {
+ var newRow = self.boardRow + dir.dr * dist;
+ var newCol = self.boardCol + dir.dc * dist;
+ if (!isValidSquare(newRow, newCol)) break;
+ var piece = getPieceAt(newRow, newCol);
+ if (piece) {
+ if (piece.pieceColor !== self.pieceColor) {
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ break;
+ }
+ moves.push({
+ row: newRow,
+ col: newCol
+ });
+ }
+ }
+ break;
+ }
+ return moves;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x2c3e50
+});
+
+/****
+* Game Code
+****/
+var BOARD_WIDTH = 10;
+var BOARD_HEIGHT = 11;
+var SQUARE_SIZE = 180;
+var BOARD_START_X = (2048 - BOARD_WIDTH * SQUARE_SIZE) / 2;
+var BOARD_START_Y = (2732 - BOARD_HEIGHT * SQUARE_SIZE) / 2;
+var boardSquares = [];
+var pieces = [];
+var selectedPiece = null;
+var highlightedSquares = [];
+var currentPlayer = 0; // 0 = white, 1 = black
+var gameStarted = false;
+function getBoardPosition(row, col) {
+ return {
+ x: BOARD_START_X + col * SQUARE_SIZE + SQUARE_SIZE / 2,
+ y: BOARD_START_Y + row * SQUARE_SIZE + SQUARE_SIZE / 2
+ };
+}
+function getBoardCoordinates(x, y) {
+ var col = Math.floor((x - BOARD_START_X) / SQUARE_SIZE);
+ var row = Math.floor((y - BOARD_START_Y) / SQUARE_SIZE);
+ return {
+ row: row,
+ col: col
+ };
+}
+function isValidSquare(row, col) {
+ return row >= 0 && row < BOARD_HEIGHT && col >= 0 && col < BOARD_WIDTH;
+}
+function getPieceAt(row, col) {
+ for (var i = 0; i < pieces.length; i++) {
+ if (pieces[i].boardRow === row && pieces[i].boardCol === col) {
+ return pieces[i];
+ }
+ }
+ return null;
+}
+function createBoard() {
+ for (var row = 0; row < BOARD_HEIGHT; row++) {
+ boardSquares[row] = [];
+ for (var col = 0; col < BOARD_WIDTH; col++) {
+ var isLight = (row + col) % 2 === 0;
+ var square = game.addChild(LK.getAsset(isLight ? 'lightSquare' : 'darkSquare', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ }));
+ var pos = getBoardPosition(row, col);
+ square.x = pos.x;
+ square.y = pos.y;
+ square.boardRow = row;
+ square.boardCol = col;
+ boardSquares[row][col] = square;
+ }
+ }
+}
+function createPieces() {
+ // Create pieces for both players
+ var pieceSetup = [
+ // White pieces (bottom)
+ {
+ type: 'rook',
+ color: 0,
+ row: 10,
+ col: 0
+ }, {
+ type: 'knight',
+ color: 0,
+ row: 10,
+ col: 1
+ }, {
+ type: 'warElephant',
+ color: 0,
+ row: 10,
+ col: 2
+ }, {
+ type: 'bishop',
+ color: 0,
+ row: 10,
+ col: 3
+ }, {
+ type: 'queen',
+ color: 0,
+ row: 10,
+ col: 4
+ }, {
+ type: 'king',
+ color: 0,
+ row: 10,
+ col: 5
+ }, {
+ type: 'bishop',
+ color: 0,
+ row: 10,
+ col: 6
+ }, {
+ type: 'siegeTower',
+ color: 0,
+ row: 10,
+ col: 7
+ }, {
+ type: 'knight',
+ color: 0,
+ row: 10,
+ col: 8
+ }, {
+ type: 'rook',
+ color: 0,
+ row: 10,
+ col: 9
+ },
+ // White royal guards
+ {
+ type: 'royalGuard',
+ color: 0,
+ row: 9,
+ col: 4
+ }, {
+ type: 'royalGuard',
+ color: 0,
+ row: 9,
+ col: 5
+ },
+ // Black pieces (top)
+ {
+ type: 'rook',
+ color: 1,
+ row: 0,
+ col: 0
+ }, {
+ type: 'knight',
+ color: 1,
+ row: 0,
+ col: 1
+ }, {
+ type: 'warElephant',
+ color: 1,
+ row: 0,
+ col: 2
+ }, {
+ type: 'bishop',
+ color: 1,
+ row: 0,
+ col: 3
+ }, {
+ type: 'queen',
+ color: 1,
+ row: 0,
+ col: 4
+ }, {
+ type: 'king',
+ color: 1,
+ row: 0,
+ col: 5
+ }, {
+ type: 'bishop',
+ color: 1,
+ row: 0,
+ col: 6
+ }, {
+ type: 'siegeTower',
+ color: 1,
+ row: 0,
+ col: 7
+ }, {
+ type: 'knight',
+ color: 1,
+ row: 0,
+ col: 8
+ }, {
+ type: 'rook',
+ color: 1,
+ row: 0,
+ col: 9
+ },
+ // Black royal guards
+ {
+ type: 'royalGuard',
+ color: 1,
+ row: 1,
+ col: 4
+ }, {
+ type: 'royalGuard',
+ color: 1,
+ row: 1,
+ col: 5
+ }];
+ // Add pawns
+ for (var col = 0; col < BOARD_WIDTH; col++) {
+ pieceSetup.push({
+ type: 'pawn',
+ color: 0,
+ row: 8,
+ col: col
+ }); // White pawns
+ pieceSetup.push({
+ type: 'pawn',
+ color: 1,
+ row: 2,
+ col: col
+ }); // Black pawns
+ }
+ for (var i = 0; i < pieceSetup.length; i++) {
+ var setup = pieceSetup[i];
+ var piece = game.addChild(new ChessPiece(setup.type, setup.color, setup.row, setup.col));
+ var pos = getBoardPosition(setup.row, setup.col);
+ piece.x = pos.x;
+ piece.y = pos.y;
+ pieces.push(piece);
+ }
+}
+function clearHighlights() {
+ for (var i = 0; i < highlightedSquares.length; i++) {
+ highlightedSquares[i].destroy();
+ }
+ highlightedSquares = [];
+}
+function highlightMoves(moves) {
+ clearHighlights();
+ for (var i = 0; i < moves.length; i++) {
+ var move = moves[i];
+ var highlight = game.addChild(LK.getAsset('highlightSquare', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ alpha: 0.5
+ }));
+ var pos = getBoardPosition(move.row, move.col);
+ highlight.x = pos.x;
+ highlight.y = pos.y;
+ highlightedSquares.push(highlight);
+ }
+}
+function selectPiece(piece) {
+ if (selectedPiece) {
+ selectedPiece.removeChild(selectedPiece.selectionHighlight);
+ selectedPiece.selectionHighlight = null;
+ }
+ selectedPiece = piece;
+ if (piece) {
+ piece.selectionHighlight = piece.addChild(LK.getAsset('selectedSquare', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ alpha: 0.3
+ }));
+ var validMoves = piece.getValidMoves();
+ highlightMoves(validMoves);
+ } else {
+ clearHighlights();
+ }
+}
+function makeMove(piece, targetRow, targetCol) {
+ var capturedPiece = getPieceAt(targetRow, targetCol);
+ if (capturedPiece) {
+ var index = pieces.indexOf(capturedPiece);
+ if (index > -1) {
+ pieces.splice(index, 1);
+ }
+ capturedPiece.destroy();
+ LK.getSound('capture').play();
+ } else {
+ LK.getSound('move').play();
+ }
+ piece.moveTo(targetRow, targetCol);
+ currentPlayer = 1 - currentPlayer;
+ selectPiece(null);
+ // Check for victory conditions
+ checkGameEnd();
+}
+function checkGameEnd() {
+ var whiteKing = null;
+ var blackKing = null;
+ for (var i = 0; i < pieces.length; i++) {
+ if (pieces[i].pieceType === 'king') {
+ if (pieces[i].pieceColor === 0) {
+ whiteKing = pieces[i];
+ } else {
+ blackKing = pieces[i];
+ }
+ }
+ }
+ if (!whiteKing) {
+ LK.setScore(currentPlayer === 1 ? 1 : 0);
+ LK.showGameOver();
+ } else if (!blackKing) {
+ LK.setScore(currentPlayer === 0 ? 1 : 0);
+ LK.showGameOver();
+ }
+}
+function initializeGame() {
+ createBoard();
+ createPieces();
+ gameStarted = true;
+}
+// Player turn indicator
+var turnText = new Text2('White to move', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+turnText.anchor.set(0.5, 0);
+LK.gui.top.addChild(turnText);
+game.down = function (x, y, obj) {
+ if (!gameStarted) return;
+ var coords = getBoardCoordinates(x, y);
+ if (!isValidSquare(coords.row, coords.col)) return;
+ var clickedPiece = getPieceAt(coords.row, coords.col);
+ if (selectedPiece) {
+ var validMoves = selectedPiece.getValidMoves();
+ var isValidMove = false;
+ for (var i = 0; i < validMoves.length; i++) {
+ if (validMoves[i].row === coords.row && validMoves[i].col === coords.col) {
+ isValidMove = true;
+ break;
+ }
+ }
+ if (isValidMove) {
+ makeMove(selectedPiece, coords.row, coords.col);
+ return;
+ }
+ }
+ if (clickedPiece && clickedPiece.pieceColor === currentPlayer) {
+ selectPiece(clickedPiece);
+ } else {
+ selectPiece(null);
+ }
+};
+game.update = function () {
+ if (!gameStarted) {
+ initializeGame();
+ }
+ turnText.setText(currentPlayer === 0 ? 'White to move' : 'Black to move');
+};
\ No newline at end of file