40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
|
let symbol = "X"
|
||
|
let matrix = [-1, -1, -1, -1, -1, -1, -1, -1, -1]
|
||
|
let num = 0
|
||
|
let wincombo = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
|
||
|
|
||
|
for (let i = 0; i < 9; i++){
|
||
|
document.getElementById(i.toString()).addEventListener("click", function(){
|
||
|
if (this.innerHTML == ""){
|
||
|
this.innerHTML = symbol
|
||
|
num = this.id
|
||
|
if (symbol.toLowerCase() === "x")
|
||
|
matrix[num] = 1
|
||
|
else
|
||
|
matrix[num] = 0
|
||
|
this.classList.add(symbol.toLowerCase())
|
||
|
let nyertes = CheckWin()
|
||
|
if (nyertes == 1 || nyertes == 0)
|
||
|
document.getElementById("nyertes").innerHTML = `Nyertes játékos: ${symbol}`
|
||
|
else{
|
||
|
if (symbol === "X")
|
||
|
symbol = "O"
|
||
|
else symbol = "X"
|
||
|
document.getElementById("nyertes").innerHTML = `Folyamatban a játék, <i>${symbol}</i> játékos következik.`
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
function CheckWin(){
|
||
|
for (let ind of wincombo){
|
||
|
if (matrix[ind[0]] == matrix[ind[1]] && matrix[ind[2]] == matrix[ind[1]] && matrix[ind[0]] != -1)
|
||
|
return matrix[ind[0]]
|
||
|
}
|
||
|
return -1
|
||
|
}
|
||
|
|
||
|
function ujra(){
|
||
|
location.reload()
|
||
|
}
|