From 08a11554ec25e7ded8b9a54998281eee68c83dc4 Mon Sep 17 00:00:00 2001 From: Vearsmon Date: Tue, 27 Feb 2024 19:49:58 +0500 Subject: [PATCH] '' --- index.js | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 133 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 7553909..20e6fb9 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,13 @@ -const CROSS = 'X'; -const ZERO = 'O'; +const PLAYER1 = 'X'; +const PLAYER2 = 'O'; const EMPTY = ' '; +let counter = 0; +let winner = null; +let player = PLAYER1 +let field = [EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY]; + + const container = document.getElementById('fieldWrapper'); startGame(); @@ -27,15 +33,130 @@ function renderGrid (dimension) { } function cellClickHandler (row, col) { - // Пиши код тут - console.log(`Clicked on cell: ${row}, ${col}`); + if (winner === null) { + let cellCymbol = field[row * 3+ col]; + + if (cellCymbol === EMPTY && (winner === null || counter == 0)){ + let newCymbol = (player === PLAYER1) ? PLAYER1 : PLAYER2; + field[row * 3+ col] = player; + player = (player === PLAYER1) ? PLAYER2 : PLAYER1; + counter++; + renderSymbolInCell(newCymbol, row, col) + } + //printFieldAlert() + winner = checkWinner(); + + if (winner !== null){ + if (winner === PLAYER1){ + comb = findWinnerCombination() + alert(comb) + renderSymbolInCell (field[comb[0]], Math.trunc(comb[0] / 3), comb[0] % 3, '#990000'); + renderSymbolInCell (field[comb[1]], Math.trunc(comb[1] / 3), comb[1] % 3, '#990000'); + renderSymbolInCell (field[comb[2]], Math.trunc(comb[2] / 3), comb[2] % 3, '#990000'); + alert ("Победил Player 1", comb); + } + else if (winner === PLAYER2){ + comb = findWinnerCombination() + renderSymbolInCell (field[comb[0]], Math.trunc(comb[0] / 3), comb[0] % 3, '#990000'); + renderSymbolInCell (field[comb[1]], Math.trunc(comb[1] / 3), comb[1] % 3, '#990000'); + renderSymbolInCell (field[comb[2]], Math.trunc(comb[2] / 3), comb[2] % 3, '#990000'); + alert ("Победил Player 2", comb); + } + } + else if (checkDraw()){ + alert ("Победила дружба"); + } + + console.log(`Clicked on cell: ${row}, ${col}`); + } + /* Пользоваться методом для размещения символа в клетке так: renderSymbolInCell(ZERO, row, col); */ + + + +} + +function checkDraw () { + for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + if (field[i * 3 + j] === EMPTY) { + return false; + } + } + } + return true; +} + +function printFieldAlert() { + res = ''; + for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + if (field[i * 3 + j] === EMPTY) { + res += '_'; + } else { + res += field[i * 3 + j]; + } + } + + } + alert(res); +} + +function findWinnerCombination () { + let combs = [ + [0, 1, 2], + [3, 4, 5], + [6, 7, 8], + [0, 3, 6], + [1, 4, 7], + [2, 5, 8], + [0, 4, 8], + [2, 4, 6], + ]; + + for (let i = 0; i < 8; i++) { + if ( + field[combs[i][0]] === field[combs[i][1]] && + field[combs[i][1]] === field[combs[i][2]] && + field[combs[i][0]] != EMPTY + ) { + return combs[i]; + } + } + + return []; +} + + +function checkWinner () { + if ((field[0] === field[1] && field[1] === field[2] && field[0] === PLAYER1)|| + (field[3] === field[4] && field[4] === field[5] && field[3] === PLAYER1) || + (field[6] === field[7] && field[7] === field[8] && field[6] === PLAYER1) || + (field[0] === field[3] && field[3] === field[6] && field[0] === PLAYER1) || + (field[1] === field[4] && field[4] === field[7] && field[1] === PLAYER1) || + (field[2] === field[5] && field[5] === field[8] && field[2] === PLAYER1) || + (field[0] === field[4] && field[4] === field[8] && field[0] === PLAYER1) || + (field[2] === field[4] && field[4] === field[6] && field[6] === PLAYER1)) { + return PLAYER1 + } + else if ((field[0] === field[1] && field[1] === field[2] && field[0] === PLAYER2)|| + (field[3] === field[4] && field[4] === field[5] && field[3] === PLAYER2) || + (field[6] === field[7] && field[7] === field[8] && field[6] === PLAYER2) || + (field[0] === field[3] && field[3] === field[6] && field[0] === PLAYER2) || + (field[1] === field[4] && field[4] === field[7] && field[1] === PLAYER2) || + (field[2] === field[5] && field[5] === field[8] && field[2] === PLAYER2) || + (field[0] === field[4] && field[4] === field[8] && field[0] === PLAYER2) || + (field[2] === field[4] && field[4] === field[6] && field[6] === PLAYER2)) { + return PLAYER2 + } + else return null } + function renderSymbolInCell (symbol, row, col, color = '#333') { const targetCell = findCell(row, col); @@ -54,6 +175,14 @@ function addResetListener () { } function resetClickHandler () { + for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + field[i * 3 + j] = EMPTY; + renderSymbolInCell(EMPTY, i, j) + + } + } + winner = null console.log('reset!'); }