/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Cell = Container.expand(function (row, col) {
var self = Container.call(this);
self.row = row;
self.col = col;
self.state = 'empty'; // 'empty', 'X', 'O'
// Cell background
var background = self.attachAsset('cellBackground', {
anchorX: 0.5,
anchorY: 0.5
});
self.mark = null;
self.placeMark = function (markType) {
if (self.state !== 'empty') return false;
self.state = markType;
if (markType === 'X') {
self.mark = self.attachAsset('xMark', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0,
scaleY: 0
});
tween(self.mark, {
scaleX: 1,
scaleY: 1
}, {
duration: 200,
easing: tween.easeOut
});
LK.getSound('placeX').play();
} else if (markType === 'O') {
self.mark = self.attachAsset('oMark', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0,
scaleY: 0
});
tween(self.mark, {
scaleX: 1,
scaleY: 1
}, {
duration: 200,
easing: tween.easeOut
});
LK.getSound('placeO').play();
}
return true;
};
self.highlight = function () {
tween(background, {
tint: 0xf39c12
}, {
duration: 300,
easing: tween.easeInOut
});
};
self.down = function (x, y, obj) {
if (gameState === 'playing') {
handleCellClick(self);
}
};
return self;
});
var GameBoard = Container.expand(function () {
var self = Container.call(this);
// Grid background
var gridBg = self.attachAsset('gridBackground', {
anchorX: 0.5,
anchorY: 0.5
});
// Grid lines - vertical
var vLine1 = self.attachAsset('gridLine', {
anchorX: 0.5,
anchorY: 0.5,
x: -100,
rotation: Math.PI / 2
});
var vLine2 = self.attachAsset('gridLine', {
anchorX: 0.5,
anchorY: 0.5,
x: 100,
rotation: Math.PI / 2
});
// Grid lines - horizontal
var hLine1 = self.attachAsset('gridLine', {
anchorX: 0.5,
anchorY: 0.5,
y: -100
});
var hLine2 = self.attachAsset('gridLine', {
anchorX: 0.5,
anchorY: 0.5,
y: 100
});
self.cells = [];
// Create 3x3 grid of cells
for (var row = 0; row < 3; row++) {
self.cells[row] = [];
for (var col = 0; col < 3; col++) {
var cell = new Cell(row, col);
cell.x = (col - 1) * 200;
cell.y = (row - 1) * 200;
self.cells[row][col] = cell;
self.addChild(cell);
}
}
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a252f
});
/****
* Game Code
****/
var gameBoard;
var currentPlayer = 'X';
var gameState = 'playing'; // 'playing', 'gameOver'
var playerXScore = 0;
var playerOScore = 0;
var drawScore = 0;
// UI Elements
var currentPlayerText;
var scoreText;
var resultText;
// Initialize game board
gameBoard = new GameBoard();
gameBoard.x = 2048 / 2;
gameBoard.y = 2732 / 2 - 100;
game.addChild(gameBoard);
// Current player indicator
currentPlayerText = new Text2('Player X\'s Turn', {
size: 80,
fill: 0xFFFFFF
});
currentPlayerText.anchor.set(0.5, 0.5);
currentPlayerText.x = 2048 / 2;
currentPlayerText.y = 400;
game.addChild(currentPlayerText);
// Score display
scoreText = new Text2('X: 0 | O: 0 | Draws: 0', {
size: 60,
fill: 0xBDC3C7
});
scoreText.anchor.set(0.5, 0.5);
scoreText.x = 2048 / 2;
scoreText.y = 300;
game.addChild(scoreText);
// Result text (initially hidden)
resultText = new Text2('', {
size: 100,
fill: 0xF39C12
});
resultText.anchor.set(0.5, 0.5);
resultText.x = 2048 / 2;
resultText.y = 2000;
resultText.alpha = 0;
game.addChild(resultText);
function handleCellClick(cell) {
if (cell.placeMark(currentPlayer)) {
var winner = checkWinner();
if (winner) {
handleGameEnd(winner);
} else if (isBoardFull()) {
handleGameEnd('draw');
} else {
// Switch players
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
updateCurrentPlayerText();
}
}
}
function checkWinner() {
var cells = gameBoard.cells;
// Check rows
for (var row = 0; row < 3; row++) {
if (cells[row][0].state === cells[row][1].state && cells[row][1].state === cells[row][2].state && cells[row][0].state !== 'empty') {
highlightWinningCells([cells[row][0], cells[row][1], cells[row][2]]);
return cells[row][0].state;
}
}
// Check columns
for (var col = 0; col < 3; col++) {
if (cells[0][col].state === cells[1][col].state && cells[1][col].state === cells[2][col].state && cells[0][col].state !== 'empty') {
highlightWinningCells([cells[0][col], cells[1][col], cells[2][col]]);
return cells[0][col].state;
}
}
// Check diagonals
if (cells[0][0].state === cells[1][1].state && cells[1][1].state === cells[2][2].state && cells[0][0].state !== 'empty') {
highlightWinningCells([cells[0][0], cells[1][1], cells[2][2]]);
return cells[0][0].state;
}
if (cells[0][2].state === cells[1][1].state && cells[1][1].state === cells[2][0].state && cells[0][2].state !== 'empty') {
highlightWinningCells([cells[0][2], cells[1][1], cells[2][0]]);
return cells[0][2].state;
}
return null;
}
function isBoardFull() {
var cells = gameBoard.cells;
for (var row = 0; row < 3; row++) {
for (var col = 0; col < 3; col++) {
if (cells[row][col].state === 'empty') {
return false;
}
}
}
return true;
}
function highlightWinningCells(winningCells) {
for (var i = 0; i < winningCells.length; i++) {
winningCells[i].highlight();
}
}
function handleGameEnd(result) {
gameState = 'gameOver';
if (result === 'X') {
playerXScore++;
resultText.setText('Player X Wins!');
resultText.fill = '#e74c3c';
LK.getSound('win').play();
LK.setScore(LK.getScore() + 10);
} else if (result === 'O') {
playerOScore++;
resultText.setText('Player O Wins!');
resultText.fill = '#2ecc71';
LK.getSound('win').play();
LK.setScore(LK.getScore() + 10);
} else if (result === 'draw') {
drawScore++;
resultText.setText('It\'s a Draw!');
resultText.fill = '#f39c12';
LK.getSound('draw').play();
LK.setScore(LK.getScore() + 5);
}
updateScoreText();
showResult();
// Auto restart after 3 seconds
LK.setTimeout(function () {
resetGame();
}, 3000);
}
function showResult() {
resultText.alpha = 1;
resultText.y = 1800;
tween(resultText, {
y: 1600
}, {
duration: 500,
easing: tween.easeOut
});
}
function updateCurrentPlayerText() {
if (currentPlayer === 'X') {
currentPlayerText.setText('Player X\'s Turn');
currentPlayerText.fill = '#e74c3c';
} else {
currentPlayerText.setText('Player O\'s Turn');
currentPlayerText.fill = '#2ecc71';
}
}
function updateScoreText() {
scoreText.setText('X: ' + playerXScore + ' | O: ' + playerOScore + ' | Draws: ' + drawScore);
}
function resetGame() {
gameState = 'playing';
currentPlayer = 'X';
// Clear all cells
var cells = gameBoard.cells;
for (var row = 0; row < 3; row++) {
for (var col = 0; col < 3; col++) {
var cell = cells[row][col];
cell.state = 'empty';
if (cell.mark) {
cell.removeChild(cell.mark);
cell.mark = null;
}
}
}
updateCurrentPlayerText();
// Hide result text
tween(resultText, {
alpha: 0,
y: 2000
}, {
duration: 300,
easing: tween.easeIn
});
}
// Touch anywhere to restart when game is over
game.down = function (x, y, obj) {
if (gameState === 'gameOver') {
resetGame();
}
};
game.update = function () {
// Game logic is handled through events
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,310 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Cell = Container.expand(function (row, col) {
+ var self = Container.call(this);
+ self.row = row;
+ self.col = col;
+ self.state = 'empty'; // 'empty', 'X', 'O'
+ // Cell background
+ var background = self.attachAsset('cellBackground', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.mark = null;
+ self.placeMark = function (markType) {
+ if (self.state !== 'empty') return false;
+ self.state = markType;
+ if (markType === 'X') {
+ self.mark = self.attachAsset('xMark', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 0,
+ scaleY: 0
+ });
+ tween(self.mark, {
+ scaleX: 1,
+ scaleY: 1
+ }, {
+ duration: 200,
+ easing: tween.easeOut
+ });
+ LK.getSound('placeX').play();
+ } else if (markType === 'O') {
+ self.mark = self.attachAsset('oMark', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 0,
+ scaleY: 0
+ });
+ tween(self.mark, {
+ scaleX: 1,
+ scaleY: 1
+ }, {
+ duration: 200,
+ easing: tween.easeOut
+ });
+ LK.getSound('placeO').play();
+ }
+ return true;
+ };
+ self.highlight = function () {
+ tween(background, {
+ tint: 0xf39c12
+ }, {
+ duration: 300,
+ easing: tween.easeInOut
+ });
+ };
+ self.down = function (x, y, obj) {
+ if (gameState === 'playing') {
+ handleCellClick(self);
+ }
+ };
+ return self;
+});
+var GameBoard = Container.expand(function () {
+ var self = Container.call(this);
+ // Grid background
+ var gridBg = self.attachAsset('gridBackground', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Grid lines - vertical
+ var vLine1 = self.attachAsset('gridLine', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: -100,
+ rotation: Math.PI / 2
+ });
+ var vLine2 = self.attachAsset('gridLine', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 100,
+ rotation: Math.PI / 2
+ });
+ // Grid lines - horizontal
+ var hLine1 = self.attachAsset('gridLine', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ y: -100
+ });
+ var hLine2 = self.attachAsset('gridLine', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ y: 100
+ });
+ self.cells = [];
+ // Create 3x3 grid of cells
+ for (var row = 0; row < 3; row++) {
+ self.cells[row] = [];
+ for (var col = 0; col < 3; col++) {
+ var cell = new Cell(row, col);
+ cell.x = (col - 1) * 200;
+ cell.y = (row - 1) * 200;
+ self.cells[row][col] = cell;
+ self.addChild(cell);
+ }
+ }
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x1a252f
+});
+
+/****
+* Game Code
+****/
+var gameBoard;
+var currentPlayer = 'X';
+var gameState = 'playing'; // 'playing', 'gameOver'
+var playerXScore = 0;
+var playerOScore = 0;
+var drawScore = 0;
+// UI Elements
+var currentPlayerText;
+var scoreText;
+var resultText;
+// Initialize game board
+gameBoard = new GameBoard();
+gameBoard.x = 2048 / 2;
+gameBoard.y = 2732 / 2 - 100;
+game.addChild(gameBoard);
+// Current player indicator
+currentPlayerText = new Text2('Player X\'s Turn', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+currentPlayerText.anchor.set(0.5, 0.5);
+currentPlayerText.x = 2048 / 2;
+currentPlayerText.y = 400;
+game.addChild(currentPlayerText);
+// Score display
+scoreText = new Text2('X: 0 | O: 0 | Draws: 0', {
+ size: 60,
+ fill: 0xBDC3C7
+});
+scoreText.anchor.set(0.5, 0.5);
+scoreText.x = 2048 / 2;
+scoreText.y = 300;
+game.addChild(scoreText);
+// Result text (initially hidden)
+resultText = new Text2('', {
+ size: 100,
+ fill: 0xF39C12
+});
+resultText.anchor.set(0.5, 0.5);
+resultText.x = 2048 / 2;
+resultText.y = 2000;
+resultText.alpha = 0;
+game.addChild(resultText);
+function handleCellClick(cell) {
+ if (cell.placeMark(currentPlayer)) {
+ var winner = checkWinner();
+ if (winner) {
+ handleGameEnd(winner);
+ } else if (isBoardFull()) {
+ handleGameEnd('draw');
+ } else {
+ // Switch players
+ currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
+ updateCurrentPlayerText();
+ }
+ }
+}
+function checkWinner() {
+ var cells = gameBoard.cells;
+ // Check rows
+ for (var row = 0; row < 3; row++) {
+ if (cells[row][0].state === cells[row][1].state && cells[row][1].state === cells[row][2].state && cells[row][0].state !== 'empty') {
+ highlightWinningCells([cells[row][0], cells[row][1], cells[row][2]]);
+ return cells[row][0].state;
+ }
+ }
+ // Check columns
+ for (var col = 0; col < 3; col++) {
+ if (cells[0][col].state === cells[1][col].state && cells[1][col].state === cells[2][col].state && cells[0][col].state !== 'empty') {
+ highlightWinningCells([cells[0][col], cells[1][col], cells[2][col]]);
+ return cells[0][col].state;
+ }
+ }
+ // Check diagonals
+ if (cells[0][0].state === cells[1][1].state && cells[1][1].state === cells[2][2].state && cells[0][0].state !== 'empty') {
+ highlightWinningCells([cells[0][0], cells[1][1], cells[2][2]]);
+ return cells[0][0].state;
+ }
+ if (cells[0][2].state === cells[1][1].state && cells[1][1].state === cells[2][0].state && cells[0][2].state !== 'empty') {
+ highlightWinningCells([cells[0][2], cells[1][1], cells[2][0]]);
+ return cells[0][2].state;
+ }
+ return null;
+}
+function isBoardFull() {
+ var cells = gameBoard.cells;
+ for (var row = 0; row < 3; row++) {
+ for (var col = 0; col < 3; col++) {
+ if (cells[row][col].state === 'empty') {
+ return false;
+ }
+ }
+ }
+ return true;
+}
+function highlightWinningCells(winningCells) {
+ for (var i = 0; i < winningCells.length; i++) {
+ winningCells[i].highlight();
+ }
+}
+function handleGameEnd(result) {
+ gameState = 'gameOver';
+ if (result === 'X') {
+ playerXScore++;
+ resultText.setText('Player X Wins!');
+ resultText.fill = '#e74c3c';
+ LK.getSound('win').play();
+ LK.setScore(LK.getScore() + 10);
+ } else if (result === 'O') {
+ playerOScore++;
+ resultText.setText('Player O Wins!');
+ resultText.fill = '#2ecc71';
+ LK.getSound('win').play();
+ LK.setScore(LK.getScore() + 10);
+ } else if (result === 'draw') {
+ drawScore++;
+ resultText.setText('It\'s a Draw!');
+ resultText.fill = '#f39c12';
+ LK.getSound('draw').play();
+ LK.setScore(LK.getScore() + 5);
+ }
+ updateScoreText();
+ showResult();
+ // Auto restart after 3 seconds
+ LK.setTimeout(function () {
+ resetGame();
+ }, 3000);
+}
+function showResult() {
+ resultText.alpha = 1;
+ resultText.y = 1800;
+ tween(resultText, {
+ y: 1600
+ }, {
+ duration: 500,
+ easing: tween.easeOut
+ });
+}
+function updateCurrentPlayerText() {
+ if (currentPlayer === 'X') {
+ currentPlayerText.setText('Player X\'s Turn');
+ currentPlayerText.fill = '#e74c3c';
+ } else {
+ currentPlayerText.setText('Player O\'s Turn');
+ currentPlayerText.fill = '#2ecc71';
+ }
+}
+function updateScoreText() {
+ scoreText.setText('X: ' + playerXScore + ' | O: ' + playerOScore + ' | Draws: ' + drawScore);
+}
+function resetGame() {
+ gameState = 'playing';
+ currentPlayer = 'X';
+ // Clear all cells
+ var cells = gameBoard.cells;
+ for (var row = 0; row < 3; row++) {
+ for (var col = 0; col < 3; col++) {
+ var cell = cells[row][col];
+ cell.state = 'empty';
+ if (cell.mark) {
+ cell.removeChild(cell.mark);
+ cell.mark = null;
+ }
+ }
+ }
+ updateCurrentPlayerText();
+ // Hide result text
+ tween(resultText, {
+ alpha: 0,
+ y: 2000
+ }, {
+ duration: 300,
+ easing: tween.easeIn
+ });
+}
+// Touch anywhere to restart when game is over
+game.down = function (x, y, obj) {
+ if (gameState === 'gameOver') {
+ resetGame();
+ }
+};
+game.update = function () {
+ // Game logic is handled through events
+};
\ No newline at end of file