From 4dd999972fc66462bd0438a85a49d93c1d81d4a6 Mon Sep 17 00:00:00 2001 From: dclxxxvi Date: Tue, 5 Oct 2021 17:07:49 +0500 Subject: [PATCH 1/4] 1-3 complete --- index.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 7553909..e6ac8e8 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,13 @@ const EMPTY = ' '; const container = document.getElementById('fieldWrapper'); +let currentPlayer; startGame(); addResetListener(); function startGame () { renderGrid(3); + currentPlayer = CROSS; } function renderGrid (dimension) { @@ -27,13 +29,9 @@ function renderGrid (dimension) { } function cellClickHandler (row, col) { - // Пиши код тут + renderSymbolInCell(currentPlayer, row, col) + console.log(`Clicked on cell: ${row}, ${col}`); - - - /* Пользоваться методом для размещения символа в клетке так: - renderSymbolInCell(ZERO, row, col); - */ } function renderSymbolInCell (symbol, row, col, color = '#333') { From 5793d96cb5bcd323174cf184355339ddb25788c8 Mon Sep 17 00:00:00 2001 From: dclxxxvi Date: Tue, 5 Oct 2021 17:11:49 +0500 Subject: [PATCH 2/4] =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=20=D0=A2=D0=B8=D1=85=D0=BE=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e6ac8e8..366fffa 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ const EMPTY = ' '; const container = document.getElementById('fieldWrapper'); let currentPlayer; +let cells = new Array(); + startGame(); addResetListener(); @@ -17,8 +19,10 @@ function renderGrid (dimension) { container.innerHTML = ''; for (let i = 0; i < dimension; i++) { + cells[i] = new Array(); const row = document.createElement('tr'); for (let j = 0; j < dimension; j++) { + cells[i][j] = EMPTY; const cell = document.createElement('td'); cell.textContent = EMPTY; cell.addEventListener('click', () => cellClickHandler(i, j)); @@ -29,13 +33,25 @@ function renderGrid (dimension) { } function cellClickHandler (row, col) { - renderSymbolInCell(currentPlayer, row, col) - + if (cells[row][col] != EMPTY) { + return; + } + renderSymbolInCell(currentPlayer, row, col); + //checkWinner(currentPlayer); + console.log(cells); console.log(`Clicked on cell: ${row}, ${col}`); + currentPlayer = (currentPlayer == CROSS) ? ZERO : CROSS; +} + +function checkWinner(player) { + for(let i = 0; i < length(cells); i++) { + + } } function renderSymbolInCell (symbol, row, col, color = '#333') { const targetCell = findCell(row, col); + cells[row][col] = symbol; targetCell.textContent = symbol; targetCell.style.color = color; From 66b757b2285bad23c4c83d89319b17880981ebfe Mon Sep 17 00:00:00 2001 From: dclxxxvi Date: Tue, 5 Oct 2021 21:29:30 +0500 Subject: [PATCH 3/4] =?UTF-8?q?=D0=A2=D0=B8=D1=85=D0=BE=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 136 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 122 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 366fffa..1ef12d9 100644 --- a/index.js +++ b/index.js @@ -5,24 +5,38 @@ const EMPTY = ' '; const container = document.getElementById('fieldWrapper'); let currentPlayer; -let cells = new Array(); - +let currentTurn; +let field; +let maxTurns; +let dimension; +let winCombination; +let isGameOver; + +getDimensionCount(); startGame(); addResetListener(); function startGame () { - renderGrid(3); + isGameOver = false; + maxTurns = dimension ** 2; + field = []; + renderGrid(dimension); currentPlayer = CROSS; + currentTurn = 1; +} + +function getDimensionCount() { + dimension = parseInt(prompt('Введите количество измерений', '3')); } function renderGrid (dimension) { container.innerHTML = ''; for (let i = 0; i < dimension; i++) { - cells[i] = new Array(); + field[i] = []; const row = document.createElement('tr'); for (let j = 0; j < dimension; j++) { - cells[i][j] = EMPTY; + field[i][j] = EMPTY; const cell = document.createElement('td'); cell.textContent = EMPTY; cell.addEventListener('click', () => cellClickHandler(i, j)); @@ -33,25 +47,109 @@ function renderGrid (dimension) { } function cellClickHandler (row, col) { - if (cells[row][col] != EMPTY) { + cellClick(row, col); +} + +function cellClick (row, col) { + if (field[row][col] != EMPTY || isGameOver) { return; } renderSymbolInCell(currentPlayer, row, col); - //checkWinner(currentPlayer); - console.log(cells); - console.log(`Clicked on cell: ${row}, ${col}`); + checkWinner(currentPlayer, row, col); + currentTurn++; currentPlayer = (currentPlayer == CROSS) ? ZERO : CROSS; } -function checkWinner(player) { - for(let i = 0; i < length(cells); i++) { - +function checkWinner(player, row, col) { + if (currentTurn < dimension * 2 - 1) return; + if (isWinCombination(rowWinnerCombination(player, row)) + || isWinCombination(colWinnerCombination(player, col)) + || isWinCombination(mainDiagWinnerCombination(player)) + || isWinCombination(secondDiagWinnerCombination(player))) { + alertWinner(player); + } + if (currentTurn == maxTurns){ + alert('Победила дружба!'); + isGameOver = true; + return; + } +} + +function isWinCombination(combination) { + if (combination.length == dimension) { + winCombination = combination; + return true; + } + return false; +} + +function mainDiagWinnerCombination(player) { + combination = []; + for (let i = 0; i < dimension; i++){ + if (field[i][i] == player){ + combination.push([i, i]); + } + else return []; + } + return combination; +} + +function secondDiagWinnerCombination(player) { + combination = []; + for (let i = 0; i < dimension; i++){ + if (field[dimension - 1 - i][i] == player){ + combination.push([dimension - 1 - i, i]); + } + else return []; + } + return combination; +} + +function rowWinnerCombination(player, row) { + combination = []; + for (let i = 0; i < dimension; i++){ + if (field[row][i] == player){ + combination.push([row, i]); + } + else return []; + } + return combination; +} + +function colWinnerCombination(player, col) { + combination = []; + for (let i = 0; i < dimension; i++){ + if (field[i][col] == player){ + combination.push([i, col]); + } + else return []; } + return combination; +} + +function alertWinner(player) { + switch(player) { + case CROSS: + alert('Победили крестики!'); + break; + case ZERO: + alert('Победили нолики!'); + break; + } + isGameOver = true; + colorWinner(winCombination); +} + +function colorWinner(winComb) { + winComb.forEach(winCell => { + const cell = findCell(winCell[0], winCell[1]); + cell.style.backgroundColor = "red"; + }); } function renderSymbolInCell (symbol, row, col, color = '#333') { const targetCell = findCell(row, col); - cells[row][col] = symbol; + field[row][col] = symbol; targetCell.textContent = symbol; targetCell.style.color = color; @@ -68,9 +166,19 @@ function addResetListener () { } function resetClickHandler () { - console.log('reset!'); + startGame(); } +function botTurn() { + while(true){ + x = Math.floor(Math.random() * dimension); + y = Math.floor(Math.random() * dimension); + if (field[x][y] == EMPTY) { + clickOnCell(x, y); + break; + } + } +} /* Test Function */ /* Победа первого игрока */ From 814a3dba1f28c56487e9ca21d2898dee7bcb58cf Mon Sep 17 00:00:00 2001 From: dclxxxvi Date: Tue, 5 Oct 2021 21:52:20 +0500 Subject: [PATCH 4/4] =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=20=D0=A2=D0=B8=D1=85=D0=BE=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1ef12d9..5febb50 100644 --- a/index.js +++ b/index.js @@ -47,7 +47,11 @@ function renderGrid (dimension) { } function cellClickHandler (row, col) { + if (field[row][col] != EMPTY || isGameOver) { + return; + } cellClick(row, col); + botTurn(); } function cellClick (row, col) { @@ -170,11 +174,11 @@ function resetClickHandler () { } function botTurn() { - while(true){ + while(!isGameOver){ x = Math.floor(Math.random() * dimension); y = Math.floor(Math.random() * dimension); if (field[x][y] == EMPTY) { - clickOnCell(x, y); + cellClick(x, y); break; } }