Skip to content
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
120 changes: 115 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,32 @@ const ZERO = 'O';
const EMPTY = ' ';

const container = document.getElementById('fieldWrapper');
let fieldData;
let count;
let isPlayable;
let dimension;

startGame();
addResetListener();

function startGame () {
renderGrid(3);
dimension = prompt("Введите размер поля");
count = dimension**2;
fieldData = [];
isPlayable = true;
renderGrid(dimension);
}

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

for (let i = 0; i < dimension; i++) {
const row = document.createElement('tr');
fieldData.push([]);
for (let j = 0; j < dimension; j++) {
const cell = document.createElement('td');
cell.textContent = EMPTY;
fieldData[i][j] = EMPTY;
cell.addEventListener('click', () => cellClickHandler(i, j));
row.appendChild(cell);
}
Expand All @@ -27,13 +37,112 @@ function renderGrid (dimension) {
}

function cellClickHandler (row, col) {
// Пиши код тут
if (findCell(row,col).textContent === EMPTY && isPlayable)
if(count % 2 === 0)
processClick (ZERO, row, col)
else
processClick (CROSS, row, col)

console.log(`Clicked on cell: ${row}, ${col}`);
}

function processClick (symbol, row, col) {
renderSymbolInCell(symbol, row, col);
fieldData[row][col] = symbol;
if (checkWinner(symbol, row, col)) {
alert(`${symbol === CROSS ? "Крестики" : "Нолики"} победили!`)
isPlayable = false;
}
if (--count === 0 && isPlayable)
alert("Победила дружба!");
}

function checkWinner (symbol, row, col) {

let isLined = true;
let winType;
{
for (let i = 0; i < dimension; i++) {
if (fieldData[row][i] !== symbol) {
isLined = false;
break;
}
isLined = true;
}
winType = "ver";
}
if (!isLined) {
for (let i = 0; i < dimension; i++) {
if (fieldData[i][col] !== symbol) {
isLined = false;
break;
}
isLined = true;
}
winType = "hor";
}
if (!isLined) {
for (let i = 0; i < dimension; i++) {
if (fieldData[i][i] !== symbol) {
isLined = false;
break;
}
isLined = true;
}
winType = "diagL";
}
if (!isLined) {
for (let i = 0; i < dimension; i++) {
if (fieldData[i][dimension - 1 - i] !== symbol) {
isLined = false;
break;
}
isLined = true;
}
winType = "diagR";
}

if (isLined)
changeColor(winType, row, col);
return isLined;
}

function changeColor(way, row, col)
{
for (let i = 0; i < dimension; i++) {
switch (way) {
case "hor":
changeCellColor(i, col)
break;

case "ver":
changeCellColor(row, i)
break;

case "diagL":
changeCellColor(i, i)
break;

case "diagR":
changeCellColor(i, dimension - 1 - i)
break;
}
}
}

function changeCellColor(x, y)
{
findCell(x, y).style.color = 'red'
}


function computerMove () {
let x = getRandomInt(), y = getRandomInt();
console.log(x, y);
}

/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}

function renderSymbolInCell (symbol, row, col, color = '#333') {
Expand All @@ -55,6 +164,7 @@ function addResetListener () {

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


Expand Down