Skip to content
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

Новожилова Екатерина #47

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
96 changes: 92 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@ const EMPTY = ' ';

const container = document.getElementById('fieldWrapper');

let field = [];
let count = 0;
let isWin = false;

startGame();
addResetListener();

function startGame () {
renderGrid(3);
let dem = prompt("Размер поля:", 3);
renderGrid(dem);
count = dem*dem;
}

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

for (let i = 0; i < dimension; i++) {
field[i] = [];
const row = document.createElement('tr');
for (let j = 0; j < dimension; j++) {
const cell = document.createElement('td');
cell.textContent = EMPTY;
field[i][j] = EMPTY;
cell.addEventListener('click', () => cellClickHandler(i, j));
row.appendChild(cell);
}
Expand All @@ -28,21 +36,100 @@ function renderGrid (dimension) {

function cellClickHandler (row, col) {
// Пиши код тут
if(count%2 == 0)
getSymbol(row, col, CROSS);
else
getSymbol(row, col, ZERO);
console.log(`Clicked on cell: ${row}, ${col}`);
}

function getSymbol (row, col, sym){
if(findCell(row, col).textContent == EMPTY && !isWin)
{
renderSymbolInCell(sym, row, col);
field[row][col] = sym;
count-=1;
if(checkWinner (row, col, sym)){
alert(`Победитель: ${sym === CROSS ? "крестики" : "нолики"}`);
isWin = true;
}
if(count == 0 && !checkWinner (row, col, sym))
alert("Ничья");
}
}

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
function checkWinner (row, col, sym){
let flag = true;
let winWay = '';
for(let i = 0; i<field.length; i++){
if(field[row][i] !== sym){
flag = false;
break;
}
winWay= "gor";
}
if(!flag)
for(let i = 0; i<field.length; i++){
if(field[i][col] !== sym){
flag = false;
break;
}
flag = true;
winWay= "ver";
}
if(!flag)
for(let i = 0; i<field.length; i++){
if(field[i][i] !== sym){
flag = false;
break;
}
flag = true;
winWay="diagUp";
}
if(!flag)
for(let i = 0; i<field.length; i++){
if(field[i][field.length -1 - i] !== sym){
flag = false;
break;
}
flag = true;
winWay= "diagDown";
}
if(flag)
changeColor(winWay, row, col);
return flag;
}


function renderSymbolInCell (symbol, row, col, color = '#333') {
const targetCell = findCell(row, col);

targetCell.textContent = symbol;
targetCell.style.color = color;
}

function changeColor(way, row, col){
switch(way){
case "gor":
for(let i = 0; i<field.length; i++)
findCell(row, i).style.color = "#ff2605";
break;
case "ver":
for(let i = 0; i<field.length; i++)
findCell(i, col).style.color = "#ff2605";
break;
case "diagUp":
for(let i = 0; i<field.length; i++)
findCell(i, i).style.color = "#ff2605";
break;

case "diagDown":
for(let i = 0; i<field.length; i++)
findCell(i, field.length -1 - i).style.color = "#ff2605";
break;
}
}

function findCell (row, col) {
const targetRow = container.querySelectorAll('tr')[row];
return targetRow.querySelectorAll('td')[col];
Expand All @@ -55,6 +142,7 @@ function addResetListener () {

function resetClickHandler () {
console.log('reset!');
startGame();
}


Expand Down