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
75 changes: 72 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';

let count;
let dimension;
let startPosition;
let map;
let flag;

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

startGame();
Expand All @@ -12,10 +18,19 @@ function startGame () {
}

function renderGrid (dimension) {
container.innerHTML = '';
function renderGrid() {
dimension = prompt('Введите размер поля:');
map = new Map();
flag = false;
startPosition = 1;
count = 0;
container.innerHTML = '';
renderMap();
}

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;
Expand All @@ -28,14 +43,67 @@ function renderGrid (dimension) {

function cellClickHandler (row, col) {
// Пиши код тут
if (!(map.has(`${row}${col}`) || flag)) {
startPosition = (startPosition + 1) % 2;
let symbol = startPosition === 0 ? CROSS : ZERO;
map.set(`${row}${col}`, symbol);
renderSymbolInCell(symbol, row, col);
flag = map.size === dimension ** 2;

if (checkWin(symbol, row, col)) {
alert(`${symbol} победа`);
flag = true;
}

else if (flag) {
alert('Победила дружба');
}
console.log(`Clicked on cell: ${row}, ${col}`);


}
/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
}

function checkWin(symbol, row, col) {
for (let i = 0; i < dimension; i++) {
if (map.get(`${i}${col}`) !== symbol) break;
if (i === dimension - 1) {
for (let x = 0; x < dimension; x++) renderSymbolInCell(symbol, x, col, '#d40b0b');
return true;
}
}

for (let j = 0; j < dimension; j++) {
if (map.get(`${row}${j}`) !== symbol) break;

if (j === dimension - 1) {
for (let y = 0; y < dimension; y++) renderSymbolInCell(symbol, row, y, '#d40b0b');
return true;
}
}

for (let ij = 0; ij < dimension; ij++) {
if (map.get(`${dimension - ij - 1}${ij}`) !== symbol) break;

if (ij === dimension - 1) {
for (let x = 0; x < dimension; x++) renderSymbolInCell(symbol, dimension - 1 - x, x, '#d40b0b');
return true;
}
}

for (let ij = 0; ij < dimension; ij++) {
if (map.get(`${ij}${ij}`) !== symbol) break;

if (ij === dimension - 1) {
for (let x = 0; x < dimension; x++) renderSymbolInCell(symbol, x, x, '#d40b0b');
return true;
}
}

return false
}

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

Expand All @@ -54,6 +122,7 @@ function addResetListener () {
}

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

Expand Down