From 4480017df198f1a6d07eaf1c306c4895aa9750c9 Mon Sep 17 00:00:00 2001 From: rispele <50751327+Rispele@users.noreply.github.com> Date: Tue, 27 Feb 2024 18:38:43 +0500 Subject: [PATCH 1/3] YEAHHH WE WIN A PART OF TASKS --- index.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 7553909..abb7d23 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,42 @@ const CROSS = 'X'; const ZERO = 'O'; const EMPTY = ' '; +const DIMENSION = 3; const container = document.getElementById('fieldWrapper'); +let grid; +let currSymbol = CROSS; +let stepsCount = DIMENSION * DIMENSION; +let winner = EMPTY + + startGame(); addResetListener(); function startGame () { - renderGrid(3); + createGrid() + renderGrid(); +} + +function createGrid () { + grid = []; + for (let i = 0; i < DIMENSION; i++) { + grid[i] = []; + for (let j = 0; j < DIMENSION; j++) { + grid[i][j] = EMPTY; + } + } } -function renderGrid (dimension) { +function renderGrid () { container.innerHTML = ''; - for (let i = 0; i < dimension; i++) { + for (let i = 0; i < DIMENSION; i++) { const row = document.createElement('tr'); - for (let j = 0; j < dimension; j++) { + for (let j = 0; j < DIMENSION; j++) { const cell = document.createElement('td'); - cell.textContent = EMPTY; + cell.textContent = grid[i][j]; cell.addEventListener('click', () => cellClickHandler(i, j)); row.appendChild(cell); } @@ -27,13 +45,56 @@ function renderGrid (dimension) { } function cellClickHandler (row, col) { - // Пиши код тут + console.log(`Clicked on cell: ${row}, ${col}`); + if (grid[row][col] != EMPTY) { + return; + } + + renderSymbol(row, col); + + stepsCount -= 1; + + checkWinner(); +} - /* Пользоваться методом для размещения символа в клетке так: - renderSymbolInCell(ZERO, row, col); - */ +function renderSymbol(row, col) { + renderSymbolInCell(currSymbol, row, col); + grid[row][col] = currSymbol; + + switch (currSymbol) { + case ZERO: + currSymbol = CROSS; + break; + case CROSS: + currSymbol = ZERO; + break; + } +} + +function checkWinner() { + for (let i = 0; i < DIMENSION; i++) { + if (grid[i][0] == grid[i][1] == grid[i][2] && grid[i][0] != EMPTY) { + winner = grid[i][0]; + return; + } + + if (grid[0][i] == grid[1][i] == grid[2][i] && grid[0][i] != EMPTY) { + winner = grid[0][i] + return; + } + } + + if (grid[0][0] == grid[1][1] == grid[2][2] && grid[0][0] != EMPTY) { + winner = grid[0][0] + return; + } + + if (grid[0][2] == grid[1][1] == grid[2][0] && grid[0][0] != EMPTY) { + winner = grid[0][0] + return; + } } function renderSymbolInCell (symbol, row, col, color = '#333') { From ae6a226c271958f24545c30928f9379da2cdd3a4 Mon Sep 17 00:00:00 2001 From: rispele <50751327+Rispele@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:26:02 +0500 Subject: [PATCH 2/3] NEMNOGA GOTOVO --- index.js | 131 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 113 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index abb7d23..46833a3 100644 --- a/index.js +++ b/index.js @@ -1,21 +1,24 @@ const CROSS = 'X'; const ZERO = 'O'; const EMPTY = ' '; -const DIMENSION = 3; +let DIMENSION = 3; const container = document.getElementById('fieldWrapper'); let grid; let currSymbol = CROSS; let stepsCount = DIMENSION * DIMENSION; -let winner = EMPTY +let winner = EMPTY; +let winnerLoc = []; startGame(); addResetListener(); function startGame () { - createGrid() + DIMENSION = parseInt(prompt("Введи размер:", '3'), 10); + stepsCount = DIMENSION * DIMENSION; + createGrid(); renderGrid(); } @@ -47,7 +50,10 @@ function renderGrid () { function cellClickHandler (row, col) { console.log(`Clicked on cell: ${row}, ${col}`); - + if (winner != EMPTY) { + return; + } + if (grid[row][col] != EMPTY) { return; } @@ -56,7 +62,23 @@ function cellClickHandler (row, col) { stepsCount -= 1; - checkWinner(); + setWinner(); + + console.log(winner); + + if (winner != EMPTY) { + console.log(winnerLoc); + for(let loc of winnerLoc) { + renderSymbolInCell(winner, loc[0], loc[1], '#FF0000'); + } + alert(`Победили ${winner}!!`); + return; + } + + if (stepsCount == 0) { + alert('Победила дружба!!!'); + return; + } } function renderSymbol(row, col) { @@ -73,28 +95,94 @@ function renderSymbol(row, col) { } } -function checkWinner() { - for (let i = 0; i < DIMENSION; i++) { - if (grid[i][0] == grid[i][1] == grid[i][2] && grid[i][0] != EMPTY) { - winner = grid[i][0]; - return; +function setWinner() { + setWinnerHorizontal(); + setWinnerVertical(); + setWinnerDiagonal(); + setWinnerDiagonalReverse(); +} + +function setWinnerHorizontal () { + next: for (let i = 0; i < DIMENSION; i++) { + locations = [ [i, 0] ]; + checkValue = grid[i][0]; + + if (checkValue == EMPTY){ + continue next; } - - if (grid[0][i] == grid[1][i] == grid[2][i] && grid[0][i] != EMPTY) { - winner = grid[0][i] - return; + + for (let j = 1; j < DIMENSION; j++) { + if (grid[i][j] != checkValue) { + continue next; + } + locations.push([i, j]); + } + + winner = checkValue; + // for (let loc of ) + winnerLoc = winnerLoc.concat(locations); + break; + } +} + +function setWinnerVertical () { + next: for (let i = 0; i < DIMENSION; i++) { + locations = [ [0, i] ]; + checkValue = grid[0][i]; + + if (checkValue == EMPTY){ + continue next; } + + for (let j = 1; j < DIMENSION; j++) { + if (grid[j][i] != checkValue) { + continue next; + } + locations.push([j, i]); + } + + winner = checkValue; + winnerLoc = winnerLoc.concat(locations); + break; } +} + +function setWinnerDiagonal () { + locations = [ [0, 0] ]; + checkValue = grid[0][0]; - if (grid[0][0] == grid[1][1] == grid[2][2] && grid[0][0] != EMPTY) { - winner = grid[0][0] + if (checkValue == EMPTY){ return; } - if (grid[0][2] == grid[1][1] == grid[2][0] && grid[0][0] != EMPTY) { - winner = grid[0][0] + for (let i = 1; i < DIMENSION; i++) { + if (grid[i][i] != checkValue) { + return; + } + locations.push([i, i]); + } + + winner = checkValue; + winnerLoc = winnerLoc.concat(locations); +} + +function setWinnerDiagonalReverse () { + locations = [ [0, DIMENSION - 1] ]; + checkValue = grid[0][DIMENSION - 1]; + + if (checkValue == EMPTY){ return; } + + for (let i = 2; i <= DIMENSION; i++) { + if (grid[i][DIMENSION - i] != checkValue) { + return; + } + locations.push([i, DIMENSION - i]); + } + + winner = checkValue; + winnerLoc = winnerLoc.concat(locations); } function renderSymbolInCell (symbol, row, col, color = '#333') { @@ -116,6 +204,13 @@ function addResetListener () { function resetClickHandler () { console.log('reset!'); + + startGame(); + currSymbol = CROSS; + stepsCount = DIMENSION * DIMENSION; + winner = EMPTY; + winnerLoc = []; + } From f9c61b834896f85b4e82a09cd40de34d9a9aa40b Mon Sep 17 00:00:00 2001 From: rispele <50751327+Rispele@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:30:08 +0500 Subject: [PATCH 3/3] GOTOVO YEAAAAHHH 1-9 --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 46833a3..7c8ee8a 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ function cellClickHandler (row, col) { if (winner != EMPTY) { return; } - + if (grid[row][col] != EMPTY) { return; } @@ -174,11 +174,11 @@ function setWinnerDiagonalReverse () { return; } - for (let i = 2; i <= DIMENSION; i++) { - if (grid[i][DIMENSION - i] != checkValue) { + for (let i = 1; i < DIMENSION; i++) { + if (grid[i][DIMENSION - i - 1] != checkValue) { return; } - locations.push([i, DIMENSION - i]); + locations.push([i, DIMENSION - i - 1]); } winner = checkValue;