Code edit (4 edits merged)
Please save this source code
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading 'row')' in this line: 'if (visited[card.row][card.col] || bends > 2) {' Line Number: 160
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'Uncaught TypeError: Cannot set properties of undefined (setting '1')' in this line: 'board[card.row + 1][card.col + 1] = card;' Line Number: 249
User prompt
Fix Bug: 'Uncaught TypeError: Cannot set properties of undefined (setting '1')' in this line: 'board[card.row + 1][card.col + 1] = card;' Line Number: 249
User prompt
Fix Bug: 'Uncaught TypeError: Cannot set properties of undefined (setting '1')' in this line: 'board[card.row + 1][card.col + 1] = card;' Line Number: 245
Code edit (2 edits merged)
Please save this source code
User prompt
add debuging console logs in each step of canBeLinked
Code edit (1 edits merged)
Please save this source code
User prompt
update canBeLinked to take Card objects in parameter instead of arrays
User prompt
update the cards array to be 2 dimensional [nbRows][nbCols]
Code edit (1 edits merged)
Please save this source code
User prompt
fill each card col and row
User prompt
calculate col and row properties from x and y of the card
User prompt
Add two properties row and col to Card class
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading '256')' in this line: 'if (cards[card1[0]][card1[1]] !== cards[card2[0]][card2[1]]) {' Line Number: 148
User prompt
call canBeLinked in the down event
Code edit (2 edits merged)
Please save this source code
User prompt
in the tick event update matchDebugText with last matched value
User prompt
update lastMatched with the result of the match after 2nd card selection
User prompt
add a global bool variable lastMatched
User prompt
Add another debug text at top right and display "Match:"
Code edit (1 edits merged)
Please save this source code
var Line = Container.expand(function (startX, startY, endX, endY) {
var self = Container.call(this);
var lineGraphics = self.createAsset('line', 'Line Graphics', 0, 0);
lineGraphics.width = Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2));
lineGraphics.rotation = Math.atan2(endY - startY, endX - startX);
lineGraphics.x = startX;
lineGraphics.y = startY;
self.addChild(lineGraphics);
return self;
});
var Picture = Container.expand(function (index) {
var self = Container.call(this);
self.index = index;
var pictureGraphics = self.createAsset('picture' + self.index, 'Picture Graphics', .5, .5);
return self;
});
var Card = Container.expand(function (index) {
var self = Container.call(this);
self.shakeAnimation = function (card) {
if (!card) return;
var shakeCount = 0;
var shakeTimes = 7;
var shakeStrength = 30;
var originalX = card.x;
var originalY = card.y;
function shake() {
if (shakeCount < shakeTimes) {
card.x = originalX + (Math.random() - 0.5) * shakeStrength;
card.y = originalY + (Math.random() - 0.5) * shakeStrength;
shakeCount++;
LK.setTimeout(shake, 50);
} else {
card.x = originalX;
card.y = originalY;
card.scale.x = 1;
card.scale.y = 1;
card.children[0].scale.x = 1;
card.children[0].scale.y = 1;
if (card === selectedCard1) selectedCard1 = null;
if (card === selectedCard2) selectedCard2 = null;
isAnimating = false;
}
}
shake();
};
self.id = index;
self.on('down', function (obj) {
if (!self.isMatched && !isAnimating) {
if (!selectedCard1 || selectedCard1 && selectedCard2) {
if (selectedCard1) {
selectedCard1.scale.x = 1;
selectedCard1.scale.y = 1;
if (selectedCard1 === self) {
selectedCard1 = null;
return;
}
}
selectedCard1 = self;
selectedCard2 = null;
self.scale.x = 1.2;
self.scale.y = 1.2;
} else if (selectedCard1 && !selectedCard2 && selectedCard1 === self) {
selectedCard1.scale.x = 1;
selectedCard1.scale.y = 1;
selectedCard1 = null;
} else if (selectedCard1 && !selectedCard2 && selectedCard1 !== self) {
selectedCard2 = self;
self.scale.x = 1.2;
self.scale.y = 1.2;
if (selectedCard1.id == selectedCard2.id) {
selectedCard1.match(selectedCard2);
selectedCard2.match(selectedCard1);
selectedCard1 = null;
selectedCard2 = null;
} else {
isAnimating = true;
LK.setTimeout(function () {
self.shakeAnimation(selectedCard1);
self.shakeAnimation(selectedCard2);
}, 300);
}
}
}
});
var cardGraphics = self.createAsset('card', 'Card Graphics', .5, .5);
self.isMatched = false;
self.match = function (otherCard) {
var line = new Line(self.x, self.y, otherCard.x, otherCard.y);
LK.stage.addChild(line);
LK.setTimeout(function () {
line.destroy();
}, 1000);
var tadaAnimationSteps = 10;
var tadaAnimationCount = 0;
var scaleDelta = 0.2;
var tadaDelay = 0;
var tadaDelayMax = 60;
self.tadaAnimation = function () {
if (tadaAnimationCount < tadaAnimationSteps) {
self.scale.x += tadaAnimationCount % 2 === 0 ? scaleDelta : -scaleDelta;
self.scale.y += tadaAnimationCount % 2 === 0 ? scaleDelta : -scaleDelta;
tadaAnimationCount++;
} else {
LK.off('tick', self.tadaAnimation);
}
};
self.shrinkAnimation = function () {
if (self.scale.x > 0.1) {
self.scale.x -= 0.01;
self.scale.y -= 0.01;
} else {
self.visible = false;
self.isMatched = true;
LK.off('tick', self.tadaAnimation);
LK.off('tick', self.shrinkAnimation);
}
};
LK.on('tick', self.tadaAnimation);
LK.setTimeout(function () {
LK.on('tick', self.shrinkAnimation);
}, tadaAnimationSteps * 100);
};
var picture = self.addChild(new Picture(index));
picture.x = 0;
picture.y = 0;
return self;
});
console.log("Principle : A pair matching game is a type of puzzle game where the player has to find and connect two identical images on a board. The player can only connect the images with a straight line or a line that bends at most twice. The line cannot cross any other images on the board. ");
var debug = true;
var currentCol = 0;
var currentRow = 0;
var cardPairs = [];
var cards = [];
var selectedCard1 = null;
var selectedCard2 = null;
var isAnimating = false;
var nbRows = 2;
var nbCols = 3;
var nbCards = nbRows * nbCols;
var cardWidth = 200;
var cardHeight = 200;
var horizontalOffset = 320;
var verticalOffset = 256;
function canBeLinked(cards, card1, card2) {
if (cards[card1[0]][card1[1]] !== cards[card2[0]][card2[1]]) {
return false;
}
var visited = Array.from({
length: cards.length
}, () => Array(cards[0].length).fill(false));
function dfs(i, j, bends) {
if (visited[i][j] || bends > 2) {
return false;
}
if (i === card2[0] && j === card2[1]) {
return true;
}
visited[i][j] = true;
var directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
for (let [di, dj] of directions) {
var ni = i + di, nj = j + dj;
if (ni >= 0 && ni < cards.length && nj >= 0 && nj < cards[0].length) {
if (cards[ni][nj] === 0 || ni === card2[0] && nj === card2[1]) {
if (di !== 0 && dfs(ni, nj, bends + (dj !== 0)) || dj !== 0 && dfs(ni, nj, bends + (di !== 0))) {
return true;
}
}
}
}
visited[i][j] = false;
return false;
}
return dfs(card1[0], card1[1], 0);
}
var Game = Container.expand(function () {
var self = Container.call(this);
self.initGame = function () {
cardPairs = [];
for (var i = 0; i < nbCards / 2; i++) {
cardPairs.push(i, i);
}
cardPairs = shuffleArray(cardPairs);
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
};
self.initGame();
var debugText = new Text2('', {
size: 50,
fill: '#ffffff'
});
debugText.anchor.set(0.5, 0);
LK.gui.topCenter.addChild(debugText);
self.on('move', function (obj) {
var pos = obj.event.getLocalPosition(self);
var col = 1 + Math.floor((pos.x - horizontalOffset + cardWidth / 2) / cardWidth);
var row = 1 + Math.floor((pos.y - verticalOffset + cardHeight / 2) / cardHeight);
currentCol = Math.max(1, Math.min(nbCols, col));
currentRow = Math.max(1, Math.min(nbRows, row));
if (debug) {
debugText.setText('Row: ' + currentRow + ' Col: ' + currentCol);
}
});
for (var i = 0; i < nbCards; i++) {
var card = self.addChild(new Card(cardPairs[i]));
card.x = horizontalOffset + i % nbCols * cardWidth;
card.y = verticalOffset + Math.floor(i / nbCols) * cardHeight;
cards.push(card);
}
LK.on('tick', function () {
if (cards.every(function (card) {
return card.isMatched;
})) {
LK.showGameOver();
}
});
});
a photo realistic top view of empty flat beige plastic square. Single Game Texture. In-Game asset. 2d. No background. High contrast. No shadows.
a christmas tree. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas gift. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas ball. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a golden christmas tree star. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas hat. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas leaf. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas holly leaf. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas snow flake. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas snow man. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas reindeer. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas candy cane. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas green ball . plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas gingerbrean man. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas pine cone. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas present green. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas boe tie. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas socks. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas penguin. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas decorated blue present. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas scarf. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas gloves. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas blue decorated ball. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas candle. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas snow globe. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
an christmas elongated cuboid present. plastic style. No shadow. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a round christmas gift.plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a round christmas gift.plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
one cute christmas elf. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
one cute christmas elf. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
one cute christmas reindeer head. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
one cute christmas reindeer head with a red nose. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
one christmas bell. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a couple of christmas bells. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas gifts bag. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas candle. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a cute santa clauss. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Santa's sleigh. Side view. Plastic style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Reimagine the cute teddy bear sitting without background. Plastic style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Christmas Rocking Horse. Plastic style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
One Christmas Matryoshka Doll. Plastic style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A christmas Miniature Train. Plastic style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A christmas Toy Soldier. Plastic style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A christmas Music Box . Plastic style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a cute polar bear cub. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a sled plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas gingerbread girl. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas gingerbread house. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a cute christmas jack in the box. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a cute christmas cookie. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas log cacke. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a christmas donut. plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a cute christmas owl. Plastic style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a feeric christmas landscape at night with decorated trees Background image
a feeric christmas landscape at night with snow men and snow flakes Background image
a feeric christmas landscape at night with a snow man , candy canes ,holly leafs and snow flakes, Background image
a feeric christmas landscape at night with a snow man , candy canes ,holly leafs and snow flakes, Santa's reindeers, green, red and blue presents Background image
a feeric christmas landscape at night with a snow man , candy canes ,holly leafs and snow flakes, cute penguin, Santa's reindeers, green, red and blue presents and a lot of toys, a sled, gingerbread boy and girl, snow globes, a cute polar bear cub. a gingerbread house Background image
A Magical feeric starry christmas landscape at night with a snow man , candy canes ,holly leafs and snow flakes, cute penguin, Santa's reindeers, green, red and blue presents and a lot of toys, a sled, gingerbread boy and girl, snow globes, cute polar bears cub and a gingerbread house. Realistic. Plastic style. Background image