From 63b1adf1cf77ff044cadf04e113b6b52c268f75b Mon Sep 17 00:00:00 2001 From: debebantur Date: Tue, 28 Sep 2021 22:33:12 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=A4=D0=B8=D0=BB=D0=B8=D0=BF=D0=BF=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=20=D0=92=D0=BB?= =?UTF-8?q?=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit решил до 11 --- .idea/1-tic-tac-toe.iml | 9 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/runConfigurations.xml | 10 +++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 40 ++++++++++ index.js | 143 ++++++++++++++++++++++++++++++++---- 7 files changed, 207 insertions(+), 15 deletions(-) create mode 100644 .idea/1-tic-tac-toe.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/1-tic-tac-toe.iml b/.idea/1-tic-tac-toe.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/1-tic-tac-toe.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..fac583c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ 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/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..281922d --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + 1632673019106 + + + + + + \ No newline at end of file diff --git a/index.js b/index.js index 7553909..35b6919 100644 --- a/index.js +++ b/index.js @@ -4,20 +4,30 @@ const EMPTY = ' '; const container = document.getElementById('fieldWrapper'); +let tableSize; +let table ; +let totalPlaced; +let inGame = false; startGame(); addResetListener(); -function startGame () { - renderGrid(3); +function startGame () { // обновление поля + inGame = true; + tableSize = prompt('Задайте сторону поля',3); + table = new Array(tableSize); + totalPlaced = 0; + renderGrid(tableSize); } -function renderGrid (dimension) { +function renderGrid (dimension) {//создаёт поле container.innerHTML = ''; for (let i = 0; i < dimension; i++) { const row = document.createElement('tr'); + table[i] = new Array(tableSize); for (let j = 0; j < dimension; j++) { const cell = document.createElement('td'); + table[i][j] = EMPTY; cell.textContent = EMPTY; cell.addEventListener('click', () => cellClickHandler(i, j)); row.appendChild(cell); @@ -26,37 +36,140 @@ function renderGrid (dimension) { } } -function cellClickHandler (row, col) { - // Пиши код тут - console.log(`Clicked on cell: ${row}, ${col}`); - +function cellClickHandler (row, col) { // срабатывает по клику на поле + if(!inGame) + return; + let cell = findCell(row, col); + if(cell.textContent == EMPTY){ + placeSymbolInCell(CROSS, row, col); + console.log(`Clicked on cell: ${row}, ${col}`); + //console.log('stupidIntelligence start'); + if(inGame) + stupidIntelligence(); + //console.log('stupidIntelligence end'); + } + else + console.log(`Clicked on not empty cell: ${row}, ${col}`); +} - /* Пользоваться методом для размещения символа в клетке так: - renderSymbolInCell(ZERO, row, col); - */ +function placeSymbolInCell (symbol, row, col) { //записывает символ + drawSymbol (symbol, row, col, color = '#333') + table[row][col] = symbol; + totalPlaced+=1; + findReasonToEndGame(row, col); } -function renderSymbolInCell (symbol, row, col, color = '#333') { +function drawSymbol (symbol, row, col, color) { const targetCell = findCell(row, col); - targetCell.textContent = symbol; targetCell.style.color = color; } -function findCell (row, col) { +function findCell (row, col) {// возвращает клетку const targetRow = container.querySelectorAll('tr')[row]; return targetRow.querySelectorAll('td')[col]; } -function addResetListener () { +function addResetListener () { //привязка кнопки перезапуска const resetButton = document.getElementById('reset'); resetButton.addEventListener('click', resetClickHandler); } -function resetClickHandler () { +function resetClickHandler () { // работа кнопки перезапуска + startGame(); console.log('reset!'); } +function stupidIntelligence(){ //интеллект из пункта 10 + while(true){ + let row = Math.floor((Math.random()*tableSize))%tableSize; + let col = Math.floor((Math.random()*tableSize))%tableSize; + let cell = findCell(row, col) + if (cell.textContent == EMPTY){ + placeSymbolInCell(ZERO, row, col); + break; + } + } +} + +function findReasonToEndGame(lastRow, lastCol){ // обработка возможных окончаний игры + if(totalPlaced == tableSize*tableSize){ + inGame = false; + alert('победила дружба'); + return; + } + + let sym = findCell(lastRow, lastCol).textContent; + let win = true; + + for(let i =0; i Date: Thu, 30 Sep 2021 22:16:39 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=2012=20=D0=BF=D1=83=D0=BD=D0=BA=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 35b6919..5f42f98 100644 --- a/index.js +++ b/index.js @@ -43,10 +43,8 @@ function cellClickHandler (row, col) { // срабатывает по клик if(cell.textContent == EMPTY){ placeSymbolInCell(CROSS, row, col); console.log(`Clicked on cell: ${row}, ${col}`); - //console.log('stupidIntelligence start'); if(inGame) stupidIntelligence(); - //console.log('stupidIntelligence end'); } else console.log(`Clicked on not empty cell: ${row}, ${col}`); @@ -98,7 +96,6 @@ function findReasonToEndGame(lastRow, lastCol){ // обработка возмо alert('победила дружба'); return; } - let sym = findCell(lastRow, lastCol).textContent; let win = true; @@ -132,7 +129,6 @@ function findReasonToEndGame(lastRow, lastCol){ // обработка возмо console.log('победа по горизонтали'); return; } - if(lastCol == lastRow){ win = true; for(let i =0; i=Number(tableSize)*Number(tableSize)){ + console.log('checking largness enter if'); + EnlargeField() + } } - +function EnlargeField() { + let newField = new Array(Number(tableSize)+2); +console.log('enlarging'); + for(let i=0;i cellClickHandler(i, j)); + row.appendChild(cell); + } + container.appendChild(row); + } +} /* Test Function */ /* Победа первого игрока */ function testWin () {