let btnTab = [["btn00","btn01","btn02"], ["btn10","btn11","btn12"], ["btn20","btn21","btn22"]] btn00 = document.getElementById("btn00"); btn01 = document.getElementById("btn01"); btn02 = document.getElementById("btn02"); btn10 = document.getElementById("btn10"); btn11 = document.getElementById("btn11"); btn12 = document.getElementById("btn12"); btn20 = document.getElementById("btn20"); btn21 = document.getElementById("btn21"); btn22 = document.getElementById("btn22"); info = document.getElementById("info"); let tab = [["00","01","02"], ["10","11","12"], ["20","21","22"]]; let isGameOver = false; //document.write(tab[2][2]); let sign = ["o", "x"]; let currentSign = sign[0]; // i: row // j: column for (let i = 0; i < tab.length; i++) { for (let j = 0; j < tab.length; j++) { //let btn = `${btnTab[i][j]}`; let btn = eval(btnTab[i][j]); console.log(btn); btn.onclick = function(){ btn.innerText = currentSign; btn.disabled = true; tab[i][j] = currentSign; isGameOver = check(i,j,currentSign); if (!isGameOver){ console.log(isGameOver); changeSign(); //info.innerText = tab; }else{ for (let i = 0; i < tab.length; i++) { for (let j = 0; j < tab.length; j++) { let btn = eval(btnTab[i][j]); btn.disabled = true; } } info.innerText = "Game Over"; } } } } changeSign = function(){ console.log(currentSign); if(currentSign == sign[0]){ currentSign = sign[1]; }else{ currentSign = sign[0]; } } check = function(i,j,currentSign){ isGameOver = false; isCheckedDiagonal = false; isCheckedColumn = checkColumn(i,j,currentSign); isCheckedRow = checkRow(i,j,currentSign); if (i == j || (i == 0 && j == tab.length - 1 ) || (j == 0 && i == tab.length - 1 ) ){ isCheckedDiagonal = checkDiagonal(i,j,currentSign); } if (isCheckedColumn || isCheckedRow || isCheckedDiagonal){ isGameOver = true; } return isGameOver; } // i: row // j: column checkColumn = function (i,j,currentSign){ isGameOver = false; let checked = 0; for(let i = 0; i < tab.length; i++){ if (tab[i][j] == currentSign){ checked ++; } } if (checked == tab.length){ isGameOver = true; } return isGameOver; } // i: row // j: column checkRow = function (i,j,currentSign){ isGameOver = false; let checked = 0; for(let j = 0; j < tab.length; j++){ if (tab[i][j] == currentSign){ checked ++; } } if (checked == tab.length){ isGameOver = true; } return isGameOver; } // i: row // j: column checkDiagonal = function (i,j,currentSign){ isGameOver = false; isDiagonalGameOver1 = false; isDiagonalGameOver2 = false; let checked1 = 0; let checked2 = 0; if (i == j){ i = 0; for(let j = 0; j < tab.length; j++){ if (tab[i][j] == currentSign){ checked1 ++; } i++; } if (checked1 == tab.length){ isDiagonalGameOver1 = true; } } if ((i == 0 && j == tab.length - 1 ) || (j == 0 && i == tab.length - 1 )){ i = tab.length - 1; for(let j = 0; j < tab.length; j++){ if (tab[i][j] == currentSign){ checked2 ++; } i--; } if (checked2 == tab.length){ isDiagonalGameOver2 = true; } } if(isDiagonalGameOver1 || isDiagonalGameOver2){ isGameOver = true; } return isGameOver; }