User prompt
add a shop so you can buy skins ↪💡 Consider importing and using the following plugins: @upit/storage.v1
User prompt
When I'm winning in tic-tac-toe, it doesn't give me a point. So when you win in tic-tac-toe, it gives the player a point and makes the X disappear. Actually, no, restart the game.
User prompt
If it is 3 in a row, give the player a point. If it is any kind of line, like a diagonal line, or a line going up, give the X a point. But if it's 4-0, don't give X a point.
User prompt
If X wins, give them one win, and if O wins, don't give X a win, and make it so if O wins, you win.
User prompt
add a menu screen that you can play multiplayer or play with an AI
User prompt
Make tic-tac-toe
Code edit (1 edits merged)
Please save this source code
User prompt
Ultimate Tic-Tac-Toe Arena
Initial prompt
can you make tic-tac-toe like better so you could have like multiplayer custom modes
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var GameGrid = Container.expand(function (gridSize, cellSize) {
var self = Container.call(this);
self.gridSize = gridSize;
self.cellSize = cellSize;
self.cells = [];
self.winCondition = Math.min(gridSize, 5);
// Initialize grid array
for (var row = 0; row < gridSize; row++) {
self.cells[row] = [];
for (var col = 0; col < gridSize; col++) {
self.cells[row][col] = null;
}
}
self.createVisualGrid = function () {
var totalSize = gridSize * cellSize;
var startX = -totalSize / 2 + cellSize / 2;
var startY = -totalSize / 2 + cellSize / 2;
// Create cells
for (var row = 0; row < gridSize; row++) {
for (var col = 0; col < gridSize; col++) {
var cell = new GridCell(row, col, cellSize);
cell.x = startX + col * cellSize;
cell.y = startY + row * cellSize;
self.addChild(cell);
self.cells[row][col] = cell;
}
}
// Create grid lines
for (var i = 1; i < gridSize; i++) {
// Vertical lines
var vLine = self.attachAsset('gridLine', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1,
scaleY: totalSize / 8
});
vLine.x = startX + i * cellSize - cellSize / 2;
vLine.y = 0;
// Horizontal lines
var hLine = self.attachAsset('gridLine', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: totalSize / 600,
scaleY: 1,
rotation: Math.PI / 2
});
hLine.x = 0;
hLine.y = startY + i * cellSize - cellSize / 2;
}
};
self.checkWinner = function () {
// Check rows
for (var row = 0; row < gridSize; row++) {
for (var col = 0; col <= gridSize - self.winCondition; col++) {
var symbol = self.cells[row][col].symbol;
if (symbol) {
var count = 1;
for (var k = 1; k < self.winCondition; k++) {
if (self.cells[row][col + k].symbol === symbol) {
count++;
} else {
break;
}
}
if (count === self.winCondition) return symbol;
}
}
}
// Check columns
for (var col = 0; col < gridSize; col++) {
for (var row = 0; row <= gridSize - self.winCondition; row++) {
var symbol = self.cells[row][col].symbol;
if (symbol) {
var count = 1;
for (var k = 1; k < self.winCondition; k++) {
if (self.cells[row + k][col].symbol === symbol) {
count++;
} else {
break;
}
}
if (count === self.winCondition) return symbol;
}
}
}
// Check diagonals
for (var row = 0; row <= gridSize - self.winCondition; row++) {
for (var col = 0; col <= gridSize - self.winCondition; col++) {
var symbol = self.cells[row][col].symbol;
if (symbol) {
var count = 1;
for (var k = 1; k < self.winCondition; k++) {
if (self.cells[row + k][col + k].symbol === symbol) {
count++;
} else {
break;
}
}
if (count === self.winCondition) return symbol;
}
}
}
// Check anti-diagonals
for (var row = 0; row <= gridSize - self.winCondition; row++) {
for (var col = self.winCondition - 1; col < gridSize; col++) {
var symbol = self.cells[row][col].symbol;
if (symbol) {
var count = 1;
for (var k = 1; k < self.winCondition; k++) {
if (self.cells[row + k][col - k].symbol === symbol) {
count++;
} else {
break;
}
}
if (count === self.winCondition) return symbol;
}
}
}
return null;
};
self.isFull = function () {
for (var row = 0; row < gridSize; row++) {
for (var col = 0; col < gridSize; col++) {
if (self.cells[row][col].symbol === null) {
return false;
}
}
}
return true;
};
self.getEmptyCells = function () {
var emptyCells = [];
for (var row = 0; row < gridSize; row++) {
for (var col = 0; col < gridSize; col++) {
if (self.cells[row][col].symbol === null) {
emptyCells.push({
row: row,
col: col
});
}
}
}
return emptyCells;
};
self.createVisualGrid();
return self;
});
var GridCell = Container.expand(function (row, col, size) {
var self = Container.call(this);
self.row = row;
self.col = col;
self.cellSize = size;
self.symbol = null; // 'X', 'O', or null
var background = self.attachAsset('cellBackground', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: self.cellSize / 180,
scaleY: self.cellSize / 180
});
var highlight = self.attachAsset('cellHighlight', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: self.cellSize / 180,
scaleY: self.cellSize / 180,
alpha: 0
});
self.symbolGraphic = null;
self.setSymbol = function (symbol) {
if (self.symbol !== null) return false;
self.symbol = symbol;
if (symbol === 'X') {
self.symbolGraphic = self.attachAsset('xSymbol', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: self.cellSize * 0.6 / 120,
scaleY: self.cellSize * 0.6 / 120,
rotation: Math.PI / 4
});
} else if (symbol === 'O') {
self.symbolGraphic = self.attachAsset('oSymbol', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: self.cellSize * 0.6 / 120,
scaleY: self.cellSize * 0.6 / 120
});
}
if (self.symbolGraphic) {
self.symbolGraphic.alpha = 0;
tween(self.symbolGraphic, {
alpha: 1,
scaleX: self.symbolGraphic.scaleX * 1.2,
scaleY: self.symbolGraphic.scaleY * 1.2
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self.symbolGraphic, {
scaleX: self.symbolGraphic.scaleX / 1.2,
scaleY: self.symbolGraphic.scaleY / 1.2
}, {
duration: 100,
easing: tween.easeIn
});
}
});
}
return true;
};
self.showHighlight = function () {
tween(highlight, {
alpha: 0.3
}, {
duration: 150
});
};
self.hideHighlight = function () {
tween(highlight, {
alpha: 0
}, {
duration: 150
});
};
self.down = function (x, y, obj) {
if (gameState === 'playing' && self.symbol === null && currentPlayer === 'human') {
if (self.setSymbol(playerSymbol)) {
LK.getSound('placeSymbol').play();
makeMove(self.row, self.col);
}
}
};
self.move = function (x, y, obj) {
if (gameState === 'playing' && self.symbol === null) {
self.showHighlight();
}
};
return self;
});
var MenuButton = Container.expand(function (text, width, height) {
var self = Container.call(this);
var button = self.attachAsset('menuButton', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: width / 400,
scaleY: height / 80
});
var buttonText = new Text2(text, {
size: 40,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
LK.getSound('buttonClick').play();
tween(self, {
scaleX: 0.95,
scaleY: 0.95
}, {
duration: 100,
onFinish: function onFinish() {
tween(self, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
}
});
if (self.onClick) {
self.onClick();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a252f
});
/****
* Game Code
****/
var gameState = 'menu'; // 'menu', 'playing', 'gameOver'
var currentGridSize = 3;
var currentGrid = null;
var currentPlayer = 'human'; // 'human', 'ai'
var playerSymbol = 'X';
var aiSymbol = 'O';
var gameMode = 'classic'; // 'classic', 'timed', 'powerup'
var aiDifficulty = 'medium'; // 'easy', 'medium', 'hard'
// UI Elements
var titleText = new Text2('Ultimate Tic-Tac-Toe', {
size: 80,
fill: 0x3498DB
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 400;
var instructionText = new Text2('Choose your grid size:', {
size: 50,
fill: 0xECF0F1
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 1024;
instructionText.y = 600;
var statusText = new Text2('', {
size: 60,
fill: 0xE74C3C
});
statusText.anchor.set(0.5, 0.5);
var scoreText = new Text2('Score: 0', {
size: 50,
fill: 0xF39C12
});
scoreText.anchor.set(0.5, 0);
// Menu buttons
var button3x3 = new MenuButton('3x3 Classic', 300, 80);
button3x3.x = 1024;
button3x3.y = 800;
button3x3.onClick = function () {
startGame(3);
};
var button5x5 = new MenuButton('5x5 Strategic', 300, 80);
button5x5.x = 1024;
button5x5.y = 920;
button5x5.onClick = function () {
startGame(5);
};
var button7x7 = new MenuButton('7x7 Epic', 300, 80);
button7x7.x = 1024;
button7x7.y = 1040;
button7x7.onClick = function () {
startGame(7);
};
var backButton = new MenuButton('Back to Menu', 250, 60);
backButton.x = 1024;
backButton.y = 2500;
backButton.onClick = function () {
showMenu();
};
// Add score to GUI
LK.gui.top.addChild(scoreText);
// Game functions
function showMenu() {
gameState = 'menu';
// Clear game elements
if (currentGrid) {
currentGrid.destroy();
currentGrid = null;
}
// Show menu elements
game.addChild(titleText);
game.addChild(instructionText);
game.addChild(button3x3);
game.addChild(button5x5);
game.addChild(button7x7);
// Hide game elements
if (statusText.parent) {
statusText.parent.removeChild(statusText);
}
if (backButton.parent) {
backButton.parent.removeChild(backButton);
}
}
function startGame(gridSize) {
gameState = 'playing';
currentGridSize = gridSize;
currentPlayer = 'human';
// Hide menu elements
game.removeChild(titleText);
game.removeChild(instructionText);
game.removeChild(button3x3);
game.removeChild(button5x5);
game.removeChild(button7x7);
// Create game grid
var cellSize = Math.min(200, 1400 / gridSize);
currentGrid = new GameGrid(gridSize, cellSize);
currentGrid.x = 1024;
currentGrid.y = 1366;
game.addChild(currentGrid);
// Show game UI
statusText.setText("Your turn (X)");
statusText.x = 1024;
statusText.y = 200;
game.addChild(statusText);
game.addChild(backButton);
scoreText.setText('Score: ' + LK.getScore());
}
function makeMove(row, col) {
if (gameState !== 'playing') return;
var winner = currentGrid.checkWinner();
if (winner) {
endGame(winner);
return;
}
if (currentGrid.isFull()) {
endGame('tie');
return;
}
// Switch to AI turn
currentPlayer = 'ai';
statusText.setText("AI thinking...");
LK.setTimeout(function () {
makeAIMove();
}, 500);
}
function makeAIMove() {
if (gameState !== 'playing') return;
var emptyCells = currentGrid.getEmptyCells();
if (emptyCells.length === 0) return;
var move;
if (aiDifficulty === 'easy') {
// Random move
move = emptyCells[Math.floor(Math.random() * emptyCells.length)];
} else {
// Try to win or block player
move = getBestMove();
if (!move) {
move = emptyCells[Math.floor(Math.random() * emptyCells.length)];
}
}
if (currentGrid.cells[move.row][move.col].setSymbol(aiSymbol)) {
LK.getSound('placeSymbol').play();
var winner = currentGrid.checkWinner();
if (winner) {
endGame(winner);
return;
}
if (currentGrid.isFull()) {
endGame('tie');
return;
}
// Switch back to human
currentPlayer = 'human';
statusText.setText("Your turn (X)");
}
}
function getBestMove() {
var emptyCells = currentGrid.getEmptyCells();
// Check if AI can win
for (var i = 0; i < emptyCells.length; i++) {
var cell = emptyCells[i];
currentGrid.cells[cell.row][cell.col].symbol = aiSymbol;
if (currentGrid.checkWinner() === aiSymbol) {
currentGrid.cells[cell.row][cell.col].symbol = null;
return cell;
}
currentGrid.cells[cell.row][cell.col].symbol = null;
}
// Check if AI needs to block player
for (var i = 0; i < emptyCells.length; i++) {
var cell = emptyCells[i];
currentGrid.cells[cell.row][cell.col].symbol = playerSymbol;
if (currentGrid.checkWinner() === playerSymbol) {
currentGrid.cells[cell.row][cell.col].symbol = null;
return cell;
}
currentGrid.cells[cell.row][cell.col].symbol = null;
}
return null;
}
function endGame(winner) {
gameState = 'gameOver';
if (winner === playerSymbol) {
statusText.setText("You Win!");
statusText.tint = 0x2ecc71;
LK.getSound('gameWin').play();
// Award points based on grid size
var points = currentGridSize * 10;
LK.setScore(LK.getScore() + points);
scoreText.setText('Score: ' + LK.getScore());
// Check for win condition
if (LK.getScore() >= 100) {
LK.setTimeout(function () {
LK.showYouWin();
}, 2000);
}
} else if (winner === aiSymbol) {
statusText.setText("AI Wins!");
statusText.tint = 0xe74c3c;
} else {
statusText.setText("It's a Tie!");
statusText.tint = 0xf39c12;
// Small points for tie
LK.setScore(LK.getScore() + 5);
scoreText.setText('Score: ' + LK.getScore());
}
// Reset to menu after delay
LK.setTimeout(function () {
showMenu();
statusText.tint = 0xe74c3c;
}, 3000);
}
// Initialize storage
var playerStats = storage.stats || {
gamesWon: 0,
totalGames: 0,
bestScore: 0
};
// Update best score
if (LK.getScore() > playerStats.bestScore) {
playerStats.bestScore = LK.getScore();
storage.stats = playerStats;
}
// Start with menu
showMenu();
game.update = function () {
// Update game logic if needed
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,521 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var GameGrid = Container.expand(function (gridSize, cellSize) {
+ var self = Container.call(this);
+ self.gridSize = gridSize;
+ self.cellSize = cellSize;
+ self.cells = [];
+ self.winCondition = Math.min(gridSize, 5);
+ // Initialize grid array
+ for (var row = 0; row < gridSize; row++) {
+ self.cells[row] = [];
+ for (var col = 0; col < gridSize; col++) {
+ self.cells[row][col] = null;
+ }
+ }
+ self.createVisualGrid = function () {
+ var totalSize = gridSize * cellSize;
+ var startX = -totalSize / 2 + cellSize / 2;
+ var startY = -totalSize / 2 + cellSize / 2;
+ // Create cells
+ for (var row = 0; row < gridSize; row++) {
+ for (var col = 0; col < gridSize; col++) {
+ var cell = new GridCell(row, col, cellSize);
+ cell.x = startX + col * cellSize;
+ cell.y = startY + row * cellSize;
+ self.addChild(cell);
+ self.cells[row][col] = cell;
+ }
+ }
+ // Create grid lines
+ for (var i = 1; i < gridSize; i++) {
+ // Vertical lines
+ var vLine = self.attachAsset('gridLine', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 1,
+ scaleY: totalSize / 8
+ });
+ vLine.x = startX + i * cellSize - cellSize / 2;
+ vLine.y = 0;
+ // Horizontal lines
+ var hLine = self.attachAsset('gridLine', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: totalSize / 600,
+ scaleY: 1,
+ rotation: Math.PI / 2
+ });
+ hLine.x = 0;
+ hLine.y = startY + i * cellSize - cellSize / 2;
+ }
+ };
+ self.checkWinner = function () {
+ // Check rows
+ for (var row = 0; row < gridSize; row++) {
+ for (var col = 0; col <= gridSize - self.winCondition; col++) {
+ var symbol = self.cells[row][col].symbol;
+ if (symbol) {
+ var count = 1;
+ for (var k = 1; k < self.winCondition; k++) {
+ if (self.cells[row][col + k].symbol === symbol) {
+ count++;
+ } else {
+ break;
+ }
+ }
+ if (count === self.winCondition) return symbol;
+ }
+ }
+ }
+ // Check columns
+ for (var col = 0; col < gridSize; col++) {
+ for (var row = 0; row <= gridSize - self.winCondition; row++) {
+ var symbol = self.cells[row][col].symbol;
+ if (symbol) {
+ var count = 1;
+ for (var k = 1; k < self.winCondition; k++) {
+ if (self.cells[row + k][col].symbol === symbol) {
+ count++;
+ } else {
+ break;
+ }
+ }
+ if (count === self.winCondition) return symbol;
+ }
+ }
+ }
+ // Check diagonals
+ for (var row = 0; row <= gridSize - self.winCondition; row++) {
+ for (var col = 0; col <= gridSize - self.winCondition; col++) {
+ var symbol = self.cells[row][col].symbol;
+ if (symbol) {
+ var count = 1;
+ for (var k = 1; k < self.winCondition; k++) {
+ if (self.cells[row + k][col + k].symbol === symbol) {
+ count++;
+ } else {
+ break;
+ }
+ }
+ if (count === self.winCondition) return symbol;
+ }
+ }
+ }
+ // Check anti-diagonals
+ for (var row = 0; row <= gridSize - self.winCondition; row++) {
+ for (var col = self.winCondition - 1; col < gridSize; col++) {
+ var symbol = self.cells[row][col].symbol;
+ if (symbol) {
+ var count = 1;
+ for (var k = 1; k < self.winCondition; k++) {
+ if (self.cells[row + k][col - k].symbol === symbol) {
+ count++;
+ } else {
+ break;
+ }
+ }
+ if (count === self.winCondition) return symbol;
+ }
+ }
+ }
+ return null;
+ };
+ self.isFull = function () {
+ for (var row = 0; row < gridSize; row++) {
+ for (var col = 0; col < gridSize; col++) {
+ if (self.cells[row][col].symbol === null) {
+ return false;
+ }
+ }
+ }
+ return true;
+ };
+ self.getEmptyCells = function () {
+ var emptyCells = [];
+ for (var row = 0; row < gridSize; row++) {
+ for (var col = 0; col < gridSize; col++) {
+ if (self.cells[row][col].symbol === null) {
+ emptyCells.push({
+ row: row,
+ col: col
+ });
+ }
+ }
+ }
+ return emptyCells;
+ };
+ self.createVisualGrid();
+ return self;
+});
+var GridCell = Container.expand(function (row, col, size) {
+ var self = Container.call(this);
+ self.row = row;
+ self.col = col;
+ self.cellSize = size;
+ self.symbol = null; // 'X', 'O', or null
+ var background = self.attachAsset('cellBackground', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: self.cellSize / 180,
+ scaleY: self.cellSize / 180
+ });
+ var highlight = self.attachAsset('cellHighlight', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: self.cellSize / 180,
+ scaleY: self.cellSize / 180,
+ alpha: 0
+ });
+ self.symbolGraphic = null;
+ self.setSymbol = function (symbol) {
+ if (self.symbol !== null) return false;
+ self.symbol = symbol;
+ if (symbol === 'X') {
+ self.symbolGraphic = self.attachAsset('xSymbol', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: self.cellSize * 0.6 / 120,
+ scaleY: self.cellSize * 0.6 / 120,
+ rotation: Math.PI / 4
+ });
+ } else if (symbol === 'O') {
+ self.symbolGraphic = self.attachAsset('oSymbol', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: self.cellSize * 0.6 / 120,
+ scaleY: self.cellSize * 0.6 / 120
+ });
+ }
+ if (self.symbolGraphic) {
+ self.symbolGraphic.alpha = 0;
+ tween(self.symbolGraphic, {
+ alpha: 1,
+ scaleX: self.symbolGraphic.scaleX * 1.2,
+ scaleY: self.symbolGraphic.scaleY * 1.2
+ }, {
+ duration: 200,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ tween(self.symbolGraphic, {
+ scaleX: self.symbolGraphic.scaleX / 1.2,
+ scaleY: self.symbolGraphic.scaleY / 1.2
+ }, {
+ duration: 100,
+ easing: tween.easeIn
+ });
+ }
+ });
+ }
+ return true;
+ };
+ self.showHighlight = function () {
+ tween(highlight, {
+ alpha: 0.3
+ }, {
+ duration: 150
+ });
+ };
+ self.hideHighlight = function () {
+ tween(highlight, {
+ alpha: 0
+ }, {
+ duration: 150
+ });
+ };
+ self.down = function (x, y, obj) {
+ if (gameState === 'playing' && self.symbol === null && currentPlayer === 'human') {
+ if (self.setSymbol(playerSymbol)) {
+ LK.getSound('placeSymbol').play();
+ makeMove(self.row, self.col);
+ }
+ }
+ };
+ self.move = function (x, y, obj) {
+ if (gameState === 'playing' && self.symbol === null) {
+ self.showHighlight();
+ }
+ };
+ return self;
+});
+var MenuButton = Container.expand(function (text, width, height) {
+ var self = Container.call(this);
+ var button = self.attachAsset('menuButton', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: width / 400,
+ scaleY: height / 80
+ });
+ var buttonText = new Text2(text, {
+ size: 40,
+ fill: 0xFFFFFF
+ });
+ buttonText.anchor.set(0.5, 0.5);
+ self.addChild(buttonText);
+ self.down = function (x, y, obj) {
+ LK.getSound('buttonClick').play();
+ tween(self, {
+ scaleX: 0.95,
+ scaleY: 0.95
+ }, {
+ duration: 100,
+ onFinish: function onFinish() {
+ tween(self, {
+ scaleX: 1,
+ scaleY: 1
+ }, {
+ duration: 100
+ });
+ }
+ });
+ if (self.onClick) {
+ self.onClick();
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x1a252f
+});
+
+/****
+* Game Code
+****/
+var gameState = 'menu'; // 'menu', 'playing', 'gameOver'
+var currentGridSize = 3;
+var currentGrid = null;
+var currentPlayer = 'human'; // 'human', 'ai'
+var playerSymbol = 'X';
+var aiSymbol = 'O';
+var gameMode = 'classic'; // 'classic', 'timed', 'powerup'
+var aiDifficulty = 'medium'; // 'easy', 'medium', 'hard'
+// UI Elements
+var titleText = new Text2('Ultimate Tic-Tac-Toe', {
+ size: 80,
+ fill: 0x3498DB
+});
+titleText.anchor.set(0.5, 0.5);
+titleText.x = 1024;
+titleText.y = 400;
+var instructionText = new Text2('Choose your grid size:', {
+ size: 50,
+ fill: 0xECF0F1
+});
+instructionText.anchor.set(0.5, 0.5);
+instructionText.x = 1024;
+instructionText.y = 600;
+var statusText = new Text2('', {
+ size: 60,
+ fill: 0xE74C3C
+});
+statusText.anchor.set(0.5, 0.5);
+var scoreText = new Text2('Score: 0', {
+ size: 50,
+ fill: 0xF39C12
+});
+scoreText.anchor.set(0.5, 0);
+// Menu buttons
+var button3x3 = new MenuButton('3x3 Classic', 300, 80);
+button3x3.x = 1024;
+button3x3.y = 800;
+button3x3.onClick = function () {
+ startGame(3);
+};
+var button5x5 = new MenuButton('5x5 Strategic', 300, 80);
+button5x5.x = 1024;
+button5x5.y = 920;
+button5x5.onClick = function () {
+ startGame(5);
+};
+var button7x7 = new MenuButton('7x7 Epic', 300, 80);
+button7x7.x = 1024;
+button7x7.y = 1040;
+button7x7.onClick = function () {
+ startGame(7);
+};
+var backButton = new MenuButton('Back to Menu', 250, 60);
+backButton.x = 1024;
+backButton.y = 2500;
+backButton.onClick = function () {
+ showMenu();
+};
+// Add score to GUI
+LK.gui.top.addChild(scoreText);
+// Game functions
+function showMenu() {
+ gameState = 'menu';
+ // Clear game elements
+ if (currentGrid) {
+ currentGrid.destroy();
+ currentGrid = null;
+ }
+ // Show menu elements
+ game.addChild(titleText);
+ game.addChild(instructionText);
+ game.addChild(button3x3);
+ game.addChild(button5x5);
+ game.addChild(button7x7);
+ // Hide game elements
+ if (statusText.parent) {
+ statusText.parent.removeChild(statusText);
+ }
+ if (backButton.parent) {
+ backButton.parent.removeChild(backButton);
+ }
+}
+function startGame(gridSize) {
+ gameState = 'playing';
+ currentGridSize = gridSize;
+ currentPlayer = 'human';
+ // Hide menu elements
+ game.removeChild(titleText);
+ game.removeChild(instructionText);
+ game.removeChild(button3x3);
+ game.removeChild(button5x5);
+ game.removeChild(button7x7);
+ // Create game grid
+ var cellSize = Math.min(200, 1400 / gridSize);
+ currentGrid = new GameGrid(gridSize, cellSize);
+ currentGrid.x = 1024;
+ currentGrid.y = 1366;
+ game.addChild(currentGrid);
+ // Show game UI
+ statusText.setText("Your turn (X)");
+ statusText.x = 1024;
+ statusText.y = 200;
+ game.addChild(statusText);
+ game.addChild(backButton);
+ scoreText.setText('Score: ' + LK.getScore());
+}
+function makeMove(row, col) {
+ if (gameState !== 'playing') return;
+ var winner = currentGrid.checkWinner();
+ if (winner) {
+ endGame(winner);
+ return;
+ }
+ if (currentGrid.isFull()) {
+ endGame('tie');
+ return;
+ }
+ // Switch to AI turn
+ currentPlayer = 'ai';
+ statusText.setText("AI thinking...");
+ LK.setTimeout(function () {
+ makeAIMove();
+ }, 500);
+}
+function makeAIMove() {
+ if (gameState !== 'playing') return;
+ var emptyCells = currentGrid.getEmptyCells();
+ if (emptyCells.length === 0) return;
+ var move;
+ if (aiDifficulty === 'easy') {
+ // Random move
+ move = emptyCells[Math.floor(Math.random() * emptyCells.length)];
+ } else {
+ // Try to win or block player
+ move = getBestMove();
+ if (!move) {
+ move = emptyCells[Math.floor(Math.random() * emptyCells.length)];
+ }
+ }
+ if (currentGrid.cells[move.row][move.col].setSymbol(aiSymbol)) {
+ LK.getSound('placeSymbol').play();
+ var winner = currentGrid.checkWinner();
+ if (winner) {
+ endGame(winner);
+ return;
+ }
+ if (currentGrid.isFull()) {
+ endGame('tie');
+ return;
+ }
+ // Switch back to human
+ currentPlayer = 'human';
+ statusText.setText("Your turn (X)");
+ }
+}
+function getBestMove() {
+ var emptyCells = currentGrid.getEmptyCells();
+ // Check if AI can win
+ for (var i = 0; i < emptyCells.length; i++) {
+ var cell = emptyCells[i];
+ currentGrid.cells[cell.row][cell.col].symbol = aiSymbol;
+ if (currentGrid.checkWinner() === aiSymbol) {
+ currentGrid.cells[cell.row][cell.col].symbol = null;
+ return cell;
+ }
+ currentGrid.cells[cell.row][cell.col].symbol = null;
+ }
+ // Check if AI needs to block player
+ for (var i = 0; i < emptyCells.length; i++) {
+ var cell = emptyCells[i];
+ currentGrid.cells[cell.row][cell.col].symbol = playerSymbol;
+ if (currentGrid.checkWinner() === playerSymbol) {
+ currentGrid.cells[cell.row][cell.col].symbol = null;
+ return cell;
+ }
+ currentGrid.cells[cell.row][cell.col].symbol = null;
+ }
+ return null;
+}
+function endGame(winner) {
+ gameState = 'gameOver';
+ if (winner === playerSymbol) {
+ statusText.setText("You Win!");
+ statusText.tint = 0x2ecc71;
+ LK.getSound('gameWin').play();
+ // Award points based on grid size
+ var points = currentGridSize * 10;
+ LK.setScore(LK.getScore() + points);
+ scoreText.setText('Score: ' + LK.getScore());
+ // Check for win condition
+ if (LK.getScore() >= 100) {
+ LK.setTimeout(function () {
+ LK.showYouWin();
+ }, 2000);
+ }
+ } else if (winner === aiSymbol) {
+ statusText.setText("AI Wins!");
+ statusText.tint = 0xe74c3c;
+ } else {
+ statusText.setText("It's a Tie!");
+ statusText.tint = 0xf39c12;
+ // Small points for tie
+ LK.setScore(LK.getScore() + 5);
+ scoreText.setText('Score: ' + LK.getScore());
+ }
+ // Reset to menu after delay
+ LK.setTimeout(function () {
+ showMenu();
+ statusText.tint = 0xe74c3c;
+ }, 3000);
+}
+// Initialize storage
+var playerStats = storage.stats || {
+ gamesWon: 0,
+ totalGames: 0,
+ bestScore: 0
+};
+// Update best score
+if (LK.getScore() > playerStats.bestScore) {
+ playerStats.bestScore = LK.getScore();
+ storage.stats = playerStats;
+}
+// Start with menu
+showMenu();
+game.update = function () {
+ // Update game logic if needed
+};
\ No newline at end of file