Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let playerText = document.getElementById("playerText")
- let restartButton = document.getElementById("restartButton")
- let boxes = Array.from(document.getElementsByClassName("box"))
- const xText = "X"
- const oText = "O"
- let currentPlayer = xText
- let cells = Array(9).fill(null)
- function startGame() {
- for(let i = 0; i < 9; i++) {
- boxes[i].addEventListener('click', boxClicked)
- }
- }
- function boxClicked(e) {
- let cellsNotEmpty = 0;
- let idx = e.target.id;
- for(let i = 0; i < 9; i++) {
- if(cells[i]) {
- cellsNotEmpty++;
- }
- }
- if(cellsNotEmpty == 9) {
- playerText.innerText = "It is a draw!"
- return
- }
- if(!cells[idx]) {
- cells[idx] = currentPlayer
- e.target.innerText = currentPlayer
- if(playerWon()) {
- }
- if(currentPlayer == xText) {
- currentPlayer = oText
- }
- else {
- currentPlayer = xText
- }
- }
- }
- function playerWon() {
- }
- /**
- * winning combos
- * 0, 1, 2
- * 3, 4, 5
- * 6, 7, 8
- * 0, 3, 6
- * 1, 4, 7
- * 2, 5, 8
- * 0, 4, 8
- * 2, 4, 6
- */
- startGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement