/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Class for a Tic Tac Toe cell
var Cell = Container.expand(function () {
var self = Container.call(this);
self.value = null; // 'X', 'O', or null
self.graphics = self.attachAsset('cell', {
anchorX: 0.5,
anchorY: 0.5
});
self.setValue = function (val) {
self.value = val;
if (val === 'X') {
self.graphics.tint = 0xff0000; // Red for player
} else if (val === 'O') {
self.graphics.tint = 0x0000ff; // Blue for robot
}
};
self.isEmpty = function () {
return self.value === null;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var board = [];
var currentPlayer = 'X'; // Player starts first
var gameOver = false;
// Initialize the board
function initBoard() {
var startX = 1024 - 150; // Center the board horizontally
var startY = 1366 - 150; // Center the board vertically
var cellSize = 100;
for (var i = 0; i < 3; i++) {
board[i] = [];
for (var j = 0; j < 3; j++) {
var cell = new Cell();
cell.x = startX + j * cellSize;
cell.y = startY + i * cellSize;
game.addChild(cell);
board[i][j] = cell;
}
}
}
// Check for a win or draw
function checkGameState() {
// Check rows, columns, and diagonals
for (var i = 0; i < 3; i++) {
if (board[i][0].value && board[i][0].value === board[i][1].value && board[i][1].value === board[i][2].value) {
return board[i][0].value;
}
if (board[0][i].value && board[0][i].value === board[1][i].value && board[1][i].value === board[2][i].value) {
return board[0][i].value;
}
}
if (board[0][0].value && board[0][0].value === board[1][1].value && board[1][1].value === board[2][2].value) {
return board[0][0].value;
}
if (board[0][2].value && board[0][2].value === board[1][1].value && board[1][1].value === board[2][0].value) {
return board[0][2].value;
}
// Check for draw
var draw = true;
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (board[i][j].isEmpty()) {
draw = false;
break;
}
}
}
return draw ? 'Draw' : null;
}
// Handle player move
function playerMove(x, y) {
if (gameOver) return;
var cellX = Math.floor((x - (1024 - 150)) / 100);
var cellY = Math.floor((y - (1366 - 150)) / 100);
if (cellX >= 0 && cellX < 3 && cellY >= 0 && cellY < 3 && board[cellY][cellX].isEmpty()) {
board[cellY][cellX].setValue(currentPlayer);
var result = checkGameState();
if (result) {
gameOver = true;
if (result === 'Draw') {
console.log("It's a draw!");
} else {
console.log(result + " wins!");
}
} else {
currentPlayer = 'O';
robotMove();
}
}
}
// Simple AI for robot move
function robotMove() {
if (gameOver) return;
// Random move for simplicity
var emptyCells = [];
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (board[i][j].isEmpty()) {
emptyCells.push(board[i][j]);
}
}
}
if (emptyCells.length > 0) {
var randomCell = emptyCells[Math.floor(Math.random() * emptyCells.length)];
randomCell.setValue(currentPlayer);
var result = checkGameState();
if (result) {
gameOver = true;
if (result === 'Draw') {
console.log("It's a draw!");
} else {
console.log(result + " wins!");
}
} else {
currentPlayer = 'X';
}
}
}
// Initialize the game
initBoard();
// Add event listeners
game.down = function (x, y, obj) {
playerMove(x, y);
};
the letter x. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
the letter o. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
explosion. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
button with the word restart on it. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
confetti popper. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
sad face. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
handshake. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.