diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..fcdf5cc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/tic-tac-toe.iml b/.idea/tic-tac-toe.iml new file mode 100644 index 0000000..0c8867d --- /dev/null +++ b/.idea/tic-tac-toe.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/index.js b/index.js index 7553909..07de6f5 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,10 @@ const CROSS = 'X'; const ZERO = 'O'; const EMPTY = ' '; - +let winnerExist = false; const container = document.getElementById('fieldWrapper'); +let field = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY],[EMPTY, EMPTY, EMPTY]]; +let currentMove = CROSS; startGame(); addResetListener(); @@ -28,19 +30,85 @@ function renderGrid (dimension) { function cellClickHandler (row, col) { // Пиши код тут - console.log(`Clicked on cell: ${row}, ${col}`); - - + if (field[row][col] === EMPTY && !winnerExist) { + renderSymbolInCell(currentMove, row, col); + winnerExist = checkWinner(row, col); + currentMove = currentMove === CROSS ? ZERO : CROSS; + + console.log(`Clicked on cell: ${row}, ${col}`); + if (!winnerExist) { + let flag = false; + for (let row of field) { + for (let col of row) { + if (col === EMPTY) { + flag = true; + } + } + } + if (!flag) { + alert('Победила дружба') + } + } else { + alert(field[row][col] + ' wins'); + } + } /* Пользоваться методом для размещения символа в клетке так: renderSymbolInCell(ZERO, row, col); */ } -function renderSymbolInCell (symbol, row, col, color = '#333') { - const targetCell = findCell(row, col); + +function checkWinner(row, col) { + + if (field[row][0] === field[row][1] && field[row][1] === field[row][2]){ + for (let i = 0; i < 3; i++) + renderSymbolInCell(field[row][i], row, i, '#FF0000'); + return true; + } + + if (field[0][col] === field[1][col] && field[1][col] === field[2][col]){ + for (let i = 0; i < 3; i++) + renderSymbolInCell(field[i][col], i, col, '#FF0000'); + return true; + } + if ((row + col) % 2 === 0){ + if ( row === 1 && col === 1 ){ + return checkMainDiagonal() || checkSecondDiagonal(); + } else { + if (row === col) { + return checkMainDiagonal(); + } else { + return checkSecondDiagonal(); + } + } + } + return false; +} + +function checkMainDiagonal(){ + if (field[0][0] === field[1][1] && field[1][1] === field[2][2]) { + for (let i = 0; i < 3; i++) { + renderSymbolInCell(field[i][i], i, i, '#FF0000'); + } + } + + return (field[0][0] === field[1][1] && field[1][1] === field[2][2]); +} + +function checkSecondDiagonal() { + if (field[2][0] === field[1][1] && field[1][1] === field[0][2]) { + for (let i = 0; i < 3; i++) { + renderSymbolInCell(field[i][2-i], i, 2-i, '#FF0000'); + } + } + return (field[2][0] === field[1][1] && field[1][1] === field[0][2]); +} +function renderSymbolInCell (symbol, row, col, color = '#333') { + const targetCell = findCell(row, col) targetCell.textContent = symbol; targetCell.style.color = color; + field[row][col] = symbol; } function findCell (row, col) { @@ -54,6 +122,10 @@ function addResetListener () { } function resetClickHandler () { + startGame(); + field = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY],[EMPTY, EMPTY, EMPTY]]; + winnerExist = false; + currentMove = CROSS; console.log('reset!'); }