const data = { // All the data of the game
gameSet: "In Game", // For stating who won the game or if it ended in a draw, is to be stated as "In Game" when the game is still in session
nextTurn: true, // What player is playing (true for player 1, while false is for player 2)
filledCells: [ // The script representation of the cells
[false,""], // Cell 1
[false,""], // Cell 2
[false,""], // Cell 3
[false,""], // Cell 4
[false,""], // Cell 5
[false,""], // Cell 6
[false,""], // Cell 7
[false,""], // Cell 8
[false,""], // Cell 9
false // This is only true if all cells are filled
],
pieces: ["X","O"], // The pieces
winCond: [ // The win conditions
[0, 1, 2],
[0, 3, 6],
[0, 4, 8],
[1, 4, 7],
[2, 4, 6],
[2, 5, 8],
[3, 4, 5],
[6, 7, 8]
]
};
var cellItems = document.querySelectorAll('.cell'); // The call for all the cells
var winner = document.getElementById("winner"); // The call to the winner display
var plCall = document.getElementById("pl"); // The call to the player's turn display
function endGame(winnr) { // This function ends the game
if (winnr==data.pieces[0]) { // If winnr equals 'X' then Player 1 wins
data.gameSet = "Player 1 Won";
} else if (winnr==data.pieces[1]) { // If winnr equals 'O' then Player 2 wins
data.gameSet = "Player 2 Won";
} else if (winnr=="XO") { // If winnr equals 'XO' then nether Player wins
data.gameSet = "Draw";
}
winner.innerHTML = data.gameSet; // Sets the winner display to display who won
document.getElementById("reset").style.display = "inline"; // Displays the reset button
}
function checkForWin() { // This is the function that checks for wins/draw
for (let on=0; on<data.pieces.length; on++) {
var onThis = data.pieces[on];
var love = 0;
for (let i=0; i<data.winCond.length; i++) {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.