"Tic Tac Toe using html, javascript and css" Code Answer's
You're definitely familiar with the best coding language Javascript that developers use to develop their projects and they get all their queries like "Tic Tac Toe using html, javascript and css" answered properly. Developers are finding an appropriate answer about Tic Tac Toe using html, javascript and css related to the Javascript coding language. By visiting this online portal developers get answers concerning Javascript codes question like Tic Tac Toe using html, javascript and css. Enter your desired code related query in the search bar and get every piece of information about Javascript code related question on Tic Tac Toe using html, javascript and css.
Tic Tac Toe using html, javascript and css
const player = "O";
const computer = "X";
let board_full = false;
let play_board = ["", "", "", "", "", "", "", "", ""];
const board_container = document.querySelector(".play-area");
const winner_statement = document.getElementById("winner");
check_board_complete = () => {
let flag = true;
play_board.forEach(element => {
if (element != player && element != computer) {
flag = false;
}
});
board_full = flag;
};
const check_line = (a, b, c) => {
return (
play_board[a] == play_board[b] &&
play_board[b] == play_board[c] &&
(play_board[a] == player || play_board[a] == computer)
);
};
const check_match = () => {
for (i = 0; i < 9; i += 3) {
if (check_line(i, i + 1, i + 2)) {
return play_board[i];
}
}
for (i = 0; i < 3; i++) {
if (check_line(i, i + 3, i + 6)) {
return play_board[i];
}
}
if (check_line(0, 4, 8)) {
return play_board[0];
}
if (check_line(2, 4, 6)) {
return play_board[2];
}
return "";
};
const check_for_winner = () => {
let res = check_match()
if (res == player) {
winner.innerText = "Winner is player!!";
winner.classList.add("playerWin");
board_full = true
} else if (res == computer) {
winner.innerText = "Winner is computer";
winner.classList.add("computerWin");
board_full = true
} else if (board_full) {
winner.innerText = "Draw!";
winner.classList.add("draw");
}
};
const render_board = () => {
board_container.innerHTML = ""
play_board.forEach((e, i) => {
board_container.innerHTML += `<div id="block_${i}" class="block" onclick="addPlayerMove(${i})">${play_board[i]}</div>`
if (e == player || e == computer) {
document.querySelector(`#block_${i}`).classList.add("occupied");
}
});
};
const game_loop = () => {
render_board();
check_board_complete();
check_for_winner();
}
const addPlayerMove = e => {
if (!board_full && play_board[e] == "") {
play_board[e] = player;
game_loop();
addComputerMove();
}
};
const addComputerMove = () => {
if (!board_full) {
do {
selected = Math.floor(Math.random() * 9);
} while (play_board[selected] != "");
play_board[selected] = computer;
game_loop();
}
};
const reset_board = () => {
play_board = ["", "", "", "", "", "", "", "", ""];
board_full = false;
winner.classList.remove("playerWin");
winner.classList.remove("computerWin");
winner.classList.remove("draw");
winner.innerText = "";
render_board();
};
//initial render
render_board();
Source: dev.to
All those coders who are working on the Javascript based application and are stuck on Tic Tac Toe using html, javascript and css can get a collection of related answers to their query. Programmers need to enter their query on Tic Tac Toe using html, javascript and css related to Javascript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Tic Tac Toe using html, javascript and css for the programmers working on Javascript code while coding their module. Coders are also allowed to rectify already present answers of Tic Tac Toe using html, javascript and css while working on the Javascript language code. Developers can add up suggestions if they deem fit any other answer relating to "Tic Tac Toe using html, javascript and css". Visit this developer's friendly online web community, CodeProZone, and get your queries like Tic Tac Toe using html, javascript and css resolved professionally and stay updated to the latest Javascript updates.