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].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();
===================================================================
--- original.js
+++ change.js
@@ -116,25 +116,40 @@
}
}
};
self.computerMove = function () {
- // Check if 'X' can win in the next move and block it
+ // 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 'X' and check for win
+ // 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'
+ // Block 'X' by placing 'O'
self.grid[i][j].setPlayer(self.currentPlayer);
return;
}
- // Reset the spot
+ // Reset the spot
self.grid[i][j].setPlayer('');
}
}
}
- // If no blocking move is found, take the next available space
+ // 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);
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.