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..5f42f98 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,38 +36,176 @@ 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}`); + if(inGame) + stupidIntelligence(); + } + 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=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 () {