User prompt
Please fix the bug: 'board is not defined' in or related to this line: 'board[i] = [];' Line Number: 139
User prompt
Please fix the bug: 'boardSize is not defined' in or related to this line: 'for (var i = 0; i < boardSize; i++) {' Line Number: 137
Code edit (1 edits merged)
Please save this source code
User prompt
diyelim ki [i][j] ve [i][j+1] de aynı renk bulunsun muhtemel eşleşme için kontrol edilmesi gereken konumlar şunlar [i-1][j-1], [i][j-2], [i+1][j-1], [i-1][j+2], [i][j+2], [i+1][j+2] buna göre yapılabilir hamle kalıp kalmadığını kontrol et ve kalmadıysa oyun sonlansın.
User prompt
diyelim ki [i][j] ve [i][j+1] de aynı renk bulunsun muhtemel eşleşme için kontrol edilmesi gereken konumlar şunlar [i-1][j-1], [i+1][j-1], [i-1][j+2]i [i+1][j+2] buna göre yapılabilir hamle kalıp kalmadığını kontrol et ve kalmadıysa oyun sonlansın.
User prompt
Please fix the bug: 'Timeout.tick error: Cannot read properties of null (reading 'candyType')' in or related to this line: 'candy1.candyType = candy2.candyType;' Line Number: 103
User prompt
eğer yer değişikliği ile eşleme yapılabilir bir hamle kalmadıysa oyunu bitir
User prompt
oyunda sadece en başta spawn olan renkler olsun orta kısımlardan yok edilenlerin yeri üst kısımdakilerde dolsun fakat yeni renkler spawn edilmesin.
User prompt
yok edilen renklerin üstüne yukarıdan renkler bloak halinde gelmeye devam etsin. fakat en üstte oluşan boşluklara yeni renkler eklenmesin. boşluk olarak kalsın
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Uncaught RangeError: Maximum call stack size exceeded' in or related to this line: 'candy1.x = candy2.x;' Line Number: 160
User prompt
Please fix the bug: 'self.checkMatches is not a function' in or related to this line: 'return self.checkMatches().length > 0;' Line Number: 48
User prompt
Please fix the bug: 'self.checkMatches is not a function' in or related to this line: 'return self.checkMatches().length > 0;' Line Number: 48
Code edit (1 edits merged)
Please save this source code
User prompt
hala aynı
User prompt
oyunda hiçbir şey gözükmüyor
User prompt
candy crush benzeri oyun yap
User prompt
hataları düzelt
User prompt
Please fix the bug: 'self.reshuffleBoard is not a function' in or related to this line: 'self.reshuffleBoard();' Line Number: 37
User prompt
Please fix the bug: 'self.hasMatches is not a function' in or related to this line: 'while (self.hasMatches()) {' Line Number: 36
User prompt
Please fix the bug: 'PowerUp is not defined' in or related to this line: 'item = new PowerUp();' Line Number: 44
User prompt
Please fix the bug: 'Candy is not defined' in or related to this line: 'item = new Candy();' Line Number: 46
Code edit (1 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: gameBoard.board[x][y].pop is not a function' in or related to this line: 'gameBoard.board[x][y].pop();' Line Number: 280
Code edit (1 edits merged)
Please save this source code
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Candy = Container.expand(function (i, j, candyType) { var self = Container.call(this); var candyTypes = ["candy1", "candy2", "candy3", "candy4", "candy5", "candy6"]; if (!candyType) { do { candyType = candyTypes[Math.floor(Math.random() * candyTypes.length)]; } while (isPotentialMatch(i, j, candyType)); } self.candyType = candyType; var candyGraphics = self.attachAsset(candyType, { anchorX: 0.5, anchorY: 0.5 }); self.i = i; self.j = j; self.isMoving = false; self.destroyCandy = function () { self.destroy(); }; self.transformToWhite = function () { self.removeChild(candyGraphics); self.candyType = self.candyType + "_white"; candyGraphics = self.attachAsset(self.candyType, { anchorX: 0.5, anchorY: 0.5 }); }; self.moveTo = function (newI, newJ, _onComplete) { self.isMoving = true; self.i = newI; self.j = newJ; tween(self, { x: newI * candySize + candySize / 2 + marginX, y: newJ * candySize + candySize / 2 + marginY }, { duration: 300, onComplete: function onComplete() { self.isMoving = false; if (_onComplete) { _onComplete(); } } }); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ var score = 0; var scoreMultiplier = 1; var scoreTxt = new Text2('Score: 0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(1, 0); // Anchor to the top-right corner LK.gui.topRight.addChild(scoreTxt); function updateScore(points) { score += points * scoreMultiplier; scoreTxt.setText('Score: ' + score); } var board = []; var boardSize = 9; var candySize = Math.min(2048, 2732) / (boardSize + 0.5); var selectedCandy = null; var marginY = (2732 - boardSize * candySize) / 2; var marginX = (2048 - boardSize * candySize) / 2; var isProcessing = false; function isPotentialMatch(i, j, candyType) { // Oyun başlangıcında 3'lü eşleşmeleri engelliyoruz if (i >= 2 && board[i - 1][j] && board[i - 2][j] && board[i - 1][j].candyType === candyType && board[i - 2][j].candyType === candyType) { return true; } if (j >= 2 && board[i][j - 1] && board[i][j - 2] && board[i][j - 1].candyType === candyType && board[i][j - 2].candyType === candyType) { return true; } return false; } function initializeBoard() { for (var i = 0; i < boardSize; i++) { board[i] = []; for (var j = 0; j < boardSize; j++) { var candy = new Candy(i, j); candy.x = i * candySize + candySize / 2 + marginX; candy.y = j * candySize + candySize / 2 + marginY; board[i][j] = candy; game.addChild(candy); } } } function swapCandies(candy1, candy2) { if (!candy1 || !candy2 || isProcessing) { return; } var dx = Math.abs(candy1.i - candy2.i); var dy = Math.abs(candy1.j - candy2.j); if (dx === 1 && dy === 1 || dx === 0 && dy === 0) { return; // Çapraz veya aynı yere tıklamayı engelle } isProcessing = true; var tempI1 = candy1.i; var tempJ1 = candy1.j; var tempI2 = candy2.i; var tempJ2 = candy2.j; // Tahtada yerlerini değiştir board[tempI1][tempJ1] = candy2; board[tempI2][tempJ2] = candy1; // Görsel olarak yerlerini değiştir candy1.moveTo(tempI2, tempJ2); candy2.moveTo(tempI1, tempJ1); LK.setTimeout(function () { // Yer değiştiren her iki şekeri de kontrol et var matchInfo1 = checkAndDestroyMatches(candy1); var matchInfo2 = checkAndDestroyMatches(candy2); // Eşleşme yoksa geri döndür if (!matchInfo1.hasMatch && !matchInfo2.hasMatch) { board[tempI1][tempJ1] = candy1; board[tempI2][tempJ2] = candy2; candy1.moveTo(tempI1, tempJ1); candy2.moveTo(tempI2, tempJ2); candy1.i = tempI1; candy1.j = tempJ1; candy2.i = tempI2; candy2.j = tempJ2; LK.setTimeout(function () { isProcessing = false; }, 500); } else { if (matchInfo1.hasMatch) { // candy1 eşleşme sağladı if (matchInfo1.isFourMatch) { candy1.transformToWhite(); } else { board[candy1.i][candy1.j] = null; candy1.destroyCandy(); } } if (matchInfo2.hasMatch) { // candy2 eşleşme sağladı if (matchInfo2.isFourMatch) { candy2.transformToWhite(); } else { board[candy2.i][candy2.j] = null; candy2.destroyCandy(); } } LK.setTimeout(function () { processMatches(); }, 300); } }, 600); } function checkAndDestroyMatches(candy) { var hasMatch = false; var isFourMatch = false; var candiesToDestroy = []; var i = candy.i; var j = candy.j; var matchedCandies = [candy]; // Yatay kontrol var left = i - 1; while (left >= 0 && board[left][j] && board[left][j].candyType === candy.candyType) { matchedCandies.push(board[left][j]); left--; } var right = i + 1; while (right < boardSize && board[right][j] && board[right][j].candyType === candy.candyType) { matchedCandies.push(board[right][j]); right++; } if (matchedCandies.length >= 3) { hasMatch = true; if (matchedCandies.length === 4) { isFourMatch = true; } candiesToDestroy = candiesToDestroy.concat(matchedCandies.filter(function (c) { return c !== candy; })); } // Dikey kontrol matchedCandies = [candy]; var up = j - 1; while (up >= 0 && board[i][up] && board[i][up].candyType === candy.candyType) { matchedCandies.push(board[i][up]); up--; } var down = j + 1; while (down < boardSize && board[i][down] && board[i][down].candyType === candy.candyType) { matchedCandies.push(board[i][down]); down++; } if (matchedCandies.length >= 3) { hasMatch = true; if (matchedCandies.length === 4) { isFourMatch = true; } candiesToDestroy = candiesToDestroy.concat(matchedCandies.filter(function (c) { return c !== candy; })); } // Eşleşen şekerleri yok et candiesToDestroy.forEach(function (c) { board[c.i][c.j] = null; c.destroyCandy(); }); if (hasMatch) { if (isFourMatch) { updateScore(100); // 4-match score } else { updateScore(50); // 3-match score } scoreMultiplier++; } else { scoreMultiplier = 1; // Reset multiplier if no match } return { hasMatch: hasMatch, isFourMatch: isFourMatch }; } function processMatches() { refillBoard(); LK.setTimeout(function () { if (checkAllMatches()) { LK.setTimeout(function () { processMatches(); }, 300); } else { isProcessing = false; scoreMultiplier = 1; // Reset multiplier after processing } }, 600); } function checkAllMatches() { var foundMatch = false; for (var i = 0; i < boardSize; i++) { for (var j = 0; j < boardSize; j++) { if (board[i][j]) { var candy = board[i][j]; var candiesToDestroy = []; var matchedCandies = [candy]; // Yatay kontrol var left = i - 1; while (left >= 0 && board[left][j] && board[left][j].candyType === candy.candyType) { matchedCandies.push(board[left][j]); left--; } var right = i + 1; while (right < boardSize && board[right][j] && board[right][j].candyType === candy.candyType) { matchedCandies.push(board[right][j]); right++; } if (matchedCandies.length >= 3) { foundMatch = true; candiesToDestroy = candiesToDestroy.concat(matchedCandies); if (candiesToDestroy.length === 4) { updateScore(100); // 4-match score } else if (candiesToDestroy.length >= 3) { updateScore(50); // 3-match score } } else { // Dikey kontrol matchedCandies = [candy]; var up = j - 1; while (up >= 0 && board[i][up] && board[i][up].candyType === candy.candyType) { matchedCandies.push(board[i][up]); up--; } var down = j + 1; while (down < boardSize && board[i][down] && board[i][down].candyType === candy.candyType) { matchedCandies.push(board[i][down]); down++; } if (matchedCandies.length >= 3) { foundMatch = true; candiesToDestroy = candiesToDestroy.concat(matchedCandies); } } // Eşleşen şekerleri yok et if (candiesToDestroy.length >= 3) { if (candiesToDestroy.length === 4) { updateScore(100); // 4-match score } else if (candiesToDestroy.length >= 3) { updateScore(50); // 3-match score } candiesToDestroy.forEach(function (c) { if (board[c.i][c.j]) { board[c.i][c.j] = null; c.destroyCandy(); } }); } } } } return foundMatch; } function refillBoard() { for (var i = 0; i < boardSize; i++) { var emptySpaces = 0; for (var j = boardSize - 1; j >= 0; j--) { if (!board[i][j]) { emptySpaces++; } else if (emptySpaces > 0) { board[i][j + emptySpaces] = board[i][j]; board[i][j + emptySpaces].moveTo(i, j + emptySpaces); board[i][j] = null; } } for (var j = emptySpaces - 1; j >= 0; j--) { var newCandy = new Candy(i, j); newCandy.x = i * candySize + candySize / 2 + marginX; newCandy.y = -candySize * (emptySpaces - j) + candySize / 2 + marginY; board[i][j] = newCandy; game.addChild(newCandy); newCandy.moveTo(i, j); } } } game.down = function (x, y) { if (isProcessing) { return; } var i = Math.floor((x - marginX) / candySize); var j = Math.floor((y - marginY) / candySize); if (i >= 0 && i < boardSize && j >= 0 && j < boardSize) { if (selectedCandy) { var dx = Math.abs(selectedCandy.i - i); var dy = Math.abs(selectedCandy.j - j); if (dx === 1 && dy === 0 || dx === 0 && dy === 1) { swapCandies(selectedCandy, board[i][j]); } selectedCandy = null; } else { selectedCandy = board[i][j]; } } }; initializeBoard(); var resetButton = LK.getAsset('resetButton', { anchorX: 1, anchorY: 1, x: 2048 - 50, // Positioning the button at the bottom-right corner y: 2732 - 50 }); LK.gui.bottomRight.addChild(resetButton); resetButton.down = function () { LK.showGameOver(); // This will reset the game state }; game.update = function () {}; ;
===================================================================
--- original.js
+++ change.js
@@ -1,235 +1,58 @@
/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
* Classes
****/
-// Board class to represent the game board
-var Board = Container.expand(function () {
+var Candy = Container.expand(function (i, j, candyType) {
var self = Container.call(this);
- self.board = [];
- self.boardWidth = 9;
- self.boardHeight = 9;
- self.candySize = (game.width - (self.boardWidth - 1) * 2) / self.boardWidth;
- self.difficulty = 'easy';
- self.selectedCandy = null;
- self.moves = 0;
- self.time = 0;
- // Initialize the board with candies
- self.initializeBoard = function (difficulty) {
- self.difficulty = difficulty;
- self.moves = difficulty === 'medium' ? 30 : 0;
- self.time = difficulty === 'hard' ? 60 : 0;
- for (var i = 0; i < self.boardWidth; i++) {
- self.board[i] = [];
- for (var j = 0; j < self.boardHeight; j++) {
- self.createCandyAt(i, j);
- }
- }
- // Initial check for matches to prevent starting with matches
- while (self.hasMatches()) {
- self.reshuffleBoard();
- }
+ var candyTypes = ["candy1", "candy2", "candy3", "candy4", "candy5", "candy6"];
+ if (!candyType) {
+ do {
+ candyType = candyTypes[Math.floor(Math.random() * candyTypes.length)];
+ } while (isPotentialMatch(i, j, candyType));
+ }
+ self.candyType = candyType;
+ var candyGraphics = self.attachAsset(candyType, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.i = i;
+ self.j = j;
+ self.isMoving = false;
+ self.destroyCandy = function () {
+ self.destroy();
};
- // Create a candy at specific position
- self.createCandyAt = function (i, j) {
- var item;
- if (Math.random() < 0.1) {
- item = new PowerUp();
- } else {
- item = new Candy();
- if (self.difficulty === 'medium' && Math.random() < 0.1) {
- item.locked = true;
- } else if (self.difficulty === 'hard' && Math.random() < 0.2) {
- item.locked = true;
- item.immovable = true;
- }
- }
- item.x = i * (self.candySize + 2) + self.candySize / 2;
- item.y = j * (self.candySize + 2) + self.candySize / 2;
- item.boardX = i;
- item.boardY = j;
- self.board[i][j] = item;
- self.addChild(item);
- return item;
+ self.transformToWhite = function () {
+ self.removeChild(candyGraphics);
+ self.candyType = self.candyType + "_white";
+ candyGraphics = self.attachAsset(self.candyType, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
};
- // Check for matches on the board
- self.checkMatches = function () {
- var matches = [];
- // Check horizontal matches
- for (var j = 0; j < self.boardHeight; j++) {
- var count = 1;
- var type = null;
- for (var i = 0; i < self.boardWidth; i++) {
- var candy = self.board[i][j];
- if (candy && candy.type) {
- if (type === candy.type) {
- count++;
- if (count >= 3) {
- matches.push({
- x: i - 2,
- y: j,
- length: count,
- direction: 'horizontal'
- });
- }
- } else {
- count = 1;
- type = candy.type;
- }
+ self.moveTo = function (newI, newJ, _onComplete) {
+ self.isMoving = true;
+ self.i = newI;
+ self.j = newJ;
+ tween(self, {
+ x: newI * candySize + candySize / 2 + marginX,
+ y: newJ * candySize + candySize / 2 + marginY
+ }, {
+ duration: 300,
+ onComplete: function onComplete() {
+ self.isMoving = false;
+ if (_onComplete) {
+ _onComplete();
}
}
- }
- // Check vertical matches
- for (var i = 0; i < self.boardWidth; i++) {
- var count = 1;
- var type = null;
- for (var j = 0; j < self.boardHeight; j++) {
- var candy = self.board[i][j];
- if (candy && candy.type) {
- if (type === candy.type) {
- count++;
- if (count >= 3) {
- matches.push({
- x: i,
- y: j - 2,
- length: count,
- direction: 'vertical'
- });
- }
- } else {
- count = 1;
- type = candy.type;
- }
- }
- }
- }
- return matches;
+ });
};
- // Check if there are any matches on the board
- self.hasMatches = function () {
- return self.checkMatches().length > 0;
- };
- // Reshuffle the board when no moves are possible
- self.reshuffleBoard = function () {
- for (var i = 0; i < self.boardWidth; i++) {
- for (var j = 0; j < self.boardHeight; j++) {
- if (self.board[i][j]) {
- self.removeChild(self.board[i][j]);
- }
- }
- }
- for (var i = 0; i < self.boardWidth; i++) {
- for (var j = 0; j < self.boardHeight; j++) {
- self.createCandyAt(i, j);
- }
- }
- };
- // Get the candy at a specific position
- self.getCandyAtPosition = function (x, y) {
- var i = Math.floor((x - self.x) / (self.candySize + 2));
- var j = Math.floor((y - self.y) / (self.candySize + 2));
- if (i >= 0 && i < self.boardWidth && j >= 0 && j < self.boardHeight) {
- return self.board[i][j];
- }
- return null;
- };
- // Swap two candies on the board
- self.swapCandies = function (candy1, candy2) {
- // Check if either candy is locked or immovable
- if (candy1.locked || candy2.locked || candy1.immovable || candy2.immovable) {
- return false;
- }
- // Check if candies are adjacent
- var dx = Math.abs(candy1.boardX - candy2.boardX);
- var dy = Math.abs(candy1.boardY - candy2.boardY);
- if (dx + dy !== 1) {
- return false;
- }
- // Perform the swap
- var tempX = candy1.x;
- var tempY = candy1.y;
- var tempBX = candy1.boardX;
- var tempBY = candy1.boardY;
- candy1.x = candy2.x;
- candy1.y = candy2.y;
- candy1.boardX = candy2.boardX;
- candy1.boardY = candy2.boardY;
- candy2.x = tempX;
- candy2.y = tempY;
- candy2.boardX = tempBX;
- candy2.boardY = tempBY;
- self.board[candy1.boardX][candy1.boardY] = candy1;
- self.board[candy2.boardX][candy2.boardY] = candy2;
- // Check if the swap created any matches
- if (!self.hasMatches()) {
- // Swap back if no matches were created
- // Perform the swap back manually to avoid recursion
- tempX = candy1.x;
- tempY = candy1.y;
- tempBX = candy1.boardX;
- tempBY = candy1.boardY;
- candy1.x = candy2.x;
- candy1.y = candy2.y;
- candy1.boardX = candy2.boardX;
- candy1.boardY = candy2.boardY;
- candy2.x = tempX;
- candy2.y = tempY;
- candy2.boardX = tempBX;
- candy2.boardY = tempBY;
- self.board[candy1.boardX][candy1.boardY] = candy1;
- self.board[candy2.boardX][candy2.boardY] = candy2;
- return false;
- }
- return true;
- };
return self;
});
-// Candy class to represent each candy on the board
-var Candy = Container.expand(function () {
- var self = Container.call(this);
- var candyTypes = ['candy1', 'candy2', 'candy3', 'candy4', 'candy5'];
- self.type = candyTypes[Math.floor(Math.random() * candyTypes.length)];
- self.attachAsset(self.type, {
- anchorX: 0.5,
- anchorY: 0.5,
- width: gameBoard.candySize * 0.8,
- height: gameBoard.candySize * 0.8
- });
- self.locked = false;
- self.immovable = false;
- self.pop = function () {
- self.destroy();
- };
- return self;
-});
-// PowerUp class to represent the power-ups in the game
-var PowerUp = Container.expand(function () {
- var self = Container.call(this);
- var powerUpTypes = ['bomb', 'colorChanger', 'lightning', 'megaBomb'];
- self.type = powerUpTypes[Math.floor(Math.random() * powerUpTypes.length)];
- self.attachAsset(self.type, {
- anchorX: 0.5,
- anchorY: 0.5,
- width: gameBoard.candySize,
- height: gameBoard.candySize
- });
- self.use = function () {
- switch (self.type) {
- case 'bomb':
- // Destroy candies in a 3x3 area
- break;
- case 'colorChanger':
- // Change all candies of one color to another
- break;
- case 'lightning':
- // Destroy all candies in a row or column
- break;
- case 'megaBomb':
- // Destroy candies in a 5x5 area
- break;
- }
- };
- return self;
-});
/****
* Initialize Game
****/
@@ -239,57 +62,307 @@
/****
* Game Code
****/
-var gameBoard = new Board();
-game.addChild(gameBoard);
-gameBoard.initializeBoard('easy');
-// Center the game board
-gameBoard.y = (game.height - gameBoard.candySize * gameBoard.boardHeight - 2 * (gameBoard.boardHeight - 1)) / 2;
-gameBoard.x = (game.width - gameBoard.candySize * gameBoard.boardWidth - 2 * (gameBoard.boardWidth - 1)) / 2;
-// Handle input events
-game.down = function (x, y, obj) {
- gameBoard.selectedCandy = gameBoard.getCandyAtPosition(x, y);
- if (gameBoard.selectedCandy instanceof PowerUp) {
- gameBoard.selectedCandy.use();
- gameBoard.selectedCandy = null;
+var score = 0;
+var scoreMultiplier = 1;
+var scoreTxt = new Text2('Score: 0', {
+ size: 100,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(1, 0); // Anchor to the top-right corner
+LK.gui.topRight.addChild(scoreTxt);
+function updateScore(points) {
+ score += points * scoreMultiplier;
+ scoreTxt.setText('Score: ' + score);
+}
+var board = [];
+var boardSize = 9;
+var candySize = Math.min(2048, 2732) / (boardSize + 0.5);
+var selectedCandy = null;
+var marginY = (2732 - boardSize * candySize) / 2;
+var marginX = (2048 - boardSize * candySize) / 2;
+var isProcessing = false;
+function isPotentialMatch(i, j, candyType) {
+ // Oyun başlangıcında 3'lü eşleşmeleri engelliyoruz
+ if (i >= 2 && board[i - 1][j] && board[i - 2][j] && board[i - 1][j].candyType === candyType && board[i - 2][j].candyType === candyType) {
+ return true;
}
-};
-game.up = function (x, y, obj) {
- var candyAtMouse = gameBoard.getCandyAtPosition(x, y);
- if (gameBoard.selectedCandy && candyAtMouse && gameBoard.selectedCandy !== candyAtMouse) {
- gameBoard.swapCandies(gameBoard.selectedCandy, candyAtMouse);
+ if (j >= 2 && board[i][j - 1] && board[i][j - 2] && board[i][j - 1].candyType === candyType && board[i][j - 2].candyType === candyType) {
+ return true;
}
- gameBoard.selectedCandy = null;
-};
-game.move = function (x, y, obj) {
- if (gameBoard.selectedCandy) {
- var candyAtMouse = gameBoard.getCandyAtPosition(x, y);
- if (candyAtMouse && gameBoard.selectedCandy !== candyAtMouse) {
- if (gameBoard.swapCandies(gameBoard.selectedCandy, candyAtMouse)) {
- gameBoard.selectedCandy = null;
+ return false;
+}
+function initializeBoard() {
+ for (var i = 0; i < boardSize; i++) {
+ board[i] = [];
+ for (var j = 0; j < boardSize; j++) {
+ var candy = new Candy(i, j);
+ candy.x = i * candySize + candySize / 2 + marginX;
+ candy.y = j * candySize + candySize / 2 + marginY;
+ board[i][j] = candy;
+ game.addChild(candy);
+ }
+ }
+}
+function swapCandies(candy1, candy2) {
+ if (!candy1 || !candy2 || isProcessing) {
+ return;
+ }
+ var dx = Math.abs(candy1.i - candy2.i);
+ var dy = Math.abs(candy1.j - candy2.j);
+ if (dx === 1 && dy === 1 || dx === 0 && dy === 0) {
+ return; // Çapraz veya aynı yere tıklamayı engelle
+ }
+ isProcessing = true;
+ var tempI1 = candy1.i;
+ var tempJ1 = candy1.j;
+ var tempI2 = candy2.i;
+ var tempJ2 = candy2.j;
+ // Tahtada yerlerini değiştir
+ board[tempI1][tempJ1] = candy2;
+ board[tempI2][tempJ2] = candy1;
+ // Görsel olarak yerlerini değiştir
+ candy1.moveTo(tempI2, tempJ2);
+ candy2.moveTo(tempI1, tempJ1);
+ LK.setTimeout(function () {
+ // Yer değiştiren her iki şekeri de kontrol et
+ var matchInfo1 = checkAndDestroyMatches(candy1);
+ var matchInfo2 = checkAndDestroyMatches(candy2);
+ // Eşleşme yoksa geri döndür
+ if (!matchInfo1.hasMatch && !matchInfo2.hasMatch) {
+ board[tempI1][tempJ1] = candy1;
+ board[tempI2][tempJ2] = candy2;
+ candy1.moveTo(tempI1, tempJ1);
+ candy2.moveTo(tempI2, tempJ2);
+ candy1.i = tempI1;
+ candy1.j = tempJ1;
+ candy2.i = tempI2;
+ candy2.j = tempJ2;
+ LK.setTimeout(function () {
+ isProcessing = false;
+ }, 500);
+ } else {
+ if (matchInfo1.hasMatch) {
+ // candy1 eşleşme sağladı
+ if (matchInfo1.isFourMatch) {
+ candy1.transformToWhite();
+ } else {
+ board[candy1.i][candy1.j] = null;
+ candy1.destroyCandy();
+ }
}
+ if (matchInfo2.hasMatch) {
+ // candy2 eşleşme sağladı
+ if (matchInfo2.isFourMatch) {
+ candy2.transformToWhite();
+ } else {
+ board[candy2.i][candy2.j] = null;
+ candy2.destroyCandy();
+ }
+ }
+ LK.setTimeout(function () {
+ processMatches();
+ }, 300);
}
+ }, 600);
+}
+function checkAndDestroyMatches(candy) {
+ var hasMatch = false;
+ var isFourMatch = false;
+ var candiesToDestroy = [];
+ var i = candy.i;
+ var j = candy.j;
+ var matchedCandies = [candy];
+ // Yatay kontrol
+ var left = i - 1;
+ while (left >= 0 && board[left][j] && board[left][j].candyType === candy.candyType) {
+ matchedCandies.push(board[left][j]);
+ left--;
}
-};
-// Update function called every game tick
-game.update = function () {
- var matches = gameBoard.checkMatches();
- if (matches.length > 0) {
- matches.forEach(function (match) {
- for (var i = 0; i < match.length; i++) {
- var x = match.direction === 'horizontal' ? match.x + i : match.x;
- var y = match.direction === 'vertical' ? match.y + i : match.y;
- if (gameBoard.board[x][y]) {
- gameBoard.board[x][y].pop();
- gameBoard.board[x][y] = null;
+ var right = i + 1;
+ while (right < boardSize && board[right][j] && board[right][j].candyType === candy.candyType) {
+ matchedCandies.push(board[right][j]);
+ right++;
+ }
+ if (matchedCandies.length >= 3) {
+ hasMatch = true;
+ if (matchedCandies.length === 4) {
+ isFourMatch = true;
+ }
+ candiesToDestroy = candiesToDestroy.concat(matchedCandies.filter(function (c) {
+ return c !== candy;
+ }));
+ }
+ // Dikey kontrol
+ matchedCandies = [candy];
+ var up = j - 1;
+ while (up >= 0 && board[i][up] && board[i][up].candyType === candy.candyType) {
+ matchedCandies.push(board[i][up]);
+ up--;
+ }
+ var down = j + 1;
+ while (down < boardSize && board[i][down] && board[i][down].candyType === candy.candyType) {
+ matchedCandies.push(board[i][down]);
+ down++;
+ }
+ if (matchedCandies.length >= 3) {
+ hasMatch = true;
+ if (matchedCandies.length === 4) {
+ isFourMatch = true;
+ }
+ candiesToDestroy = candiesToDestroy.concat(matchedCandies.filter(function (c) {
+ return c !== candy;
+ }));
+ }
+ // Eşleşen şekerleri yok et
+ candiesToDestroy.forEach(function (c) {
+ board[c.i][c.j] = null;
+ c.destroyCandy();
+ });
+ if (hasMatch) {
+ if (isFourMatch) {
+ updateScore(100); // 4-match score
+ } else {
+ updateScore(50); // 3-match score
+ }
+ scoreMultiplier++;
+ } else {
+ scoreMultiplier = 1; // Reset multiplier if no match
+ }
+ return {
+ hasMatch: hasMatch,
+ isFourMatch: isFourMatch
+ };
+}
+function processMatches() {
+ refillBoard();
+ LK.setTimeout(function () {
+ if (checkAllMatches()) {
+ LK.setTimeout(function () {
+ processMatches();
+ }, 300);
+ } else {
+ isProcessing = false;
+ scoreMultiplier = 1; // Reset multiplier after processing
+ }
+ }, 600);
+}
+function checkAllMatches() {
+ var foundMatch = false;
+ for (var i = 0; i < boardSize; i++) {
+ for (var j = 0; j < boardSize; j++) {
+ if (board[i][j]) {
+ var candy = board[i][j];
+ var candiesToDestroy = [];
+ var matchedCandies = [candy];
+ // Yatay kontrol
+ var left = i - 1;
+ while (left >= 0 && board[left][j] && board[left][j].candyType === candy.candyType) {
+ matchedCandies.push(board[left][j]);
+ left--;
}
+ var right = i + 1;
+ while (right < boardSize && board[right][j] && board[right][j].candyType === candy.candyType) {
+ matchedCandies.push(board[right][j]);
+ right++;
+ }
+ if (matchedCandies.length >= 3) {
+ foundMatch = true;
+ candiesToDestroy = candiesToDestroy.concat(matchedCandies);
+ if (candiesToDestroy.length === 4) {
+ updateScore(100); // 4-match score
+ } else if (candiesToDestroy.length >= 3) {
+ updateScore(50); // 3-match score
+ }
+ } else {
+ // Dikey kontrol
+ matchedCandies = [candy];
+ var up = j - 1;
+ while (up >= 0 && board[i][up] && board[i][up].candyType === candy.candyType) {
+ matchedCandies.push(board[i][up]);
+ up--;
+ }
+ var down = j + 1;
+ while (down < boardSize && board[i][down] && board[i][down].candyType === candy.candyType) {
+ matchedCandies.push(board[i][down]);
+ down++;
+ }
+ if (matchedCandies.length >= 3) {
+ foundMatch = true;
+ candiesToDestroy = candiesToDestroy.concat(matchedCandies);
+ }
+ }
+ // Eşleşen şekerleri yok et
+ if (candiesToDestroy.length >= 3) {
+ if (candiesToDestroy.length === 4) {
+ updateScore(100); // 4-match score
+ } else if (candiesToDestroy.length >= 3) {
+ updateScore(50); // 3-match score
+ }
+ candiesToDestroy.forEach(function (c) {
+ if (board[c.i][c.j]) {
+ board[c.i][c.j] = null;
+ c.destroyCandy();
+ }
+ });
+ }
}
- });
+ }
}
- // Update game state based on difficulty
- if (gameBoard.difficulty === 'medium') {
- gameBoard.moves = Math.max(0, gameBoard.moves - 0.016); // Assuming 60 FPS
- } else if (gameBoard.difficulty === 'hard') {
- gameBoard.time = Math.max(0, gameBoard.time - 0.016);
+ return foundMatch;
+}
+function refillBoard() {
+ for (var i = 0; i < boardSize; i++) {
+ var emptySpaces = 0;
+ for (var j = boardSize - 1; j >= 0; j--) {
+ if (!board[i][j]) {
+ emptySpaces++;
+ } else if (emptySpaces > 0) {
+ board[i][j + emptySpaces] = board[i][j];
+ board[i][j + emptySpaces].moveTo(i, j + emptySpaces);
+ board[i][j] = null;
+ }
+ }
+ for (var j = emptySpaces - 1; j >= 0; j--) {
+ var newCandy = new Candy(i, j);
+ newCandy.x = i * candySize + candySize / 2 + marginX;
+ newCandy.y = -candySize * (emptySpaces - j) + candySize / 2 + marginY;
+ board[i][j] = newCandy;
+ game.addChild(newCandy);
+ newCandy.moveTo(i, j);
+ }
}
-};
\ No newline at end of file
+}
+game.down = function (x, y) {
+ if (isProcessing) {
+ return;
+ }
+ var i = Math.floor((x - marginX) / candySize);
+ var j = Math.floor((y - marginY) / candySize);
+ if (i >= 0 && i < boardSize && j >= 0 && j < boardSize) {
+ if (selectedCandy) {
+ var dx = Math.abs(selectedCandy.i - i);
+ var dy = Math.abs(selectedCandy.j - j);
+ if (dx === 1 && dy === 0 || dx === 0 && dy === 1) {
+ swapCandies(selectedCandy, board[i][j]);
+ }
+ selectedCandy = null;
+ } else {
+ selectedCandy = board[i][j];
+ }
+ }
+};
+initializeBoard();
+var resetButton = LK.getAsset('resetButton', {
+ anchorX: 1,
+ anchorY: 1,
+ x: 2048 - 50,
+ // Positioning the button at the bottom-right corner
+ y: 2732 - 50
+});
+LK.gui.bottomRight.addChild(resetButton);
+resetButton.down = function () {
+ LK.showGameOver(); // This will reset the game state
+};
+game.update = function () {};
+;
\ No newline at end of file