From eadcfe21f09f26537cc918a4a0fa2f71d19c54fe Mon Sep 17 00:00:00 2001 From: "Aratelnar (Boyakov M)" Date: Mon, 27 Feb 2023 18:17:24 +0500 Subject: [PATCH 1/6] render --- index.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 7553909..7fb875f 100644 --- a/index.js +++ b/index.js @@ -3,12 +3,14 @@ const ZERO = 'O'; const EMPTY = ' '; const container = document.getElementById('fieldWrapper'); +let grid = []; startGame(); addResetListener(); function startGame () { - renderGrid(3); + renderGrid(4); + createGrid(4) } function renderGrid (dimension) { @@ -26,14 +28,30 @@ function renderGrid (dimension) { } } +function createGrid(dimension) { + for (let i = 0; i < dimension; i++) { + grid[i] = [] + for (let j = 0; j < dimension; j++) { + grid[i][j] = EMPTY + } + } +} + +let parity = false; function cellClickHandler (row, col) { - // Пиши код тут console.log(`Clicked on cell: ${row}, ${col}`); - /* Пользоваться методом для размещения символа в клетке так: - renderSymbolInCell(ZERO, row, col); - */ + if(grid[row][col] === EMPTY){ + if(parity){ + grid[row][col] = CROSS; + renderSymbolInCell(CROSS, row, col, "#c02020") + } else { + grid[row][col] = ZERO; + renderSymbolInCell(ZERO, row, col, "#208020") + } + } + parity = !parity; } function renderSymbolInCell (symbol, row, col, color = '#333') { From 60894251ca93142d5eccfeff01c41e23dae1f77e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Mon, 27 Feb 2023 18:28:42 +0500 Subject: [PATCH 2/6] checkWin --- index.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/index.js b/index.js index 7553909..92706f4 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const CROSS = 'X'; const ZERO = 'O'; const EMPTY = ' '; +const NO = 'No'; const container = document.getElementById('fieldWrapper'); @@ -26,6 +27,58 @@ function renderGrid (dimension) { } } +function checkWinCondition(){ + for(let i = 0; i < grid.size(); i++){ + let countZero = 0; + let countCross = 0; + for(let j of grid[i]) { + if (j === ZERO) + countZero++; + if (j === CROSS) + countCross++; + } + + if (array.size() === countZero) + return ZERO + if (array.size() === countCross) + return CROSS + } + + + for(let i = 0; i < grid.size(); i++){ + let countZero = 0; + let countCross = 0; + for(let j = 0; j < grid.size(); j++) { + if (grid[j][i] === ZERO) + countZero++; + if (grid[j][i] === CROSS) + countCross++; + } + + if (array.size() === countZero) + return ZERO + if (array.size() === countCross) + return CROSS + } + + let countZero = 0; + let countCross = 0; + + for(let i = 0; i < grid.size(); i++) { + if (grid[i][i] === ZERO) + countZero++; + if (grid[i][i] === CROSS) + countCross++; + } + + if (array.size() === countZero) + return ZERO + if (array.size() === countCross) + return CROSS + + return NO; +} + function cellClickHandler (row, col) { // Пиши код тут console.log(`Clicked on cell: ${row}, ${col}`); From 5c0df6145872315347047ea72b689f5396e0c40e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Mon, 27 Feb 2023 18:47:05 +0500 Subject: [PATCH 3/6] checkWin version 2 --- index.js | 68 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 26b7ee0..ea41fe8 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const CROSS = 'X'; const ZERO = 'O'; const EMPTY = ' '; const NO = 'No'; +const DIMENSION = 3; const container = document.getElementById('fieldWrapper'); let grid = []; @@ -10,8 +11,8 @@ startGame(); addResetListener(); function startGame () { - renderGrid(4); - createGrid(4) + renderGrid(DIMENSION); + createGrid(DIMENSION) } function renderGrid (dimension) { @@ -30,55 +31,69 @@ function renderGrid (dimension) { } function checkWinCondition(){ - for(let i = 0; i < grid.size(); i++){ + let arr = []; + + for(let i = 0; i < DIMENSION; i++){ let countZero = 0; let countCross = 0; - for(let j of grid[i]) { - if (j === ZERO) + for(let j = 0; j < DIMENSION; j++) { + arr.push([i, j]); + if (grid[i][j] === ZERO) countZero++; - if (j === CROSS) + if (grid[i][j] === CROSS) countCross++; } - - if (array.size() === countZero) - return ZERO - if (array.size() === countCross) - return CROSS + console.log(countZero); + if (DIMENSION === countZero || DIMENSION === countCross) + return arr } + arr = []; - - for(let i = 0; i < grid.size(); i++){ + for(let i = 0; i < DIMENSION; i++){ let countZero = 0; let countCross = 0; - for(let j = 0; j < grid.size(); j++) { + for(let j = 0; j < DIMENSION; j++) { + arr.push([j, i]); if (grid[j][i] === ZERO) countZero++; if (grid[j][i] === CROSS) countCross++; } - - if (array.size() === countZero) - return ZERO - if (array.size() === countCross) - return CROSS + console.log(countZero); + if (DIMENSION === countZero || DIMENSION === countCross) + return arr } - + arr = []; let countZero = 0; let countCross = 0; - for(let i = 0; i < grid.size(); i++) { + for(let i = 0; i < DIMENSION; i++) { + arr.push([i, i]); if (grid[i][i] === ZERO) countZero++; if (grid[i][i] === CROSS) countCross++; } - if (array.size() === countZero) - return ZERO - if (array.size() === countCross) - return CROSS + if (DIMENSION === countZero || DIMENSION === countCross) + return arr + + + countZero = 0; + countCross = 0; + arr = []; + for(let i = 0; i < DIMENSION; i++) { + arr.push([DIMENSION - i - 1, i]); + if (grid[DIMENSION - i - 1][i] === ZERO) + countZero++; + if (grid[DIMENSION - i - 1][i] === CROSS) + countCross++; + } + + if (DIMENSION === countZero || DIMENSION === countCross) + return arr - return NO; + return false; } function createGrid(dimension) { @@ -105,6 +120,7 @@ function cellClickHandler (row, col) { } } parity = !parity; + console.log(checkWinCondition()); } function renderSymbolInCell (symbol, row, col, color = '#333') { From 582e22edb0ed8269ee72086268ef040432408345 Mon Sep 17 00:00:00 2001 From: "Aratelnar (Boyakov M)" Date: Mon, 27 Feb 2023 18:48:06 +0500 Subject: [PATCH 4/6] render2 --- index.js | 77 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 7fb875f..296e57b 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,21 @@ const CROSS = 'X'; const ZERO = 'O'; const EMPTY = ' '; +const DIMENSION = 3; const container = document.getElementById('fieldWrapper'); let grid = []; +let ended = false; startGame(); addResetListener(); -function startGame () { - renderGrid(4); - createGrid(4) +function startGame() { + renderGrid(DIMENSION); + createGrid(DIMENSION) } -function renderGrid (dimension) { +function renderGrid(dimension) { container.innerHTML = ''; for (let i = 0; i < dimension; i++) { @@ -38,47 +40,90 @@ function createGrid(dimension) { } let parity = false; -function cellClickHandler (row, col) { + +function checkEndGame() { + const line = checkWinCondition(); + if (line) { + let win = grid[line[0][0]][line[0][1]] + switch (win) { + case CROSS: + alert(CROSS); + ended = true; + break; + case ZERO: + alert(ZERO); + ended = true; + break; + } + } + if (checkDraw(DIMENSION)) + alert("Победила дружба!") + +} + +function renderWinner(line) { + for (let coord of line){ + const symbol = grid[coord[0]][coord[1]] + renderSymbolInCell(symbol, coord[0], coord[1], "#c00") + } +} + +function cellClickHandler(row, col) { console.log(`Clicked on cell: ${row}, ${col}`); - if(grid[row][col] === EMPTY){ - if(parity){ + if (grid[row][col] === EMPTY && !ended) { + if (parity) { grid[row][col] = CROSS; - renderSymbolInCell(CROSS, row, col, "#c02020") + renderSymbolInCell(CROSS, row, col) } else { grid[row][col] = ZERO; - renderSymbolInCell(ZERO, row, col, "#208020") + renderSymbolInCell(ZERO, row, col) } } parity = !parity; + checkEndGame(); +} + +function checkDraw(dimension) { + let empty = 0; + for (let i = 0; i < dimension; i++) { + for (let j = 0; j < dimension; j++) { + if (grid[i][j] === EMPTY) + empty++; + } + } + return empty === 0; } -function renderSymbolInCell (symbol, row, col, color = '#333') { +function renderSymbolInCell(symbol, row, col, color = '#333') { 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() { console.log('reset!'); + createGrid(3); + renderGrid(3); } /* Test Function */ + /* Победа первого игрока */ -function testWin () { +function testWin() { clickOnCell(0, 2); clickOnCell(0, 0); clickOnCell(2, 0); @@ -89,7 +134,7 @@ function testWin () { } /* Ничья */ -function testDraw () { +function testDraw() { clickOnCell(2, 0); clickOnCell(1, 0); clickOnCell(1, 1); @@ -102,6 +147,6 @@ function testDraw () { clickOnCell(2, 2); } -function clickOnCell (row, col) { +function clickOnCell(row, col) { findCell(row, col).click(); } From 306b9264e0c742fb5ff3e621f612fe01da96a9bc Mon Sep 17 00:00:00 2001 From: "Aratelnar (Boyakov M)" Date: Mon, 27 Feb 2023 19:03:47 +0500 Subject: [PATCH 5/6] done-basic --- index.js | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index a899627..fea3597 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ const CROSS = 'X'; const ZERO = 'O'; const EMPTY = ' '; -const DIMENSION = 3; +let DIMENSION = 3; const container = document.getElementById('fieldWrapper'); let grid = []; @@ -11,6 +11,7 @@ startGame(); addResetListener(); function startGame() { + DIMENSION = +prompt("Введите размер поля") renderGrid(DIMENSION); createGrid(DIMENSION) } @@ -30,13 +31,13 @@ function renderGrid(dimension) { } } -function checkWinCondition(){ - let arr = []; +function checkWinCondition() { + for (let i = 0; i < DIMENSION; i++) { - for(let i = 0; i < DIMENSION; i++){ + let arr = []; let countZero = 0; let countCross = 0; - for(let j = 0; j < DIMENSION; j++) { + for (let j = 0; j < DIMENSION; j++) { arr.push([i, j]); if (grid[i][j] === ZERO) countZero++; @@ -47,12 +48,11 @@ function checkWinCondition(){ if (DIMENSION === countZero || DIMENSION === countCross) return arr } - arr = []; - - for(let i = 0; i < DIMENSION; i++){ + for (let i = 0; i < DIMENSION; i++) { + arr = []; let countZero = 0; let countCross = 0; - for(let j = 0; j < DIMENSION; j++) { + for (let j = 0; j < DIMENSION; j++) { arr.push([j, i]); if (grid[j][i] === ZERO) countZero++; @@ -67,7 +67,7 @@ function checkWinCondition(){ let countZero = 0; let countCross = 0; - for(let i = 0; i < DIMENSION; i++) { + for (let i = 0; i < DIMENSION; i++) { arr.push([i, i]); if (grid[i][i] === ZERO) countZero++; @@ -82,7 +82,7 @@ function checkWinCondition(){ countZero = 0; countCross = 0; arr = []; - for(let i = 0; i < DIMENSION; i++) { + for (let i = 0; i < DIMENSION; i++) { arr.push([DIMENSION - i - 1, i]); if (grid[DIMENSION - i - 1][i] === ZERO) countZero++; @@ -113,22 +113,24 @@ function checkEndGame() { let win = grid[line[0][0]][line[0][1]] switch (win) { case CROSS: - alert(CROSS); + alert("CROSS"); ended = true; + renderWinner(line) break; case ZERO: - alert(ZERO); + alert("ZERO"); ended = true; + renderWinner(line) break; } + } else { + if (checkDraw(DIMENSION)) + alert("Победила дружба!") } - if (checkDraw(DIMENSION)) - alert("Победила дружба!") - } function renderWinner(line) { - for (let coord of line){ + for (let coord of line) { const symbol = grid[coord[0]][coord[1]] renderSymbolInCell(symbol, coord[0], coord[1], "#c00") } @@ -137,8 +139,10 @@ function renderWinner(line) { function cellClickHandler(row, col) { console.log(`Clicked on cell: ${row}, ${col}`); - - if (grid[row][col] === EMPTY && !ended) { + if (ended) { + return; + } + if (grid[row][col] === EMPTY) { if (parity) { grid[row][col] = CROSS; renderSymbolInCell(CROSS, row, col) @@ -181,8 +185,10 @@ function addResetListener() { function resetClickHandler() { console.log('reset!'); - createGrid(3); - renderGrid(3); + ended = false; + parity = false; + createGrid(DIMENSION); + renderGrid(DIMENSION); } From 9623ea1241f9631a2c6b87501972009cffb488a4 Mon Sep 17 00:00:00 2001 From: "Aratelnar (Boyakov M)" Date: Mon, 27 Feb 2023 19:10:54 +0500 Subject: [PATCH 6/6] done-basic --- index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index fea3597..1979514 100644 --- a/index.js +++ b/index.js @@ -11,9 +11,9 @@ startGame(); addResetListener(); function startGame() { - DIMENSION = +prompt("Введите размер поля") + DIMENSION = +prompt("Введите размер поля"); + createGrid(DIMENSION); renderGrid(DIMENSION); - createGrid(DIMENSION) } function renderGrid(dimension) { @@ -185,10 +185,9 @@ function addResetListener() { function resetClickHandler() { console.log('reset!'); - ended = false; parity = false; - createGrid(DIMENSION); - renderGrid(DIMENSION); + ended = false; + startGame(); }