From cba33a94c7fe1563d865552f556baf1d5575547a Mon Sep 17 00:00:00 2001 From: oh2 Date: Fri, 1 Oct 2021 16:14:56 +0500 Subject: [PATCH 1/3] 1-9 tasks --- index.js | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 7553909..15f0f88 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,20 @@ const ZERO = 'O'; const EMPTY = ' '; const container = document.getElementById('fieldWrapper'); +let fieldData; +let count; +let isPlayable; +let dimension; startGame(); addResetListener(); function startGame () { - renderGrid(3); + dimension = prompt("Введите размер поля"); + count = dimension**2; + fieldData = []; + isPlayable = true; + renderGrid(dimension); } function renderGrid (dimension) { @@ -16,9 +24,11 @@ function renderGrid (dimension) { for (let i = 0; i < dimension; i++) { const row = document.createElement('tr'); + fieldData.push([]); for (let j = 0; j < dimension; j++) { const cell = document.createElement('td'); cell.textContent = EMPTY; + fieldData[i][j] = EMPTY; cell.addEventListener('click', () => cellClickHandler(i, j)); row.appendChild(cell); } @@ -30,10 +40,100 @@ function cellClickHandler (row, col) { // Пиши код тут console.log(`Clicked on cell: ${row}, ${col}`); + if (findCell(row,col).textContent === EMPTY && isPlayable) + if(count % 2 === 0) + processClick (ZERO, row, col) + else + processClick (CROSS, row, col) + console.log(`This cell is: ${fieldData[row][col]}`); +} + +function processClick (symbol, row, col) { + renderSymbolInCell(symbol, row, col); + fieldData[row][col] = symbol; + if (checkWinner(symbol, row, col)) { + alert(`${symbol === CROSS ? "Крестики" : "Нолики"} победили!`) + isPlayable = false; + } + if (--count === 0 && isPlayable) + alert("Победила дружба!"); +} + +function checkWinner (symbol, row, col) { + + let isLined = true; + let winType; + { + for (let i = 0; i < fieldData.length; i++) { + if (fieldData[row][i] !== symbol) { + isLined = false; + break; + } + isLined = true; + } + winType = "ver"; + } + if (!isLined) { + for (let i = 0; i < fieldData.length; i++) { + if (fieldData[i][col] !== symbol) { + isLined = false; + break; + } + isLined = true; + } + winType = "hor"; + } + if (!isLined) { + for (let i = 0; i < fieldData.length; i++) { + if (fieldData[i][i] !== symbol) { + isLined = false; + break; + } + isLined = true; + } + winType = "diagL"; + } + if (!isLined) { + for (let i = 0; i < fieldData.length; i++) { + if (fieldData[i][fieldData.length - 1 - i] !== symbol) { + isLined = false; + break; + } + isLined = true; + } + winType = "diagR"; + } + + if (isLined) + changeColor(winType, row, col); + return isLined; +} + +function changeColor(way, row, col) +{ + for (let i = 0; i < fieldData.length; i++) { + switch (way) { + case "hor": + findCell(i, col).style.color = '#ff4040'; + break; + + case "ver": + findCell(row, i).style.color = '#ff4040'; + break; + + case "diagL": + findCell(i, i).style.color = '#ff4040'; + break; + + case "diagR": + findCell(i, fieldData.length - 1 - i).style.color = '#ff4040' + break; + } + } +} + +function computerMove () { - /* Пользоваться методом для размещения символа в клетке так: - renderSymbolInCell(ZERO, row, col); - */ } function renderSymbolInCell (symbol, row, col, color = '#333') { @@ -55,6 +155,7 @@ function addResetListener () { function resetClickHandler () { console.log('reset!'); + startGame(); } From 9136f184a09e374c01dc4d87b518ba84c9d1332f Mon Sep 17 00:00:00 2001 From: oh2 Date: Fri, 1 Oct 2021 16:14:56 +0500 Subject: [PATCH 2/3] 1-9 tasks --- index.js | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 7553909..15f0f88 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,20 @@ const ZERO = 'O'; const EMPTY = ' '; const container = document.getElementById('fieldWrapper'); +let fieldData; +let count; +let isPlayable; +let dimension; startGame(); addResetListener(); function startGame () { - renderGrid(3); + dimension = prompt("Введите размер поля"); + count = dimension**2; + fieldData = []; + isPlayable = true; + renderGrid(dimension); } function renderGrid (dimension) { @@ -16,9 +24,11 @@ function renderGrid (dimension) { for (let i = 0; i < dimension; i++) { const row = document.createElement('tr'); + fieldData.push([]); for (let j = 0; j < dimension; j++) { const cell = document.createElement('td'); cell.textContent = EMPTY; + fieldData[i][j] = EMPTY; cell.addEventListener('click', () => cellClickHandler(i, j)); row.appendChild(cell); } @@ -30,10 +40,100 @@ function cellClickHandler (row, col) { // Пиши код тут console.log(`Clicked on cell: ${row}, ${col}`); + if (findCell(row,col).textContent === EMPTY && isPlayable) + if(count % 2 === 0) + processClick (ZERO, row, col) + else + processClick (CROSS, row, col) + console.log(`This cell is: ${fieldData[row][col]}`); +} + +function processClick (symbol, row, col) { + renderSymbolInCell(symbol, row, col); + fieldData[row][col] = symbol; + if (checkWinner(symbol, row, col)) { + alert(`${symbol === CROSS ? "Крестики" : "Нолики"} победили!`) + isPlayable = false; + } + if (--count === 0 && isPlayable) + alert("Победила дружба!"); +} + +function checkWinner (symbol, row, col) { + + let isLined = true; + let winType; + { + for (let i = 0; i < fieldData.length; i++) { + if (fieldData[row][i] !== symbol) { + isLined = false; + break; + } + isLined = true; + } + winType = "ver"; + } + if (!isLined) { + for (let i = 0; i < fieldData.length; i++) { + if (fieldData[i][col] !== symbol) { + isLined = false; + break; + } + isLined = true; + } + winType = "hor"; + } + if (!isLined) { + for (let i = 0; i < fieldData.length; i++) { + if (fieldData[i][i] !== symbol) { + isLined = false; + break; + } + isLined = true; + } + winType = "diagL"; + } + if (!isLined) { + for (let i = 0; i < fieldData.length; i++) { + if (fieldData[i][fieldData.length - 1 - i] !== symbol) { + isLined = false; + break; + } + isLined = true; + } + winType = "diagR"; + } + + if (isLined) + changeColor(winType, row, col); + return isLined; +} + +function changeColor(way, row, col) +{ + for (let i = 0; i < fieldData.length; i++) { + switch (way) { + case "hor": + findCell(i, col).style.color = '#ff4040'; + break; + + case "ver": + findCell(row, i).style.color = '#ff4040'; + break; + + case "diagL": + findCell(i, i).style.color = '#ff4040'; + break; + + case "diagR": + findCell(i, fieldData.length - 1 - i).style.color = '#ff4040' + break; + } + } +} + +function computerMove () { - /* Пользоваться методом для размещения символа в клетке так: - renderSymbolInCell(ZERO, row, col); - */ } function renderSymbolInCell (symbol, row, col, color = '#333') { @@ -55,6 +155,7 @@ function addResetListener () { function resetClickHandler () { console.log('reset!'); + startGame(); } From 0057c4043fd53caf807cb64aa2801a1b8a2b2474 Mon Sep 17 00:00:00 2001 From: ohtoe02 Date: Fri, 8 Oct 2021 13:33:24 +0500 Subject: [PATCH 3/3] 1-9 --- index.js | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 15f0f88..0dbaaea 100644 --- a/index.js +++ b/index.js @@ -37,15 +37,13 @@ function renderGrid (dimension) { } function cellClickHandler (row, col) { - // Пиши код тут - console.log(`Clicked on cell: ${row}, ${col}`); - if (findCell(row,col).textContent === EMPTY && isPlayable) if(count % 2 === 0) processClick (ZERO, row, col) else processClick (CROSS, row, col) - console.log(`This cell is: ${fieldData[row][col]}`); + + console.log(`Clicked on cell: ${row}, ${col}`); } function processClick (symbol, row, col) { @@ -64,7 +62,7 @@ function checkWinner (symbol, row, col) { let isLined = true; let winType; { - for (let i = 0; i < fieldData.length; i++) { + for (let i = 0; i < dimension; i++) { if (fieldData[row][i] !== symbol) { isLined = false; break; @@ -74,7 +72,7 @@ function checkWinner (symbol, row, col) { winType = "ver"; } if (!isLined) { - for (let i = 0; i < fieldData.length; i++) { + for (let i = 0; i < dimension; i++) { if (fieldData[i][col] !== symbol) { isLined = false; break; @@ -84,7 +82,7 @@ function checkWinner (symbol, row, col) { winType = "hor"; } if (!isLined) { - for (let i = 0; i < fieldData.length; i++) { + for (let i = 0; i < dimension; i++) { if (fieldData[i][i] !== symbol) { isLined = false; break; @@ -94,8 +92,8 @@ function checkWinner (symbol, row, col) { winType = "diagL"; } if (!isLined) { - for (let i = 0; i < fieldData.length; i++) { - if (fieldData[i][fieldData.length - 1 - i] !== symbol) { + for (let i = 0; i < dimension; i++) { + if (fieldData[i][dimension - 1 - i] !== symbol) { isLined = false; break; } @@ -111,29 +109,40 @@ function checkWinner (symbol, row, col) { function changeColor(way, row, col) { - for (let i = 0; i < fieldData.length; i++) { + for (let i = 0; i < dimension; i++) { switch (way) { case "hor": - findCell(i, col).style.color = '#ff4040'; + changeCellColor(i, col) break; case "ver": - findCell(row, i).style.color = '#ff4040'; + changeCellColor(row, i) break; case "diagL": - findCell(i, i).style.color = '#ff4040'; + changeCellColor(i, i) break; case "diagR": - findCell(i, fieldData.length - 1 - i).style.color = '#ff4040' + changeCellColor(i, dimension - 1 - i) break; } } } +function changeCellColor(x, y) +{ + findCell(x, y).style.color = 'red' +} + + function computerMove () { + let x = getRandomInt(), y = getRandomInt(); + console.log(x, y); +} +function getRandomInt(max) { + return Math.floor(Math.random() * max); } function renderSymbolInCell (symbol, row, col, color = '#333') {