/****
* Classes
****/
// Class for the Tic Tac Toe board
var Board = Container.expand(function () {
var self = Container.call(this);
self.grid = [[new Piece(), new Piece(), new Piece()], [new Piece(), new Piece(), new Piece()], [new Piece(), new Piece(), new Piece()]];
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
self.addChild(self.grid[i][j]);
self.grid[i][j].x = i * 2048 / 3 + 2048 / 6;
self.grid[i][j].y = j * 2732 / 3 + 2732 / 6;
}
}
self.currentPlayer = 'X';
// Create the board with two horizontal and two vertical white lines
line1 = self.attachAsset('line1', {
x: 0,
y: 2732 / 3
});
line2 = self.attachAsset('line2', {
x: 0,
y: 2 * 2732 / 3
});
line3 = self.attachAsset('line3', {
x: 2048 / 3,
y: 0
});
line4 = self.attachAsset('line4', {
x: 2 * 2048 / 3,
y: 0
});
// Method to check for a win or draw
self.checkGameOver = function () {
// Check rows, columns, and diagonals for a win
for (var i = 0; i < 3; i++) {
if (self.grid[i][0].player === self.grid[i][1].player && self.grid[i][1].player === self.grid[i][2].player && self.grid[i][0].player !== '') {
return self.grid[i][0].player;
}
if (self.grid[0][i].player === self.grid[1][i].player && self.grid[1][i].player === self.grid[2][i].player && self.grid[0][i].player !== '') {
return self.grid[0][i].player;
}
}
if (self.grid[0][0].player === self.grid[1][1].player && self.grid[1][1].player === self.grid[2][2].player && self.grid[0][0].player !== '') {
return self.grid[0][0].player;
}
if (self.grid[0][2].player === self.grid[1][1].player && self.grid[1][1].player === self.grid[2][0].player && self.grid[0][2].player !== '') {
return self.grid[0][2].player;
}
// Check for draw
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (self.grid[i][j].player === '') {
return null;
}
}
}
return 'Draw';
};
// Method to reset the board
self.resetBoard = function () {
// Reset the grid
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
self.grid[i][j].setPlayer('');
}
}
// Reset the current player
self.currentPlayer = 'X';
};
// Method to handle a move
self.makeMove = function (x, y) {
if (self.grid[x][y].player === '') {
self.grid[x][y].setPlayer(self.currentPlayer);
var result = self.checkGameOver();
if (result) {
if (result === 'Draw') {
statusText.setText('Draw!');
LK.showGameOver({
title: 'Draw!',
button: 'Play Again',
onButtonPress: function onButtonPress() {
self.resetBoard();
updateStatus();
}
});
} else {
statusText.setText(result + ' Wins!');
LK.showGameOver({
title: result + ' Wins!',
button: 'Play Again',
onButtonPress: function onButtonPress() {
self.resetBoard();
updateStatus();
}
});
}
} else {
if (self.currentPlayer === 'X') {
self.currentPlayer = 'O';
self.computerMove();
self.currentPlayer = 'X';
var result = self.checkGameOver();
if (result) {
statusText.setText(result + ' Wins!');
LK.showGameOver({
title: result + ' Wins!',
button: 'Play Again',
onButtonPress: function onButtonPress() {
self.resetBoard();
updateStatus();
}
});
}
}
}
}
};
self.computerMove = function () {
// Check if 'O' can win in the next move and take it
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (self.grid[i][j].player === '') {
// Temporarily place 'O' and check for win
self.grid[i][j].setPlayer('O');
if (self.checkGameOver() === 'O') {
// 'O' can win, so keep the move
return;
}
// Reset the spot
self.grid[i][j].setPlayer('');
}
}
}
// Check if 'X' can win in the next move and block it
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (self.grid[i][j].player === '') {
// Temporarily place 'X' and check for win
self.grid[i][j].setPlayer('X');
if (self.checkGameOver() === 'X') {
// Block 'X' by placing 'O'
self.grid[i][j].setPlayer(self.currentPlayer);
return;
}
// Reset the spot
self.grid[i][j].setPlayer('');
}
}
}
// If no winning or blocking move is found, take the next available space
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (self.grid[i][j].player === '') {
self.grid[i][j].setPlayer(self.currentPlayer);
return;
}
}
}
};
});
// Red color for X-player
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the X and O pieces
var Piece = Container.expand(function () {
var self = Container.call(this);
self.player = '';
self.setPlayer = function (player) {
self.player = player;
self.removeChildren(); // Clear previous assets
if (player === 'X') {
self.attachAsset('X-player', {
anchorX: 0.5,
anchorY: 0.5
});
} else if (player === 'O') {
self.attachAsset('O-player', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize the board
// Blue color for O-player
var board = new Board();
game.addChild(board);
// Create a text object to display the game status
var statusText = new Text2('', {
size: 100,
fill: 0xFFFFFF
});
statusText.anchor.set(0.5, 0);
LK.gui.top.addChild(statusText);
// Function to update the game status
function updateStatus() {
var result = board.checkGameOver();
if (result) {
//if (result === 'Draw' && board.currentPlayer !== 'X') {
if (result === 'Draw') {
statusText.setText('Draw!');
} else {
statusText.setText(result + ' Wins!');
}
} else {
statusText.setText('Current Player: ' + board.currentPlayer);
}
}
// Handle touch events to make moves
game.down = function (x, y, obj) {
var gridX = Math.floor(x / (2048 / 3));
var gridY = Math.floor(y / (2732 / 3));
board.makeMove(gridX, gridY);
updateStatus();
};
// Show current player: X at start of game
updateStatus();
A large letter 'O' as in tic tac toe, coloured blue. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
A large letter 'X' as in tic tac toe coloured red, in a similar style to the O-player image. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. No surrounding circle nor square.