Question

In: Computer Science

make a 4x4 tic tac toe in javascript (X is the user) (O is the computer)

make a 4x4 tic tac toe in javascript (X is the user) (O is the computer)

Solutions

Expert Solution

/* Pleasefind below the index.html file for tic tac toe 4*4(containing js code)*/

/* To find wining position every box is "marked" from top to bottom, left to right, with increasing power of 2. Each box thus is an individual bit is nine bit string, and a player boxes at any given time can be viewed as a nine-bit number. A winner can thus be easily determined by checking whether the player's current nine bits have covered any of the eight three-in-a-row combination.*/

/* Save the below file as index.html, and run in any browser */

<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">

(function () {

var boxes = [],notfilled = "\xA0",n = 4,MAXMOVES = n * n,setcore,moves,turn = "X";
conditionforwin = [0x000F,/*predefined*/0x00F0,/*hardcoded*/0x0F00,/*values*/0xF000,/*values*/0x1111,/*values*/0x2222,/*values*/0x4444,/*values*/0x8888,/*values*/0x8421,/*values*/0x1248,],


gamestart = function () {
var i;
  
turn = "X";
setcore = {"X": 0, "O": 0};
moves = 0;
for (i = 0; i < boxes.length; i += 1) {
boxes[i].firstChild.nodeValue = notfilled;
}
},


win = function (setcore) {
var i;
for (i = 0; i < conditionforwin.length; i += 1) {
if ((conditionforwin[i] & setcore) === conditionforwin[i]) {
return true;
}
}
return false;
},

  
set = function () {
if (this.firstChild.nodeValue !== notfilled) {
return;
}
this.firstChild.nodeValue = turn;
moves += 1;
setcore[turn] += this.points;
console.log(turn + " " + setcore[turn]);
if (win(setcore[turn])) {
alert(turn + " conditionforwin!");
gamestart();
} else if (moves === MAXMOVES) {
alert("Cat\u2019s game!");
gamestart();
} else {
turn = turn === "X" ? "O" : "X";
}
},

startplaying = function () {
var board = document.createElement("table"),
points = 1,
i, j,
tuple, column,
previous;
board.border = 1;
for (i = 0; i < n; i += 1) {
tuple = document.createElement("tr");
board.appendChild(tuple);
for (j = 0; j < n; j += 1) {
column = document.createElement("td");
column.align = column.valign = 'center';
column.width = column.height = 50;
column.onclick = set;
column.points = points;
  
column.appendChild(document.createTextNode(""));
tuple.appendChild(column);
boxes.push(column);
points += points;
}
}

previous = document.getElementById("tictactoe") || document.body;
previous.appendChild(board);
gamestart();
};
if (typeof window.onload === "function") {
setwhattodoloading = window.onload;
window.onload = function () {
setwhattodoloading();
startplaying();
};
} else {
window.onload = startplaying;
}
}());

</script>
</head>
<body>

</body>
</html>


Related Solutions

How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
explain a pseudocode for tic tac toe in c++ between a computer and a player in...
explain a pseudocode for tic tac toe in c++ between a computer and a player in words
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (Tic-Tac-Toe) The game is single player, human vs the computer AI.
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML...
For this assignment, you will write a tic-tac-toe application in HTML and JavaScript, using an HTML <canvas> tag. The game will be played "hot seat" where players take turns using the same device. Requirements: The canvas should be 600px tall and wide, with the gameplay area occupying most of the canvas. The X's and O's may be drawn using polygons or large-font text The grid should be drawn using polygons, specifically long, thin rectangles Before & between games, the canvas...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm =...
Here is an interaction in a tic-tac-toe game, with user input in bold: >> gm = Game.new('andy', 'mike') => #<Game:0x2e91d78 @players=[Andy, Mike]> >> gm.play_game('mike') Mike, enter your next O O-- --- --- Andy, enter your next X O-X --- --- Mike, enter your next O O-X -O- --- Andy, enter your next X move Bad move dude! You go again. O-X -O- --- Andy, enter your next X move O-X -OX --- Mike, enter your next O move O-X -OX...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac toe alternatively. Anyone could win when they first make a bingo for any line(horizontal, vertical, diagonal) [Constraints] 1. Use 2dimensional Array(3 X 3) 2. Each elements are 1 or 2 so that can show their player1, player2's mark. 3. Inputs are 1 through 9 as you can see 1 2 3 4 5 6 7 8 9 4. No inputs are duplicated 5. if...
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O)...
Tony Gaddis C++ Tic-Tac-Toe Write a program that allows two players (player X and player O) to play a game of tic-tac-toe. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that: Displays the contents...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT