Skip to content

Карцева Арина #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 108 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,138 @@ const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';

const container = document.getElementById('fieldWrapper');
let currentSymbol = CROSS;

startGame();
addResetListener();
let field = [
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]
];

function startGame () {
renderGrid(3);
}
let countEMPTY = 9;

function renderGrid (dimension) {
container.innerHTML = '';
let hasWinner = false;

for (let i = 0; i < dimension; i++) {
const row = document.createElement('tr');
for (let j = 0; j < dimension; j++) {
const cell = document.createElement('td');
cell.textContent = EMPTY;
cell.addEventListener('click', () => cellClickHandler(i, j));
row.appendChild(cell);
}
container.appendChild(row);
}
const container = document.getElementById('fieldWrapper');

startGame();
@@ -27,13 +39,92 @@ function renderGrid (dimension) {
}

function cellClickHandler (row, col) {
// Пиши код тут
if(!hasWinner) {
if (field[row][col] === EMPTY) {
if(currentSymbol == CROSS) {
renderSymbolInCell(CROSS, row, col);
currentSymbol = ZERO;
field[row][col] = CROSS;
countEMPTY -= 1;
}
else {
renderSymbolInCell(ZERO, row, col);
currentSymbol = CROSS;
field[row][col] = ZERO;
countEMPTY -= 1;
}
}
}
if(findWinner() == CROSS) {
alert('Победили крестики');
hasWinner = true;
}
else if(findWinner() == ZERO) {
alert('Победили нолики');
hasWinner = true;
}
else if(countEMPTY === 0) alert('Победила дружба! :)');
console.log(`Clicked on cell: ${row}, ${col}`);
}

function findWinner() {
let sym = field[0][0];
if ((sym !== EMPTY) && (field[0][0] === sym) && (field[0][1] === sym) && (field[0][2] === sym)) {
renderSymbolInCell (sym, 0, 0, color = '#ff0000');
renderSymbolInCell (sym, 0, 1, color = '#ff0000');
renderSymbolInCell (sym, 0, 2, color = '#ff0000');
return sym;
}
if ((sym !== EMPTY) && (field[0][0] === sym) && (field[1][0] === sym) && (field[2][0] === sym)) {
renderSymbolInCell (sym, 0, 0, color = '#ff0000');
renderSymbolInCell (sym, 1, 0, color = '#ff0000');
renderSymbolInCell (sym, 2, 0, color = '#ff0000');
return sym;
}
if ((sym !== EMPTY) && (field[0][0] === sym) && (field[1][1] === sym) && (field[2][2] === sym)) {
renderSymbolInCell (sym, 0, 0, color = '#ff0000');
renderSymbolInCell (sym, 1, 1, color = '#ff0000');
renderSymbolInCell (sym, 2, 2, color = '#ff0000');
return sym;
}

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
}
sym = field[1][0];
if ((sym !== EMPTY) && (field[1][0] === sym) && (field[1][1] === sym) && (field[1][2] === sym)) {
renderSymbolInCell (sym, 1, 0, color = '#ff0000');
renderSymbolInCell (sym, 1, 1, color = '#ff0000');
renderSymbolInCell (sym, 1, 2, color = '#ff0000');
return sym;
}

function renderSymbolInCell (symbol, row, col, color = '#333') {
const targetCell = findCell(row, col);
sym = field[2][0];
if ((sym !== EMPTY) && (field[2][0] === sym) && (field[2][1] === sym) && (field[2][2] === sym)) {
renderSymbolInCell (sym, 2, 0, color = '#ff0000');
renderSymbolInCell (sym, 2, 1, color = '#ff0000');
renderSymbolInCell (sym, 2, 2, color = '#ff0000');
return sym;
}
if ((sym !== EMPTY) && (field[2][0] === sym) && (field[1][1] === sym) && (field[0][2] === sym)) {
renderSymbolInCell (sym, 2, 0, color = '#ff0000');
renderSymbolInCell (sym, 1, 1, color = '#ff0000');
renderSymbolInCell (sym, 0, 2, color = '#ff0000');
return sym;
}

targetCell.textContent = symbol;
targetCell.style.color = color;
}
sym = field[0][1];
if ((sym !== EMPTY) && (field[0][1] === sym) && (field[1][1] === sym) && (field[2][1] === sym)) {
renderSymbolInCell (sym, 0, 1, color = '#ff0000');
renderSymbolInCell (sym, 1, 1, color = '#ff0000');
renderSymbolInCell (sym, 2, 1, color = '#ff0000');
return sym;
}

function findCell (row, col) {
const targetRow = container.querySelectorAll('tr')[row];
return targetRow.querySelectorAll('td')[col];
sym = field[0][2];
if ((sym !== EMPTY) && (field[0][2] === sym) && (field[1][2] === sym) && (field[2][2] === sym)) {
renderSymbolInCell (sym, 0, 2, color = '#ff0000');
renderSymbolInCell (sym, 1, 2, color = '#ff0000');
renderSymbolInCell (sym, 2, 2, color = '#ff0000');
return sym;
}
}

function addResetListener () {
const resetButton = document.getElementById('reset');
resetButton.addEventListener('click', resetClickHandler);
function renderSymbolInCell (symbol, row, col, color = '#333') {
@@ -54,6 +145,19 @@ function addResetListener () {
}

function resetClickHandler () {
currentSymbol = CROSS;

field = [
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]
];

countEMPTY = 9;
for(let i = 0; i < field.length; i++)
for(let j = 0; j < field.length; j++)
renderSymbolInCell (EMPTY, i, j);

console.log('reset!');
}


/* Test Function */
/* Победа первого игрока */
function testWin () {
Expand All @@ -69,7 +145,6 @@ function testWin () {
clickOnCell(1, 2);
clickOnCell(2, 1);
}

/* Ничья */
function testDraw () {
clickOnCell(2, 0);
Expand All @@ -83,7 +158,6 @@ function testDraw () {
clickOnCell(2, 1);
clickOnCell(2, 2);
}

function clickOnCell (row, col) {
findCell(row, col).click();
}
}