User prompt
on computer's move first check to see if 'O' could win using an available space before preventing an 'X' win
User prompt
your new code incorrectly leaves X's in every space
User prompt
on computer's move check to see if 'X' could win on its next move and block it, else continue with using next available space
User prompt
in the computerMove function, check the remaining unoccupied spaces to see if player 'X' could win and choose that space instead of the first available unoccupied space, thus preventing an 'X' win
User prompt
when it is the computers turn, first check to see if there is a possible win for 'X' - if so block it
User prompt
make the computer move more intelligent by checking for a potential 3 X's in a line and blocking it with an 'O'
Code edit (1 edits merged)
Please save this source code
User prompt
when there is a draw, you are incorrectly displaying "Draw Wins!" instead is should be just "Draw!"
User prompt
you should be displaying game over play again after 'O' wins
User prompt
after the user (human) plays the 'X' move, you are correctly making a computer generated 'O' move but then play should toggle back to the user to play another 'X' move
User prompt
the user should only play the 'X' moves the computer should play the 'O' moves
User prompt
get the computer to play to 'O' moves
User prompt
show current player: X at start of game
User prompt
remove the restart button
User prompt
If a Draw is reached you should offer to lay again, like you do when there is a win
User prompt
The 'X' player is correctly shown on the first move, but then the 'O' player should play next, instead, this code is always showing an 'X'
User prompt
Now it is incorrectly displaying Draw! after one move. Draw! should only be displayed if neither player has won and there are no more places to move
User prompt
Now it is incorrectly displaying Draw Wins! after one move
User prompt
do not display Draw! after the first 'X' has played
User prompt
you only allow 'X' to play never 'O'
User prompt
you are also wrongly displaying Draw! at the start of the game
/****
* 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] === '') {
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!');
} else {
statusText.setText(result + ' Wins!');
LK.showGameOver();
}
} else {
if (self.currentPlayer === 'X') {
self.currentPlayer = 'O';
} else {
self.currentPlayer = 'X';
}
}
}
};
});
// 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;
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
});
}
};
});
// Class for the restart button
var RestartButton = Container.expand(function () {
var self = Container.call(this);
self.attachAsset('restartButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
// Reset the board
board.resetBoard();
// Reset the pieces
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
board.grid[i][j].setPlayer('');
}
}
// Update the game status
updateStatus();
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Blue color for O-player
// Initialize the board
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') {
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();
};
// Initialize the restart button
var restartButton = new RestartButton();
restartButton.x = 2048 / 2;
restartButton.y = 2732 - 200;
game.addChild(restartButton);
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.